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<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<string>
#include<stack>
#include<cstdio>
#include<cmath>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int,int> P;
typedef pair<int,P> P1;

#define fr first
#define sc second
#define mp make_pair
#define pb push_back
#define rep(i,x) for(int i=0;i<x;i++)
#define rep1(i,x) for(int i=1;i<=x;i++)
#define rrep(i,x) for(int i=x-1;i>=0;i--)
#define rrep1(i,x) for(int i=x;i>0;i--)
#define sor(v) sort(v.begin(),v.end())
#define rev(s) reverse(s.begin(),s.end())
#define lb(vec,a) lower_bound(vec.begin(),vec.end(),a)
#define ub(vec,a) upper_bound(vec.begin(),vec.end(),a)
#define uniq(vec) vec.erase(unique(vec.begin(),vec.end()),vec.end())
#define mp1(a,b,c) P1(a,P(b,c))

const int INF=1000000000;
const int dir_4[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
const int dir_8[8][2]={{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1}};

int main(){
	static int N,D;
	static int x[100010],y[100010];
	scanf("%d%d",&N,&D);
	for(int i = 1 ; i <= N ; i ++){
		scanf("%d%d",&x[i],&y[i]);
	}
	
	static int cnt[1002][1002] = {};
	for(int i = 1 ; i <= N ; i ++){
		cnt[x[i]%D][y[i]%D] ++;
	}
	
	static int A[1002][1002] = {},B[1002][1002] = {};
	for(int i = 0 ; i < D ; i ++){
		for(int j = 0 ; j < D ; j ++){
			while(1){
				if(A[i][j]*B[i][j] >= cnt[i][j])break;
				A[i][j] ++;
				if(A[i][j]*B[i][j] >= cnt[i][j])break;
				B[i][j] ++;
			}
		}
	}
	
	int M = 0, l = 0, r = D-1;
	for(int i = 0 ; i < D ; i ++){
		for(int j = 0 ; j < D ; j ++){
			M = max( M , A[i][j]-1 );
		}
	}
	static int imos[1002][1002] = {};
	while(l < r){
		int m = (l+r)/2;
		for(int i = 0 ; i < D ; i ++){
			for(int j = 0 ; j < D ; j ++){
				imos[i][j] = 0;
			}
		}
		int CNT = 0;
		for(int i = 0 ; i < D ; i ++){
			for(int j = 0 ; j < D ; j ++){
				if(A[i][j] == M+1){
					CNT ++;
					if(B[i][j] == M+1){
						vector<P> X;
						vector<P> Y;
						if(i < m){
							X.push_back(P(i-m+D,D-1));
							X.push_back(P(0,i));
						}
						else X.push_back(P(i-m,i));
						if(j < m){
							Y.push_back(P(j-m+D,D-1));
							Y.push_back(P(0,j));
						}
						else Y.push_back(P(j-m,j));
						for(P p: X)for(P q: Y){
							imos[p.fr][q.fr] ++;
							imos[p.fr][q.sc+1] --;
							imos[p.sc+1][q.fr] --;
							imos[p.sc+1][q.sc+1] ++;
						}
					}
					else {
						vector<P> X;
						vector<P> Y;
						if(i < m){
							X.push_back(P(i+1,i-m+D-1));
						}
						else {
							X.push_back(P(0,i-m-1));
							X.push_back(P(i+1,D-1));
						}
						if(j < m){
							Y.push_back(P(j+1,j-m+D-1));
						}
						else {
							Y.push_back(P(0,j-m-1));
							Y.push_back(P(j+1,D-1));
						}
						for(P p: X)for(P q: Y){
							imos[p.fr][q.fr] --;
							imos[p.fr][q.sc+1] ++;
							imos[p.sc+1][q.fr] ++;
							imos[p.sc+1][q.sc+1] --;
						}
						imos[0][0] ++;
						imos[0][D] --;
						imos[D][0] --;
						imos[D][D] ++;
					}
				}
			}
		}
		for(int i = 0 ; i < D ; i ++){
			for(int j = 0 ; j < D ; j ++){
				imos[i+1][j] += imos[i][j];
			}
		}
		for(int i = 0 ; i < D ; i ++){
			for(int j = 0 ; j < D ; j ++){
				imos[i][j+1] += imos[i][j];
			}
		}
		bool b = false;
		for(int i = 0 ; i < D ; i ++){
			for(int j = 0 ; j < D ; j ++){
				if(imos[i][j] == CNT){
					b = true;
					break;
				}
			}
		}
		if(b)r = m;
		else l = m+1;
	}
	cout << M*D+l << endl;
}