Problem: Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor.

Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them.

In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume).

He may perform the following operation arbitrarily many times:

* Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point.



Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square.

Find the minimum scatteredness after he performs arbitrarily many operations.

Constraints

* 2 \leq N \leq 10^5
* 1 \leq D \leq 1000
* 0 \leq x_i, y_i \leq 10^9
* Given coordinates are pairwise distinct
* All numbers given in input are integers

Input

Input is given from Standard Input in the following format:


N D
x_1 y_1
:
x_N y_N


Output

Print the answer.

Examples

Input

3 1
0 0
1 0
2 0


Output

1


Input

19 2
1 3
2 3
0 1
1 1
2 1
3 1
4 4
5 4
6 4
7 4
8 4
8 3
8 2
8 1
8 0
7 0
6 0
5 0
4 0


Output

4


Input

8 3
0 0
0 3
3 0
3 3
2 2
2 5
5 2
5 5


Output

4

Solution (in CPP):

Solution:
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+2;
const int M=1e3+2;
const int inf=1e9+7;
int cnt[M][M],cnt1[3][M][M],d;
int getsum(int idx,int l,int r,int l2,int r2){
	int ans=cnt1[idx][l2][r2];
	if(l){
		ans-=cnt1[idx][l-1][r2];
	}
	if(r){
		ans-=cnt1[idx][l2][r-1];
	}
	if(l&&r){
		ans+=cnt1[idx][l-1][r-1];
	}
	return ans;
}
int getrec(int idx,int l,int r,int a,int b){	
	if(l+a<d&&r+b<d){
		return getsum(idx,l,r,l+a,r+b);
	}
	else{
		if(l+a<d&&r+b>=d){
			return getsum(idx,l,r,l+a,d-1)+getsum(idx,l,0,l+a,r+b-d);
		}
		else{
			if(l+a>=d&&r+b<d){
				return getsum(idx,l,r,d-1,r+b)+getsum(idx,0,r,l+a-d,r+b);
			}
			else{
				return getsum(idx,l,r,d-1,d-1)+getsum(idx,l,0,d-1,r+b-d)+getsum(idx,0,r,l+a-d,d-1)+getsum(idx,0,0,l+a-d,r+b-d);
			}
		}
	}
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int n,m,i,j,k,l,z,num=0,lef,rig,mid;
	cin>>n>>d;
	for(i=1;i<=n;i++){
		cin>>j>>k;
		cnt[j%d][k%d]++;
	}
	for(i=0;i<d;i++){
		for(j=0;j<d;j++){
			if(!cnt[i][j]){
				continue;
			}
			z=sqrt(cnt[i][j])-1;
			while((z+1)*(z+1)<cnt[i][j]){
				z++;
			}
			num=max(num,z);
		}
	}
//	cout<<num<<endl;
	lef=num*d,mid=rig=num*d+d-1;
	while(lef<rig){
		mid=(lef+rig)/2;
		for(i=0;i<3;i++){
			if(i==0){
				l=(num+1)*(num+1);
			}
			if(i==1){
				l=num*(num+1);
			}
			if(i==2){
				l=num*num;
			}
			for(j=0;j<d;j++){
				for(k=0;k<d;k++){
					cnt1[i][j][k]=(cnt[j][k]>l);
					if(j){
						cnt1[i][j][k]+=cnt1[i][j-1][k];
					}
					if(k){
						cnt1[i][j][k]+=cnt1[i][j][k-1];
					}
					if(j&&k){
						cnt1[i][j][k]-=cnt1[i][j-1][k-1];
					}
				}
			}
		}
		bool cac=false;
		l=mid%d;
		for(i=0;i<d;i++){
			for(j=0;j<d;j++){
				int cost1=getrec(0,i,j,l,l),cost2=0,cost3=0;
				if(l<d-1){
					cost2=max(getrec(1,(i+l+1)%d,j,d-1-l-1,l),getrec(1,i,(j+l+1)%d,l,d-1-l-1));
//					if(i==2&&j==2){
//						cout<<(i+l+1)%d<<' '<<j<<' '<<d-1-l-1<<' '<<l<<' '<<getrec(1,(i+l+1)%d,j,d-1-l-1,l)<<endl;
//						cout<<i<<' '<<(j+l+1)%d<<' '<<l<<' '<<d-1-l-1<<' '<<getrec(1,i,(j+l+1)%d,l,d-1-l-1)<<endl;
//					}
					cost3=getrec(2,(i+l+1)%d,(j+l+1)%d,d-l-1-1,d-l-1-1);
				}
//				if(i==2&&j==2){
//					cout<<cost1<<' '<<cost2<<' '<<cost3<<" CAC"<<endl;
//				}
				if(!cost1&&!cost2&&!cost3){
					cac=true;
					break;
				}
			}
			if(cac){
				break;
			}
		}
		if(cac){
			rig=mid;
		}
		else{
			lef=mid+1;
		}
	}
	cout<<rig;
}