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;

template <typename T> void chmin(T&x,const T &y)
{
	if(x>y)x=y;
}
template <typename T> void chmax(T &x,const T &y)
{
	if(x<y)x=y;
}
typedef long long s64;
typedef unsigned long long u64;
typedef unsigned int u32;
typedef pair<int,int> pii;
#define rep(i,l,r) for(int i=l;i<=r;++i)
#define per(i,r,l) for(int i=r;i>=l;--i)
#define rep0(i,l,r) for(int i=l;i<r;++i)
#define gc (c=getchar())
int read()
{
	char c;
	while(gc<'-');
	if(c=='-')
	{
		int x=gc-'0';
		while(gc>='0')x=x*10+c-'0';
		return -x;
	}
	int x=c-'0';
	while(gc>='0')x=x*10+c-'0';
	return x;
}
#undef gc

const int N=1e5+5,D=1e3+5;
int cnt[D][D],s[D][D];
int d;

void add(int x1,int x2,int y1,int y2,int w)
{
	chmax(x1,0);chmin(x2,d-1);
	chmax(y1,0);chmin(y2,d-1);
	if(x1>x2||y1>y2)return ;
	s[x2][y2]+=w;
	if(x1)s[x1-1][y2]-=w;
	if(y1)s[x2][y1-1]-=w;
	if(x1&&y1)s[x1-1][y1-1]+=w;
}

int main()
{
#ifdef kcz
	freopen("1.in","r",stdin);//freopen("1.out","w",stdout);
#endif
	int n=read();d=read();
	rep(i,1,n)
	{
		int x=read(),y=read();
		++cnt[x%d][y%d];
	}
	int l=-1,r=1e9;
	while(l+1!=r)
	{
		memset(s,0,sizeof(s));
		int mid=(l+r)/2;
		bool OK=0;
		s64 base=mid/d;int mo=mid%d;
		rep(x,0,d-1)
		rep(y,0,d-1)
		if(base*base>=cnt[x][y])add(0,d-1,0,d-1,1); else
		if(base*(base+1)>=cnt[x][y])
		{
			add(0,d-1,0,d-1,1);
			rep(i,0,1)
			rep(j,0,1)add(mo-x+1+d*i,d-x-1+d*i,mo-y+1+d*j,d-y-1+d*j,-1);
		}
		else
		if((base+1)*(base+1)>=cnt[x][y])
		{	
			rep(i,0,1)
			rep(j,0,1)add(-x+d*i,mo-x+d*i,-y+d*j,mo-y+d*j,1);
		}
		per(x,d-1,0)
		per(y,d-1,0)s[x][y]+=s[x][y+1];
		per(x,d-1,0)
		per(y,d-1,0)s[x][y]+=s[x+1][y];
		per(x,d-1,0)
		per(y,d-1,0)OK|=s[x][y]==d*d;
		if(OK)r=mid;
		else l=mid;
	}
	cout<<r;	
}