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 \(10^6\) days as soon as possible in a day that none of them are busy in that day.
His \(i\)th friend is busy in \(m_i\) intervals of days:
- His \(i\)th friend is busy from \(l_{i, 1}\)th to \(r_{i, 1}\)th day (includes both \(l_{i, 1}\)th and \(r_{i, 1}\)th day), also he/she is busy from \(l_{i, 2}\)th to \(r_{i, 2}\)th day also .... from \(l_{i, m_i}\)th to \(r_{i, m_i}\)th day.
- \(1 \le l_{i, 1} \leq r_{i, 1} \leq l_{i, 2} \leq r_{i, 2} \leq ... \leq l_{i, m_i} \leq r_{i, m_i} \leq 10^6\).
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 \(i\)th pair of lines contains an integer \(m_i\).
- Second line contains \(2 \times m_i\) integers \(l_{i, 1}, r_{i, 1}, l_{i, 2}, r_{i, 2}, ... l_{i, m_i}, r_{i, m_i}\) separated by space.
- \(1 \le l_{i, 1} \leq r_{i, 1} \leq l_{i, 2} \leq r_{i, 2} \leq ... \leq l_{i, m_i} \leq r_{i, m_i} \leq 10^6\).
\(1 \leq n \leq 2 \times 10^5\)
\(0 \leq \sum {m_i} \leq 2 \times 10^5\)
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\).
4 2 1 2 3 5 2 1 3 5 7 2 1 3 4 5 2 3 6 11 12
8
All Y's firends has two busy intervalse that they cover all days from \(1\) to \(7\). all of them are not busy in \(8\)th day, so the answer is \(8\).
Please login to use the editor
You need to be logged in to access the code editor
Loading...
Please wait while we load the editor
Login to unlock the editorial
Please login to use the editor
You need to be logged in to access the code editor
Loading...
Please wait while we load the editor