cswork / OAOpen practice workspace

RobloxOA #13

Schedule Meeting

Estimated MediumJudge-ready

Topics (AI-labelled)Prefix & RangesGreedy

Given each employee's busy intervals within a day and the meeting length, find the earliest start minute at which all employees are free and the meeting can finish within the day; if there is none, output -1. Added by this site: times use half-open intervals [start,finish), so touching endpoints do not conflict; input intervals may overlap or be out of order.

Input

The first line contains n length; for each employee, the number of intervals m is given, followed by m pairs start finish. 1≤n≤100, 0≤m≤100, 0≤start<finish≤1400, 1≤length≤1440.

Output

Output the earliest start time; if no time works, output -1.

Example 1

Input:

2 120
2
60 150
180 240
2
0 210
360 420

Output:

240

Example 2

Input:

3 180
1
480 510
1
240 330
1
375 400

Output:

0

Example 3

Input:

1 1440
1
0 1400

Output:

-1

Editorial

Loading solution…