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 <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
#include <random>
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;

int main()
{
	int n, d;
	cin>>n>>d;
	int ct[1000][1000]={};
	for(int i=0; i<n; i++){
		int x, y;
		cin>>x>>y;
		ct[x%d][y%d]++;
	}
	ll m1=1, m2=1e9;
	while(m1!=m2){
		int m=(m1+m2)/2;
		bool ok=0;
		ll c[1000];
		ll cm=m/d+1;
		int l=0;
		for(int k=0; k<d; k++){
			if(k>m) c[k]=0;
			else c[k]=(m-k)/d+1;
			cm=min(c[k], cm);
			if(c[k]==m/d+1) l=k;
		}
		if(l==d-1){
			ok=1;
			for(int i=0; i<d; i++){
				for(int j=0; j<d; j++){
					if(ct[i][j]>cm*cm){
						ok=0;
						break;
					}
				}
				if(!ok) break;
			}
			if(ok) m2=m;
			else m1=m+1;
			continue;
		}
		bool nuee=0;
		int ctx[1001]={}, cty[1001]={};
		int ct2[1001][1001]={};
		for(int i=0; i<d; i++){
			for(int j=0; j<d; j++){
				if(ct[i][j]>(cm+1)*(cm+1)){
					nuee=1;
					break;
				}else if(ct[i][j]>(cm+1)*cm){
					ctx[i+1]++;
					cty[j+1]++;
				}else if(ct[i][j]>cm*cm){
					ct2[i+1][j+1]++;
				}
			}
			if(nuee) break;
		}
		if(nuee){
			m1=m+1;
			continue;
		}
		for(int i=1; i<=d; i++){
			ctx[i]+=ctx[i-1];
			cty[i]+=cty[i-1];
		}
		for(int i=1; i<=d; i++){
			for(int j=1; j<=d; j++){
				ct2[i][j]=(ct2[i-1][j]+ct2[i][j-1]-ct2[i-1][j-1]+ct2[i][j]);
			}
		}
		for(int i=0; i<d; i++){
			if(i+d-l-2<=d-1){
				if(ctx[i]!=ctx[i+d-l-1]) continue;
			}else{
				if(ctx[i]!=ctx[d] || ctx[(i+d-l-2)%d+1]!=0) continue;
			}
			for(int j=0; j<d; j++){
				if(j+d-l-2<=d-1){
					if(cty[j]!=cty[j+d-l-1]) continue;
				}else{
					if(cty[j]!=cty[d] || cty[(j+d-l-2)%d+1]!=0) continue;
				}
				if(j+d-l-2<=d-1 && i+d-l-2<=d-1){
					if(ct2[i+d-l-1][j+d-l-1]-ct2[i+d-l-1][j]-ct2[i][j+d-l-1]+ct2[i][j]>0) continue;
				}else if(i+d-l-2<=d-1){
					if(ct2[i+d-l-1][d]-ct2[i+d-l-1][j]-ct2[i][d]+ct2[i][j]+ct2[i+d-l-1][(j+d-l-2)%d+1]-ct2[i][(j+d-l-2)%d+1]>0) continue;
				}else if(j+d-l-2<=d-1){
					if(ct2[d][j+d-l-1]-ct2[d][j]-ct2[i][j+d-l-1]+ct2[i][j]+ct2[(i+d-l-2)%d+1][j+d-l-1]-ct2[(i+d-l-2)%d+1][j]>0) continue;
				}else{
					if(ct2[(i+d-l-2)%d+1][(j+d-l-2)%d+1]+(ct2[d][(j+d-l-2)%d+1]-ct2[i][(j+d-l-2)%d+1])+(ct2[(i+d-l-2)%d+1][d]-ct2[(i+d-l-2)%d+1][j])+ct2[d][d]-ct2[i][d]-ct2[d][j]+ct2[i][j]>0) continue;
				}
				ok=1;
				break;
			}
			if(ok) break;
		}
		if(ok) m2=m;
		else m1=m+1;
	}
	cout<<m1<<endl;
	return 0;
}