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 <vector>
#include <utility>
using namespace std;
typedef long long ll;

const int D = 1000;
int arr[2][2*D + 1][2*D + 1];

int get(int j, int x1, int y1, int x2, int y2) {
	return arr[j][x2+1][y2+1] - arr[j][x1][y2+1] - arr[j][x2+1][y1] + arr[j][x1][y1];
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);

	int n, d;
	cin >> n >> d;
	// Divide the plane into D^2 partitions
	// Say d = 3,
	// a b c a b c a b c
	// d e f d e f d e f
	// g h i g h i g h i
	// a b c a b c a b c
	// d e f d e f d e f
	// g h i g h i g h i
	// a b c a b c a b c
	// d e f d e f d e f
	// g h i g h i g h i
	// We can move points within the same partition arbitrarily. Points cannot switch partitions.
	vector<int> cou(d*d, 0);
	for (int i = 0; i < n; ++i) {
		int x, y;
		cin >> x >> y;
		x %= d;
		y %= d;
		++cou[x + y * d];
	}
	
	// All partitions want to be packed into a square, or a rectangle with one side d shorter than the other
	// Find points with longest edge size, and put them into bads. Points not in bads are not a concern
	int mx = -1;
	vector<pair<int, int>> bads;
	for (int i = 0; i < d*d; ++i) {
		if (! cou[i]) continue;

		int w = 1;
		while(w*w < cou[i]) {
			++w;
		}
		int h = (cou[i] + (w-1)) / w;

		if (w > mx) {
			mx = w;
			bads.clear();
		}
		if (w == mx) bads.push_back({i, (bool)(w > h)});
	}

	// Fill the two-dimensional sum-array so we can count how many points are in a rectangle fast.
	int sq_cou = 0;
	for (auto pr : bads) {
		int i = pr.first;
		int j = pr.second;

		int x = i % d;
		int y = i / d;
		++x; ++y;

		if (j == 0) ++sq_cou;

		++arr[j][x][y];
		++arr[j][x+d][y];
		++arr[j][x][y+d];
		++arr[j][x+d][y+d];
	}
	
	for (int j = 0; j < 2; ++j) {
		for (int x = 0; x <= 2*d; ++x) {
			for (int y = 0; y <= 2*d; ++y) {
				if (x == 0) {
					arr[j][x][y] += arr[j][x][y-1];
				} else if (y == 0) {
					arr[j][x][y] += arr[j][x-1][y];
				} else {
					arr[j][x][y] += arr[j][x-1][y] + arr[j][x][y-1] - arr[j][x-1][y-1];
				}
			}
		}
	}

	// Binary search answer
	int low = 0;
	int high = d-1;
	while(low != high) {
		int mid = (low + high) >> 1;
		bool works = false;
		for (int x = 0; x < d; ++x) {
			for (int y = 0; y < d; ++y) {
				// Rectangles must not be in [mid+1, d-1] x [mid+1, d-1]
				if (get(1, x + mid + 1, y + mid + 1, x + d-1, y + d-1) != 0) continue;
				// Squares must be in [0, mid] x [0, mid]
				if (get(0, x, y, x + mid, y + mid) != sq_cou) continue;
				works = true;
			}
		}
		if (works) high = mid;
		else low = mid + 1;
	}
	
	// Calculate answer
	if (mx == -1) {
		cout << "0\n";
	} else {
		int ans = (mx - 1) * d + low;
		cout << ans << '\n';
	}
}