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>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define mod 1000000007
#define fi first
#define sc second
#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
int n,d;
int cnt[1005][1005];
vector<int>forbid[1005];
bool a[1005],b[1005];
int par[1005],ran[1005];
void init(){ for(int i=0;i<1005;i++) par[i] = i, ran[i]  = 0; }
int find(int x){ if(x == par[x]) return x; else return par[x] = find(par[x]); }
void unite(int x,int y){
	x = find(x); y = find(y); if(x==y) return;
	if(ran[x] < ran[y]) par[x] = y;
	else{
		par[y] = x;
		if(ran[x] == ran[y]) ran[x]++;
	}
}
bool same(int x,int y){ return find(x)==find(y); }
int main(){
	scanf("%d%d",&n,&d);
	rep(i,n){
		int x,y; scanf("%d%d",&x,&y);
		cnt[x%d][y%d]++;
	}
	int mx = 0;
	rep(i,d)rep(j,d) mx=max(mx,cnt[i][j]);
	
	ll lb = 0,ub = 1000;
	//(lb,ub]
	while(ub-lb>1){
		ll mid = (lb+ub)/2;
		if(mid*mid >= mx) ub = mid;
		else lb = mid;
	}
	//1辺ub*ubあれば十分
	if(d == 1){
		cout << ub-1 << endl;
		return 0;
	}
	//どこまで減らせるか
	rep(i,d)rep(j,d){
		if(ub*(ub-1) < cnt[i][j]){
			a[i] = 1;
			b[j] = 1;
		}
		else if((ub-1)*(ub-1) < cnt[i][j]){
			forbid[i].pb(j);
		}
	}
	int mxx = 0;
	rep(beg,d){
		//いくついけるか、[lb,ub)
		int lb = 0,ub = d;
		while(ub-lb>1){
			int mid = (lb+ub)/2;
			bool ok[1005]={};
			for(int j=0;j<d;j++) ok[j] = b[j];
			int cnt = mid;
			int nw = beg;
			int fail = 0;
			while(cnt--){
				if(a[nw]){
					fail = 1; break;
				}
				rep(h,forbid[nw].size()) ok[forbid[nw][h]] = 1;
				nw = (nw+1)%d;
			}
			if(fail){
				ub = mid; continue;
			}
			init();
			for(int i=0;i<d;i++){
				int j = (i+1)%d;
				if(!ok[i] && !ok[j]) unite(i,j);
			}
			int sz[1005]={};
			for(int i=0;i<d;i++){
				if(ok[i]) continue;
				sz[find(i)]++;
			}
			int MX = 0;
			rep(i,d) MX = max(MX,sz[i]);
			if(MX >= mid){
				lb = mid;
			}
			else{
				ub = mid;
			}
		}
		mxx = max(mxx,lb);
	}
	cout << ub*d-1-mxx << endl;
}