Hide

Problem B
Billy the apprentice

Spring is in the air, and the Easter magic is undeniably here!

Apprentice bunny Billy joins Eggwynne Academy of Sorcery for training which is spread over $n$ consecutive days. On the $i$-th day, there are $k_i$ programs with varying difficulties on offer. Billy must choose exactly one program each day, and at the end of that day, his magic power will be the same as the difficulty of his chosen program. Billy starts training with zero magic power and wishes to achieve the maximum at the completion.

However, life is not straightforward. By the Academy motto, ‘Semper Ante!’ meaning ‘Always Ahead!’, Billy is not allowed to choose a program with difficulty less than his current magic power, and if he’s unable to choose a program for any day, his training is terminated without completion.

Input

The first line contains a single number $n$ ($2 \le n \le 10^3$) —the number of training days. The following $n$ lines give the daily programs. The first number in the $i$-th line is $k_i$ ($1 \le k_i \le 10^3$) —the number of programs on the $i$-th day. The rest of the line contains ${k_i}$ positive integers not exceeding $10000$ — the difficulties of the programs.

Output

Output a single number —the maximum magic power Billy can achieve, or $-1$ if he cannot complete the training.

Notes

In the first testcase, Billy can choose $1$ for the first day and $15$ for the second.

In the second testcase, Billy cannot complete the training because his magic power after the first day rules him out of all programs on offer for the second day.

Sample Input 1 Sample Output 1
2
2 1 40
3 5 6 15
15
Sample Input 2 Sample Output 2
2
2 4 5
2 3 2
-1

Please log in to submit a solution to this problem

Log in