Problem:
Loading

Aizu Ocean Transport Company (AOTC) accepted a new shipping order. The pieces of the cargo included in this order all have the same square footprint (2m x 2m). The cargo room has a rectangular storage space of 4m width with various longitudinal extents. Each pieces of cargo must be placed in alignment with partition boundaries which form a 1m x 1m square grid. A straddling arrangement with a neighboring partition (for example, protrusion by 50 cm) is not allowed. Neither an angled layout nor stacked placement are allowed. Note also that there may be some partitions on which any cargo loading is prohibited.

AOTC wishes to use the currently available ship for this consignment and needs to know how many pieces of cargo it can accommodate.

Make a program to report the maximum cargo capacity of the cargo space given the following information: the depth (m) of the cargo space and the loading-inhibited partitions.

Input

The input is given in the following format.

H N
x_1 y_1
x_2 y_2
:
x_N y_N

The first line provides the longitudinal depth H (2 ≤ H ≤ 104) of the cargo space in meters and the number of load-inhibited partitions N (0 ≤ N ≤ 4 × 104). Each of the subsequent N lines defines the position of the i-th load-inhibited partition x_i (0 ≤ x_i ≤ 3) and y_i (0 ≤ y_i ≤ H-1) in integers, where x = 0 and y = 0 indicate the bottom-left corner partition. A load-inhibited partition appears only once in the list.

Output

Output the maximum number of cargo pieces loaded into the cargo space.

Sample Input 1

5 3
0 1
1 2
3 3

Sample Output 1

2

Input example 1 corresponds to the cargo layout shown in the left-most figure.

Sample Input 2

6 4
0 2
1 3
3 4
0 5

Sample Output 2

4

Solution in C++14 (judge verdict: Accepted)

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#define ll long long
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define ROF(i,a,b) for(int i=b-1;i>=a;i--)
using namespace std;
//
const int inf=1e9+7;
const int mod=1e9+7;
//
struct poi{
	int X;int Y;int Z;
	bool operator<(const poi&R)const{
		return X==R.X ? Y==R.Y ? Z<R.Z : Y<R.Y : X<R.X;
	}
};
//
ll GCD(ll a,ll b){
	return (b==0)?(a):(GCD(b,a%b));
}
ll LCM(ll a,ll b){
	return a/GCD(a,b)*b;
}
//
int H,N;
int ok[20000][4];
int DP[20000][5];
int main(){
	cin>>H>>N;
	FOR(i,0,H){
		FOR(j,0,4){
			ok[i][j]=1;
		}
	}
	FOR(i,0,N){
		int X,Y;
		cin>>X>>Y;
		ok[Y][X]=0;
	}
	/*
	cout<<endl;
	FOR(i,0,H){
		FOR(j,0,4){
			cout<<ok[i][j]<<" ";
		}
		cout<<endl;
	}
	cout<<endl;
	*/
	//
	if(ok[0][0]&&ok[0][1]&&ok[1][0]&&ok[1][1]){
		DP[0][1]=1;
	}
	if(ok[0][1]&&ok[0][2]&&ok[1][1]&&ok[1][2]){
		DP[0][2]=1;
	}
	if(ok[0][2]&&ok[0][3]&&ok[1][2]&&ok[1][3]){
		DP[0][3]=1;
	}
	if(ok[0][0]&&ok[0][1]&&ok[1][0]&&ok[1][1]&&ok[0][2]&&ok[0][3]&&ok[1][2]&&ok[1][3]){
		DP[0][4]=2;
	}
	//
	if(ok[1][0]&&ok[1][1]&&ok[2][0]&&ok[2][1]){
		DP[1][1]=DP[0][3]+1;
	}
	if(ok[1][1]&&ok[1][2]&&ok[2][1]&&ok[2][2]){
		DP[1][2]=1;
	}
	if(ok[1][2]&&ok[1][3]&&ok[2][2]&&ok[2][3]){
		DP[1][3]=DP[0][1]+1;
	}
	if(ok[1][0]&&ok[1][1]&&ok[2][0]&&ok[2][1]&&ok[1][2]&&ok[1][3]&&ok[2][2]&&ok[2][3]){
		DP[1][4]=2;
	}
	//
	FOR(i,2,H-1){
		DP[i][0]=max({DP[i-2][0],DP[i-2][1],DP[i-2][2],DP[i-2][3],DP[i-2][4],DP[i-1][0],DP[i-1][1],DP[i-1][2],DP[i-1][3],DP[i-1][4]});
		DP[i][1]=max({DP[i-2][0],DP[i-2][1],DP[i-2][2],DP[i-2][3],DP[i-2][4],DP[i-1][3],DP[i-1][0]});
		DP[i][2]=max({DP[i-2][0],DP[i-2][1],DP[i-2][2],DP[i-2][3],DP[i-2][4],DP[i-1][0]});
		DP[i][3]=max({DP[i-2][0],DP[i-2][1],DP[i-2][2],DP[i-2][3],DP[i-2][4],DP[i-1][1],DP[i-1][0]});
		DP[i][4]=max({DP[i-2][0],DP[i-2][1],DP[i-2][2],DP[i-2][3],DP[i-2][4],DP[i-1][0]});
		if(ok[i][0]&&ok[i][1]&&ok[i+1][0]&&ok[i+1][1]){
			DP[i][1]++;
		}else{
			DP[i][1]=max({DP[i][1],DP[i][0]});
		}
		if(ok[i][1]&&ok[i][2]&&ok[i+1][1]&&ok[i+1][2]){
			DP[i][2]++;
		}else{
			DP[i][2]=max({DP[i][2],DP[i][0]});
		}
		if(ok[i][2]&&ok[i][3]&&ok[i+1][2]&&ok[i+1][3]){
			DP[i][3]++;
		}else{
			DP[i][3]=max({DP[i][3],DP[i][0]});
		}
		if(ok[i][0]&&ok[i][1]&&ok[i+1][0]&&ok[i+1][1]&&ok[i][2]&&ok[i][3]&&ok[i+1][2]&&ok[i+1][3]){
			DP[i][4]+=2;
		}else{
			DP[i][4]=max({DP[i][4],DP[i][0]});
		}
	}
	int ans=0;
	/*
	FOR(i,0,H){
		FOR(j,0,5){
			cout<<DP[i][j]<<" ";
		}
		cout<<endl;
	}
	cout<<endl;
	*/
	FOR(i,0,5){
		ans=max(ans,DP[H-2][i]);
	}
	cout<<ans<<endl;
	return 0;
}