Problem:
Score : 800 points

Problem Statement

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

Partial Scores

500 points will be awarded for passing the test set satisfying 1 \leq D \leq 30.

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.

Sample Input 1

3 1
0 0
1 0
2 0

Sample Output 1

1

Sample Input 2

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

Sample Output 2

4

Sample Input 3

8 3
0 0
0 3
3 0
3 3
2 2
2 5
5 2
5 5

Sample Output 3

4

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include<unordered_set>
#include<set>
#include<tuple>
#include<stdio.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<string.h>
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)
#define eprintf(...) fprintf(stderr, __VA_ARGS__)

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');
}

template<class T> struct RXQ {
    int n;
    vector<T> A; vector<int> I;
    RXQ(int n_=0): n(n_), A(n), I(2*n) {
	for (int i=0; i<n; i++) I[i+n] = i;
	for (int i=n; --i;) I[i] = I[2*i];
    }
    RXQ(const vector<T> &a): n(a.size()), A(a), I(2*n) {
	for (int i=0; i<n; i++) I[i+n] = i;
	for (int i=n; --i;) I[i] = (A[I[i*2]] < A[I[i*2+1]]? I[i*2]: I[i*2+1]);
    }
    void modify(int i, const T &v) {
	A[i] = v;
	for (i+=n; i>>=1;) I[i] = (A[I[i*2]] < A[I[i*2+1]]? I[i*2+1]: I[i*2]);
    }
    int max_i(int x, int y) {
	x += n; y += n;
	int p = -1;
	for (; x<y; x>>=1, y>>=1) {
	    if (y & 1) { --y; if (p < 0 || A[p] < A[I[y]]) p = I[y]; }
	    if (x & 1) { if (p < 0 || A[p] < A[I[x]]) p = I[x]; x++; }
	}
	return p;
    }
    T max_v(int x, int y) { return A[max_i(x, y)]; }
};
/*
........;
..2..2..;
........;
1..1....;
..2..2..;
........;
1..1....;
*/

int N, D;
vector<pair<int, int> > P;

using Three = tuple<int, int, int>;
vector<Three> T;

const int MAXN = 100011;
LL small[MAXN];
LL large[MAXN];

VI Z[2011], H[2011];

LL XV[MAXN], YV[MAXN];

void MAIN() {
#ifdef LOCAL
    N = 100000;
    D = 1000;
    REP (i, N) {
	P.emplace_back(rand() % D, rand() % D);
    }
#else
    scanf("%d%d", &N, &D);
    REP (i, N) {
	int x, y;
	scanf("%d%d", &x, &y);
	P.emplace_back(x % D, y % D);
    }
#endif

    sort(P.begin(), P.end());

    for (int i=0; i<(int)P.size(); ) {
	int j = i+1;
	while (j < (int)P.size() && P[i] == P[j]) j++;

	T.emplace_back(j-i, P[i].first, P[i].second);
	i = j;
    }

    small[1] = large[1] = 1;
    for (int i=2; i<=N; i++) {
	if (i <= small[i-1] * large[i-1]) {
	    small[i] = small[i-1];
	    large[i] = large[i-1];
	} else if (small[i-1] == large[i-1]) {
	    small[i] = small[i-1];
	    large[i] = large[i-1] + 1;
	} else {
	    small[i] = small[i-1] + 1;
	    large[i] = large[i-1];
	}
	//eprintf("%lld %lld\n", small[i], large[i]);
    }

    int max_cnt = 0;
    EACH (e, T) amax(max_cnt, get<0>(*e));
    EACH (e, T) {
	int c = get<0>(*e);
	if (small[c] < small[max_cnt] || large[c] < large[max_cnt]) {
	    *e = Three(-1, -1, -1);
	}
    }
    T.erase(remove(T.begin(), T.end(), Three(-1, -1, -1)), T.end());

    unordered_set<int> g;
    LL ans = 1LL<<60;
    int zzz = 0;
    RXQ<LL> xrxq(N), yrxq(N);

    REP (bx, D) {
	int by = 0;

	auto update = [&](int i){
	    int cnt, x, y;
	    tie(cnt, x, y) = T[i];
	    while (x < bx) x += D;
	    while (y < by) y += D;
	    //eprintf("[%d %d]\n", bx,by);

	    if (y-by < x-bx) {
		XV[i] = x + (small[cnt]-1)*D;
		YV[i] = y + (large[cnt]-1)*D;
	    } else {
		XV[i] = x + (large[cnt]-1)*D;
		YV[i] = y + (small[cnt]-1)*D;
	    }

	    xrxq.modify(i, XV[i]);
	    yrxq.modify(i, YV[i]);
	    Z[(y-x+D)%D].push_back(i);
	    H[y].push_back(i);
	};

	REP (i, T.size()) {
	    update(i);
	}
	
	amin(ans, max(xrxq.max_v(0, N) - bx, yrxq.max_v(0, N) - by));

	while (by < D-1) {
	    g.clear();
	    EACH (e, Z[(by-bx+D)%D]) g.insert(*e);
	    EACH (e, H[by]) g.insert(*e);

	    Z[(by-bx+D)%D].clear();
	    H[by].clear();
	    by++;
	    EACH (e, g) {
		zzz++;
		int i = *e;
		update(i);
	    }

//	    eprintf("%d %d\n", bx, by);
//	    REP (i, T.size()) eprintf("(%lld %lld)", XV[i], YV[i]);
//	    eprintf("%lld %lld;\n", *Xse.rbegin(), *Yse.rbegin());
	    amin(ans, max(xrxq.max_v(0, N) - bx, yrxq.max_v(0, N) - by));
	}
    }

    printf("%lld\n", ans);
//    eprintf("%d\n", zzz);
}

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