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

#BS0000309. Nhật ký độ cao (Tourist's Notes)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Nhật ký độ cao (Tourist's Notes)

Tourist's Notes

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

A hike lasts n days with integer heights hih_i satisfying ∣hi−hi+1∣≤1|h_i-h_{i+1}|\le1. The remaining m notes (dj,Hj)(d_j,H_j) are in strictly increasing day order and prescribe hdj=Hjh_{d_j}=H_j. Find the greatest possible peak or report inconsistent notes.

Input

First line: n,m. Next m lines contain dj,Hjd_j,H_j.

Output

Print the greatest possible height, or IMPOSSIBLE for inconsistent notes.

Subtasks

  • Subtask 1 (20 points): 1≤n≤401\le n\le40; 1≤m≤min⁡(n,40)1\le m\le\min(n,40); 1≤dj≤n1\le d_j\le n; 0≤Hj≤300\le H_j\le30; d1<⋯<dmd_1<\cdots<d_m.
  • Subtask 2 (30 points): 1≤n≤1081\le n\le10^8; 1≤m≤min⁡(n,105)1\le m\le\min(n,10^5); 1≤dj≤n1\le d_j\le n; 0≤Hj≤1080\le H_j\le10^8; d1<⋯<dmd_1<\cdots<d_m; m=1m=1.
  • Subtask 3 (50 points): 1≤n≤1081\le n\le10^8; 1≤m≤min⁡(n,105)1\le m\le\min(n,10^5); 1≤dj≤n1\le d_j\le n; 0≤Hj≤1080\le H_j\le10^8; d1<⋯<dmd_1<\cdots<d_m.

Examples

Input 1

8 2
2 0
7 0

Output 1

2

Explanation

Five steps separate the two zero heights. A feasible sequence is (1,0,1,2,2,1,0,1), with peak 2.

Input 2

8 3
2 0
7 0
8 3

Output 2

IMPOSSIBLE

Explanation

Days 7 and 8 differ by 3, exceeding 1.