cswork / OAOpen practice workspace

AmazonOA #76

Get Min Time (Earliest Time Usage ≥ Max)

Estimated MediumJudge-ready

Topics (AI-labelled)Binary SearchPrefix & Ranges

Each user contributes 1 usage at every integer second from start to end. Find the integer second at which the cumulative usage first reaches max_usage. CSWork standard I/O convention: output −1 if the threshold is never reached.

Input

The first line contains n max_usage (1≤n≤100000, 1≤max_usage≤10¹⁴), followed by n lines of start end (1≤start≤end≤10⁹).

Output

Output a single integer answer.

Example 1

Input:

2 3
1 3
2 2

Output:

2

Example 2

Input:

1 1
5 5

Output:

5

Example 3

Input:

1 2
1 1

Output:

-1

Example notes

Example 1: the cumulative total is 1 at second 1; at second 2 both users contribute 1 each, for a cumulative 3, so output 2. Example 2: starting and ending in the same second still contributes 1, and the threshold is first reached at second 5. Example 3: the total usage is only 1 and never reaches 2, so output −1 per this site's convention.

Editorial

Loading solution…