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 <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <sstream>
#include <complex>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
using namespace std;
 
#define mod 1000000007
#define FOR(x,to) for(int x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
#define long long long
inline int rei(){int x;cin>>x;return x;}
inline long rel(){long x;cin>>x;return x;}
inline string res(){string x;cin>>x;return x;}
//------------------------------------------------------- 
int Num[1000000];
int D;
int Map2[1000000];
int Map3[1000000];
int C2;
int C3;
int Mapsum2(int l,int r,int d,int u){
	int c = Map2[r*D+u];
	if(l != 0){
		c -= Map2[(l-1)*D+u];
	}
	if(d != 0){
		c -= Map2[r*D+d-1];
	}
	if(l != 0 && d != 0){
		c += Map2[(l-1)*D+d-1];
	}
	return c;
}
int Mapsum3(int l,int r,int d,int u){
	int c = Map3[r*D+u];
	if(l != 0){
		c -= Map3[(l-1)*D+u];
	}
	if(d != 0){
		c -= Map3[r*D+d-1];
	}
	if(l != 0 && d != 0){
		c += Map3[(l-1)*D+d-1];
	}
	return c;
}
int MapSum2(int l,int r,int d,int u){
	if(u >= D){
		if(r >= D){
			return Mapsum2(0,r-D,0,u-D) + Mapsum2(l,D-1,d,D-1) + Mapsum2(0,r-D,d,D-1) + Mapsum2(l,D-1,0,u-D);
		}
		else{
			return Mapsum2(l,r,0,u-D) + Mapsum2(l,r,d,D-1);
		}
	}
	else{
		if(r >= D){
			return Mapsum2(0,r-D,d,u) + Mapsum2(l,D-1,d,u);
		}
		else{
			return Mapsum2(l,r,d,u);
		}
	}
}
int MapSum3(int l,int r,int d,int u){
	if(u >= D){
		if(r >= D){
			return Mapsum3(0,r-D,0,u-D) + Mapsum3(l,D-1,d,D-1) + Mapsum3(0,r-D,d,D-1) + Mapsum3(l,D-1,0,u-D);
		}
		else{
			return Mapsum3(l,r,0,u-D) + Mapsum3(l,r,d,D-1);
		}
	}
	else{
		if(r >= D){
			return Mapsum3(0,r-D,d,u) + Mapsum3(l,D-1,d,u);
		}
		else{
			return Mapsum3(l,r,d,u);
		}
	}
}
bool check(int bc){
	for(int i=0;i<D;i++){
		for(int j=0;j<D;j++){
			int c2 = MapSum2(0,D-1,j,j+bc-1) + MapSum2(i,i+bc-1,0,D-1) - MapSum2(i,i+bc-1,j,j+bc-1);
			int c3 = MapSum3(i,i+bc-1,j,j+bc-1);
			if(c2 == C2 && c3 == C3){
				return true;
			}
		}
	}
	return false;
}
void Calc(){
	int N = rei();
	D = rei();
	for(int i=0;i<N;i++){
		int x = rei();
		int y = rei();
		Num[(x % D) * D + y % D]++;
	}
	int maxN = 0;
	for(int i=0;i<D*D;i++){
		maxN = max(maxN,Num[i]);
	}
	int oneline;
	for(oneline=0;;oneline++){
		if(oneline*oneline >= maxN){
			break;
		}
	}
	long c1 = (oneline-1)*(oneline-1);
	long c2 = (oneline-1)*oneline;
	long c3 = oneline*oneline;
	for(int i=0;i<D*D;i++){
		if(Num[i] > c2){
			Map3[i]++;
			C3++;
		}
		else if(Num[i] > c1){
			Map2[i]++;
			C2++;
		}
	}
	for(int i=0;i<D*D;i++){
		if(i/D != 0){
			Map2[i] += Map2[i-D];
			Map3[i] += Map3[i-D];
		}
		if(i%D != 0){
			Map2[i] += Map2[i-1];
			Map3[i] += Map3[i-1];
		}
		if((i%D != 0) && (i/D != 0)){
			Map2[i] -= Map2[i-D-1];
			Map3[i] -= Map3[i-D-1];
		}
	}
	int bf = 1;
	int bl = D;
	while(bf != bl){
		long bc = (bf+bl)/2;
		if(check(bc)){
			bl = bc;
		}
		else{
			bf = bc+1;
		}
	}
	cout << bf-1 + (oneline-1)*D << endl;
}
int main(int argc,char** argv){
	ios::sync_with_stdio(false), cin.tie(0);
	cout.tie(0); Calc(); return 0;
}