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<stdio.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<string.h>

#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...) do {} while (0)
#endif
#include<cassert>

using namespace std;

typedef long long LL;
typedef vector<int> VI;

#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(),i##_end=(c).end();i!=i##_end;++i)

template<class T> inline void amin(T &x, const T &y) { if (y<x) x=y; }
template<class T> inline void amax(T &x, const T &y) { if (x<y) x=y; }
template<class Iter> void rprintf(const char *fmt, Iter begin, Iter end) {
    for (bool sp=0; begin!=end; ++begin) { if (sp) putchar(' '); else sp = true; printf(fmt, *begin); }
    putchar('\n');
}

int N, D;
int C[1011][1011];

int F[1011][1011];
int dp[1011][1011];
int row[1011], col[1011];

bool ok(int mid) {
    LL q = (mid+1)/D;
    int r = (mid+1)%D;
    LL val[3];
    val[0] = q*q;
    val[1] = val[0] + q;
    val[2] = val[1] + q + 1;
    LL p = val[0] * (D-r)*(D-r) + val[1] * (D-r)*r*2 + val[2] * r*r;
    assert(p == (LL)(mid+1)*(mid+1));

    memset(row, 0, sizeof row);
    memset(col, 0, sizeof col);
    REP (i, D) REP (j, D) {
	REP (t, 4) {
	    if (t == 3) return false;
	    if (C[i][j] <= val[t]) {
		amax(row[i], t);
		amax(col[j], t);
		F[i][j] = t;
		break;
	    }
	}
    }
    memset(dp, 0, sizeof dp);
    REP (t, 4) REP (i, D) REP (j, D) {
	if (F[i][j] == 0 && row[i] < 2 && col[j] < 2) {
	    int mi = 1<<29;
	    int i2 = (i+D-1)%D;
	    int j2 = (j+D-1)%D;
	    amin(mi, dp[i2][j]);
	    amin(mi, dp[i][j2]);
	    amin(mi, dp[i2][j2]);
	    amax(dp[i][j], mi+1);
	}
    }

    int s = 0;
    REP (i, D) REP (j, D) amax(s, dp[i][j]);

    return s >= D-r;
}

void MAIN() {
    scanf("%d%d", &N, &D);
    REP (i, N) {
	int x, y;
	scanf("%d%d", &x, &y);
	x %= D;
	y %= D;
	C[x][y]++;
    }

    int lo = -1, hi = 1;
    while (!ok(hi)) {
	lo = hi;
	hi += hi;
    }
    while (hi - lo > 1) {
	int mid = (hi + lo) / 2;
	(ok(mid)? hi: lo) = mid;
    }

    printf("%d\n", hi);
}

int main() {
    int TC = 1;
//    scanf("%d", &TC);
    REP (tc, TC) MAIN();
    return 0;
}