# Codeforces Round #300-Tourist's Notes(贪心)

Tourist's Notes
Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer hi. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |hi - hi + 1| ≤ 1 holds.

At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |hi - hi + 1| ≤ 1.

Input

The first line contains two space-separated numbers, n and m (1 ≤ n ≤ 1081 ≤ m ≤ 105) — the number of days of the hike and the number of notes left in the journal.

Next m lines contain two space-separated integers di and hdi (1 ≤ di ≤ n0 ≤ hdi ≤ 108) — the number of the day when the i-th note was made and height on the di-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: di < di + 1.

Output

If the notes aren't contradictory, print a single integer — the maximum possible height value throughout the whole route.

If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes).

Sample Input

Input
8 2
2 0
7 0

Output
2

Input
8 3
2 0
7 0
8 3

Output
IMPOSSIBLE


Hint

For the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1).

In the second sample the inequality between h7 and h8 does not hold, thus the information is inconsistent.

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <map>
using namespace std;
typedef long long LL;
const int inf=0x3f3f3f3f;
const double pi= acos(-1.0);
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
const int maxn=1e5+10;
struct node
{
int x,y;
}q[maxn],p;

int main()
{
int n,m,i;
int two;
int three;
int flag;
while(~scanf("%d %d",&n,&m)){
flag=0;
scanf("%d %d",&q.x,&q.y);
int res=q.x+q.y-1;
p.x=q.x;
p.y=q.y;
for(i=1;i<m;i++){
scanf("%d %d",&q[i].x,&q[i].y);
if(fabs(q[i].y-p.y)>q[i].x-p.x){
flag=1;
}
two=(q[i].x-p.x+q[i].y+p.y)/2;
res=max(res,two);
p.x=q[i].x;
p.y=q[i].y;
}
three=p.y+(n-p.x);
res=max(res,three);
if(flag)
printf("IMPOSSIBLE\n");
else
printf("%d\n",res);

}
return 0;
}


07-15 201                                                                          05-01 2267
04-28 1295
02-21 271
12-03 95
04-29 502
07-16 25
04-03 32
04-28 44
05-11 325
07-25 467
12-03 194
05-25 473
05-18 95
09-26 1433
08-09 1万+
02-01 1万+
10-26 37
12-05 1008 点击重新获取   扫码支付  余额充值