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 rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define all(c) c.begin(),c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << (x) << endl
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
using namespace std;
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){
	return o<<"("<<p.fs<<","<<p.sc<<")";
}
template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){
	o<<"{";
	for(const T& v:vc) o<<v<<",";
	o<<"}";
	return o;
}
using ll = long long;
template<class T> using V = vector<T>;
template<class T> using VV = vector<vector<T>>;

int N,D;
int cnt[1000][1000];

int s0[2010][2010];
int s2[2010][2010];

int Count(int a,int b,int c,int d,int t){
	if(t==0){
		return s0[c][d] - s0[a][d] - s0[c][b] + s0[a][b];
	}else{
		return s2[c][d] - s2[a][d] - s2[c][b] + s2[a][b];
	}
}

int solve(VV<int>& A){
	int N = A.size();
	// puts("----------A---------");
	// rep(i,N){
	// 	rep(j,N) cout<<A[i][j];
	// 	puts("");
	// }
	// puts("-----------------------");

	int num0 = 0, num2 = 0;
	rep(i,D) rep(j,D){
		if(A[i][j]==0) num0++;
		if(A[i][j]==2) num2++;
	}

	int V = D+D;
	rep(i,V) rep(j,V){
		s0[i+1][j+1] = (A[i%D][j%D]==0);
		s2[i+1][j+1] = (A[i%D][j%D]==2);
	}
	rep(i,V+1) rep(j,V) s0[i][j+1] += s0[i][j], s2[i][j+1] += s2[i][j];
	rep(j,V+1) rep(i,V) s0[i+1][j] += s0[i][j], s2[i+1][j] += s2[i][j];

	int ub = D+1, lb = -1;
	while(ub-lb>1){
		int L = (ub+lb)/2;
		bool can = false;

		rep(i,D) rep(j,D){
			if(Count(i,j,i+L,j+L,2) == num2 
			&& Count(i+L,j+L,i+N,j+N,0) == (N-L)*(N-L)){
				can = true;
			}
		}

		if(can) ub = L;
		else lb = L;
	}
	return ub;
}

int main(){
	cin>>N>>D;
	rep(i,N){
		int x,y;
		cin>>x>>y;
		cnt[x%D][y%D]++;
	}
	int cntmax = 0;
	rep(i,D) rep(j,D) chmax(cntmax,cnt[i][j]);
	int A = 0;
	while(A*A<cntmax) A++;
	VV<int> a(D,V<int>(D,0));
	rep(i,D) rep(j,D){
		if(cnt[i][j]<=(A-1)*(A-1)) a[i][j] = 0;
		else if(cnt[i][j]<=(A-1)*A) a[i][j] = 1;
		else a[i][j] = 2;
	}
//	show(cntmax);
//	show(A);
	cout<<(A-1)*D+solve(a)-1<<endl;
}