Plan for nothing

3.7

40 votes
Introduction to Dynamic Programming-2, Algorithms, Dynamic Programming
Problem

Not a long time ago Y wasn't a prisoner and he had a lot of friends (n friends). His friends are going to plan a date in upcoming 106 days as soon as possible in a day that none of them are busy in that day.

His ith friend is busy in mi intervals of days:

  • His ith friend is busy from li,1th to ri,1th day (includes both  li,1th and  ri,1th day), also he/she is busy from  li,2th to ri,2th day also .... from li,mith to ri,mith day.
  • 1li,1ri,1li,2ri,2...li,miri,mi106.

What is the earliest day that none of his friends are busy?

Input

First line contains only an integer n, number of his friends.

Then follwing n pairs for lines contain:

  • First line of ith pair of lines contains an integer mi.
  • Second line contains 2×mi integers li,1,ri,1,li,2,ri,2,...li,mi,ri,mi separated by space.
  • 1li,1ri,1li,2ri,2...li,miri,mi106.

1n2×105

0mi2×105

Output

The only line of output contains an integer, the earliest day that none of his friends are busy. if there isn't any day that none of them are busy print 1.

Sample Input
4
2 1 2 3 5
2 1 3 5 7
2 1 3 4 5
2 3 6 11 12
Sample Output
8
Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

All Y's firends has two busy intervalse that they cover all days from 1 to 7. all of them are not busy in 8th day, so the answer is 8.

Editor Image

?