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;

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for(int64 i = 0;i < (n);i++)
#define FOR(i, a, b) for(int64 i = (a);i < (b);i++)
#define all(x) x.begin(),x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-10;

template<typename A, typename B>inline void chmin(A &a, B b){if(a > b) a = b;}
template<typename A, typename B>inline void chmax(A &a, B b){if(a < b) a = b;}

const int64 mod = 1e9+7;

int64 minsq(int64 x){ // x個を正方形内に入れる最小の正方形9 -> 3, 10 -> 4
	int64 a = sqrt(x);
	if(a*a >= x) return a;
	return a+1;
}

int main(void){
	int32 N, D;
	cin >> N >> D;
	vector<int64> x(N), y(N);
	REP(i, N) cin >> x[i] >> y[i];
	vector<vector<int32>> cnt(3030, vector<int32>(3030, 0));
	vector<vector<int32>> ok1(3030, vector<int32>(3030, 0));
	vector<vector<int32>> ok2(3030, vector<int32>(3030, 0));
	int64 d = 0;
	REP(i, N){
		cnt[x[i]%D][y[i]%D]++;
		chmax(d, cnt[x[i]%D][y[i]%D]);
	}
	using T = tuple<int64, int64, int64>;
	vector<T> v;
	sort(all(v), greater<T>());
	d = minsq(d);
	REP(i, D){
		REP(j, D){
			REP(k, 3){
				REP(l, 3){
					if(cnt[i][j] > (d-1)*(d-1)) ok1[i+D*k][j+D*l]++;
					if(cnt[i][j] > d*(d-1)) ok2[i+D*k][j+D*l]++;
				}
			}
		}
	}
	REP(i, 3*D){
		REP(j, 3*D){
			ok1[i][j+1] += ok1[i][j];
			ok2[i][j+1] += ok2[i][j];
		}
	}
	REP(j, 3*D){
		REP(i, 3*D){
			ok1[i+1][j] += ok1[i][j];
			ok2[i+1][j] += ok2[i][j];
		}
	}
	auto sum1 = [&](int32 x1, int32 y1, int32 x2, int32 y2){
		int32 res = ok1[x2][y2];
		if(x1 > 0) res -= ok1[x1-1][y2];
		if(y1 > 0) res -= ok1[x2][y1-1];
		if(x1 > 0 && y1 > 0) res += ok1[x1-1][y1-1];
		return res;
	};
	auto sum2 = [&](int32 x1, int32 y1, int32 x2, int32 y2){
		int32 res = ok2[x2][y2];
		if(x1 > 0) res -= ok2[x1-1][y2];
		if(y1 > 0) res -= ok2[x2][y1-1];
		if(x1 > 0 && y1 > 0) res += ok2[x1-1][y1-1];
		return res;
	};
	int32 l = -1, r = D+1, m;

	auto ok = [&](int32 b){
		REP(x, D){
			REP(y, D){
				bool ok = 1;
//				cout << x << " " << y << " " << b << endl;
//				cout << sum1(x, y+b+1, x+b, y+D-1) << " " << sum1(x+b+1, y, x+D-1, y+b) << " " << sum2(x+b+1, y+b+1, x+D-1, y+D-1) << endl;
				if(sum1(x+b+1, y+b+1, x+D-1, y+D-1) > 0) ok = 0;
				if(sum2(x+b+1, y, x+D-1, y+b) > 0) ok = 0;
				if(sum2(x, y+b+1, x+b, y+D-1) > 0) ok = 0;
				if(ok) return 1;
			}
		}
		return 0;
	};

	while(r-l > 1){
		m = (l+r)/2;
		if(ok(m))
			r = m;
		else
			l = m;
	}
	cout << (d-1)*D+r << endl;
}