cswork / OAOpen practice workspace

MetaOA #4

Earliest Common Meeting Slot

Estimated MediumJudge-ready

Topics (AI-labelled)Prefix & Ranges

A day is [0,1440) minutes. Given the busy half-open intervals of all employees, find the earliest integer start time at which all employees are free and a meeting of the given length fits; if none exists, output −1. The original site's example answer of 240 conflicts with the busy intervals, so this site corrected it to 60.

Input

This site combines all employees' busy intervals into one input: the first line contains the total number of intervals b (0..100000) and the meeting length L (1..1440); the next b lines are start finish (0≤start<finish≤1440). Intervals may overlap and may be unsorted.

Output

Output the answer as described; separate multiple results with spaces in order, and output an empty line for an empty array.

Example 1

Input:

6 120
180 240
720 900
0 60
540 600
330 360
540 600

Output:

60

Example 2

Input:

0 1440

Output:

0

Example 3

Input:

1 1
0 1440

Output:

-1

Example notes

Example 1: everyone is free during [60,180), whose length is exactly 120; before that, someone is busy during [0,60), so the earliest start time is 60.

Editorial

Loading solution…