PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BS0000328. Giãn cách trên đồng cỏ (Social Distancing)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Giãn cách trên đồng cỏ (Social Distancing)

Social Distancing

Source: USACO

Version: Phuoc Hung OJ Extended

Problem Statement

Farmer John wants his nn cows well separated to reduce disease transmission. Model the farm as a line with grass on mm closed intervals [li,ri][l_i,r_i] that share no points, including endpoints.

Each cow must stand at an integer coordinate on grass; interval endpoints are allowed. All cows must occupy distinct positions, and enough such positions are guaranteed.

For an arrangement, let DD be the smallest distance between any pair of cows. Determine the largest possible DD.

Input

The first line contains two integers n,mn,m, the number of cows and grass intervals.

Each of the following mm lines contains two integers li,ril_i,r_i, the endpoints of interval ii. The intervals need not be listed in coordinate order.

Output

Print one positive integer: the largest possible DD.

Subtasks

  • Subtask 1 (20 points): 2≤n≤82\le n\le8; 1≤m≤81\le m\le8; 0≤li≤ri≤160\le l_i\le r_i\le16; [li,ri]∩[lj,rj]=∅ (i≠j)[l_i,r_i]\cap[l_j,r_j]=\varnothing\ (i\ne j); ∑i=1m(ri−li+1)≥n\sum_{i=1}^{m}(r_i-l_i+1)\ge n.
  • Subtask 2 (30 points): 2≤n≤1052\le n\le10^5; 1≤m≤1051\le m\le10^5; 0≤li≤ri≤1050\le l_i\le r_i\le10^5; [li,ri]∩[lj,rj]=∅ (i≠j)[l_i,r_i]\cap[l_j,r_j]=\varnothing\ (i\ne j); ∑i=1m(ri−li+1)≥n\sum_{i=1}^{m}(r_i-l_i+1)\ge n.
  • Subtask 3 (50 points): 2≤n≤1052\le n\le10^5; 1≤m≤1051\le m\le10^5; 0≤li≤ri≤10180\le l_i\le r_i\le10^{18}; [li,ri]∩[lj,rj]=∅ (i≠j)[l_i,r_i]\cap[l_j,r_j]=\varnothing\ (i\ne j); ∑i=1m(ri−li+1)≥n\sum_{i=1}^{m}(r_i-l_i+1)\ge n.

Examples

Input 1

5 3
0 2
4 7
9 9

Output 1

2

Explanation

Place cows at 0, 2, 4, 6 and 9, giving consecutive gaps 2, 2, 2 and 3. A minimum gap of 3 would require a span of at least 4⋅3=124\cdot3=12, exceeding the available span of 9.