///////////////////////////////////////////////////////////////////////////////

#include <bits/stdc++.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>

using namespace std;

///////////////////////////////////////////////////////////////////////////////

#define DEBUG 0

#define pb push_back
#define V vector
#define M unordered_map
#define S static
#define PI  (3.1415926535)

#define rep(i,n) for(ll i=0LL; i<n; ++i)
#define srep(i,s,n) for(ll i=s; i<n; ++i)
#define rrep(i,n) for(ll i=n-1LL; i>=0LL; --i)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define CIN(x) do { \
        assert(!cin.eof()); \
        cin >> x; \
        assert(!cin.fail()); \
} while(0);
#if DEBUG
#define debug_print(...) _debug_print(__VA_ARGS__)
#define debug_printf(...) printf(__VA_ARGS__)
#define debug_print_time _debug_print_time
#else // DEBUG
#define debug_print(...)
#define debug_printf(...)
#define debug_print_time
#endif // DEBUG

typedef long long ll;
typedef unsigned long long ull;
typedef tuple<ll, ll> t2;
typedef tuple<ll, ll, ll> t3;
typedef tuple<ll, ll, ll, ll> t4;
typedef tuple<ll, ll, ll, ll, ll> t5;
template <typename T>
using priority_queue_incr = priority_queue<T, V<T>, greater<T> >;

///////////////////////////////////////////////////////////////////////////////

template <typename TT, typename T>
T get_m(M<TT, T> &m, TT k, T default_value)
{
    if (m.find(k) == m.end()) return m[k] = default_value;
    return m[k];
}

template <typename TT, typename T>
void incr_m(M<TT, T> &m, TT k)
{
    if (m.find(k) == m.end()) m[k] = 0;
    m[k]++;
}

void llin(ll &a)
{
    CIN(a);
}

void llinl1(auto &v, ll count)
{
    for (ll i = 0LL; i < count; ++i) {
        ll a;
        CIN(a);
        v.push_back(a);
    }
}

void llinl2(auto &v, ll count)
{
    for (ll i = 0LL; i < count; ++i) {
        ll a, b;
        CIN(a >> b);
        v.push_back(t2(a, b));
    }
}

void llinl3(auto &v, ll count)
{
    for (ll i = 0LL; i < count; ++i) {
        ll a, b, c;
        CIN(a >> b >> c);
        v.push_back(t3(a, b, c));
    }
}

void llinl4(auto &v, ll count)
{
    for (ll i = 0LL; i < count; ++i) {
        ll a, b, c, d;
        CIN(a >> b >> c >> d);
        v.push_back(t4(a, b, c, d));
    }
}

void llina(auto &v, ll count)
{
    llinl1(v, count);
}

template <typename T>
void sort(V<T> &v)
{
    sort(v.begin(), v.end());
}

template <typename T>
void sort_reverse(V<T> &v)
{
    sort(v.begin(), v.end(), greater<T>());
}

void _debug_print(auto x)
{
    cout << x << endl;
}

void _debug_print(const t2 &x)
{
    ll x1 = get<0>(x);
    ll x2 = get<1>(x);
    cout << "-- " << x1 << " -- " << x2 << endl;
}

void _debug_print(const t3 &x)
{
    ll x1 = get<0>(x);
    ll x2 = get<1>(x);
    ll x3 = get<2>(x);
    cout << "-- " << x1 << " -- " << x2 << " -- " << x3 << endl;
}

void _debug_print(const t4 &x)
{
    ll x1 = get<0>(x);
    ll x2 = get<1>(x);
    ll x3 = get<2>(x);
    ll x4 = get<3>(x);
    cout << "-- " << x1 << " -- " << x2
         << " -- " << x3 << " -- " << x4 << endl;
}

template <typename T>
void _debug_print(T xarray[], ll n)
{
    rep (i, n) _debug_print(xarray[i]);
}

template <typename T>
void _debug_print(const V<T> &xlist)
{
    for (auto x : xlist) {
        cout << "-- ";
        _debug_print(x);
    }
}

template <typename T>
void _debug_print(const set<T> &xset)
{
    for (auto x : xset) {
        cout << "-- ";
        _debug_print(x);
    }
}

template <typename TT, typename T>
void _debug_print(const M<TT, T> &xlist)
{
    for (auto x : xlist) {
        TT k = x.first;
        T v = x.second;
        cout << "====" << endl;
        cout << "K=";
        _debug_print(k);
        cout << "V=";
        _debug_print(v);
    }
}

void _debug_print_time(const char *prefix)
{
    struct timeval tv;
    gettimeofday(&tv, NULL);
    struct tm *tm = localtime(&tv.tv_sec);
    printf("-- %s %02d:%02d:%02d.%06ld\n",
           prefix, tm->tm_hour, tm->tm_min, tm->tm_sec, tv.tv_usec);
}

///////////////////////////////////////////////////////////////////////////////

void _main();
int main()
{
    cout << setprecision(12);
#if !DEBUG
    ios::sync_with_stdio(false);
    cin.tie(0);
#endif

    _main();
    return 0;
}

void f_lens(ll sqlens[1005][1005], ll rectlens[1005][1005],
            const ll cnts[1005][1005], ll d)
{
    rep (x, d) rep (y, d) {
        ll cnt = cnts[x][y];
        if (!cnt) continue;
        ll mlt = 0;
        while (mlt * mlt < cnt) mlt++;
        bool rect = false;
        if (cnt <= mlt * (mlt-1)) rect = true;
        if (rect) rectlens[x][y] = mlt;
        else sqlens[x][y] = mlt;
    }
}

void f_cnts(ll sqcnts[2010][2010], ll rectcnts[2010][2010],
            const ll sqlens[1005][1005], const ll rectlens[1005][1005],
            ll maxval, ll d)
{
    ll total = 0;
    rep (x, d * 2) {
        if (sqlens[x%d][0] == maxval) total++;
        sqcnts[x+1][1] = total;
    }

    total = 0;
    rep (y, d * 2) {
        if (sqlens[0][y%d] == maxval) total++;
        sqcnts[1][y+1] = total;
    }

    total = 0;
    rep (x, d * 2) {
        if (rectlens[x%d][0] == maxval) total++;
        rectcnts[x+1][1] = total;
    }

    total = 0;
    rep (y, d * 2) {
        if (rectlens[0][y%d] == maxval) total++;
        rectcnts[1][y+1] = total;
    }

    srep (x, 2, d*2+1) srep (y, 2, d*2+1) {
        sqcnts[x][y] = sqcnts[x-1][y] + sqcnts[x][y-1] - sqcnts[x-1][y-1];
        if (sqlens[(x-1)%d][(y-1)%d] == maxval) sqcnts[x][y]++;
        rectcnts[x][y] = rectcnts[x-1][y] + rectcnts[x][y-1] - rectcnts[x-1][y-1];
        if (rectlens[(x-1)%d][(y-1)%d] == maxval) rectcnts[x][y]++;
    }
}

ll solve(const ll sqcnts[2010][2010], const ll rectcnts[2010][2010],
         ll d, ll minlen, ll maxlen, ll x, ll y)
{
    ll base = minlen;
    ll now = (minlen + maxlen) / 2LL;
    ll ans = maxlen;

    for ( ; minlen <= maxlen; now = (minlen + maxlen) / 2LL) {
        ll diff = now - base;
        ll judge1a = sqcnts[x+diff+1][y+d] - sqcnts[x+diff+1][y+diff+1];
        ll judge1b = sqcnts[x+d][y+diff+1] - sqcnts[x+diff+1][y+diff+1];
        ll judge2sq = sqcnts[x+d][y+d] - sqcnts[x+diff+1][y+d] \
                      - sqcnts[x+d][y+diff+1] + sqcnts[x+diff+1][y+diff+1];
        ll judge2rect = rectcnts[x+d][y+d] - rectcnts[x+diff+1][y+d] \
                        - rectcnts[x+d][y+diff+1] + rectcnts[x+diff+1][y+diff+1];
        if (judge1a || judge1b || judge2sq || judge2rect) {
            minlen = now + 1;
        }
        else {
            maxlen = now - 1;
            ans = min(ans, now);
        }
    }

    return ans;
}

void _main()
{
    ll n; llin(n);
    ll d; llin(d);
    S ll cnts[1005][1005];
    memset(cnts, 0, sizeof(cnts));
    rep (i, n) {
        int x, y;
        cin >> x >> y;
        cnts[x%d][y%d]++;
    }

    debug_printf("---- cnts\n");
    rep (x, d) {
        rep (y, d) debug_printf("%lld ", cnts[x][y]);
        debug_printf("\n");
    }

    S ll sqlens[1005][1005]; memset(sqlens, 0, sizeof(sqlens));
    S ll rectlens[1005][1005]; memset(rectlens, 0, sizeof(rectlens));
    f_lens(sqlens, rectlens, cnts, d);

    debug_printf("---- sqlens\n");
    rep (x, d) {
        rep (y, d) debug_printf("%lld ", sqlens[x][y]);
        debug_printf("\n");
    }

    debug_printf("---- rectlens\n");
    rep (x, d) {
        rep (y, d) debug_printf("%lld ", rectlens[x][y]);
        debug_printf("\n");
    }

    ll maxval = 0;
    rep (x, d) rep (y, d) maxval = max(maxval, sqlens[x][y]);
    rep (x, d) rep (y, d) maxval = max(maxval, rectlens[x][y]);

    ll minlen = (maxval-1) * d;
    debug_printf("---- minlen = %lld\n", minlen);

    ll maxlen = minlen + d;

    S ll sqcnts[2010][2010]; memset(sqcnts, 0, sizeof(sqcnts));
    S ll rectcnts[2010][2010]; memset(rectcnts, 0, sizeof(rectcnts));
    f_cnts(sqcnts, rectcnts, sqlens, rectlens, maxval, d);

    debug_printf("---- sqcnts\n");
    rep (x, d*2+1) {
        rep (y, d*2+1) debug_printf("%lld ", sqcnts[x][y]);
        debug_printf("\n");
    }

    debug_printf("---- rectcnts\n");
    rep (x, d*2+1) {
        rep (y, d*2+1) debug_printf("%lld ", rectcnts[x][y]);
        debug_printf("\n");
    }

    ll ans = 1e18;
    rep (x, d) rep (y, d) {
        ans = min(ans, solve(sqcnts, rectcnts, d, minlen, maxlen, x, y));
    }

    debug_printf("---- ans\n");
    cout << ans << endl;
}

///////////////////////////////////////////////////////////////////////////////


Measured memory_bytes: 87040