#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")

#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/rope>

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
 
typedef long long ll;
typedef long double ld;
typedef complex<double> cd;

typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;

template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= (a); i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
#define trav(a, x) for (auto& a : x)

#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound

#define sz(x) (int)x.size()
#define beg(x) x.begin()
#define en(x) x.end()
#define all(x) beg(x), en(x)
#define resz resize

const int MOD = 1000000007;
const ll INF = 1e18;
const int MX = 100001;
const ld PI = 4*atan((ld)1);

template<class T> void ckmin(T &a, T b) { a = min(a, b); }
template<class T> void ckmax(T &a, T b) { a = max(a, b); }

namespace io {
    // Source: StackOverflow
    
    template<class T> struct like_array : is_array<T>{};
    template<class T, size_t N> struct like_array<array<T,N>> : true_type{};
    template<class T> struct like_array<vector<T>> : true_type{};
    template<class T> bool is_like_array(const T& a) { return like_array<T>::value; }

    // I/O 
    
    void setIn(string s) { freopen(s.c_str(),"r",stdin); }
    void setOut(string s) { freopen(s.c_str(),"w",stdout); }
    void setIO(string s = "") {
        ios_base::sync_with_stdio(0); cin.tie(0);
        if (sz(s)) { setIn(s+".in"), setOut(s+".out"); }
    }
    
    // INPUT 
    
    // double input seems slow on CF
    void re(double& x) { string t; cin >> t; x = stod(t); } 
    void re(ld& x) { string t; cin >> t; x = stold(t); }
    template<class T> void re(T& x) { cin >> x; }
    template<class Arg, class... Args> void re(Arg& first, Args&... rest) { 
        re(first); re(rest...); 
    }
    template<class T1, class T2> istream& operator>>(istream& is, pair<T1,T2>& p) {
        is >> p.f >> p.s; return is;
    }
    template<class T> istream& operator>>(istream& is, vector<T>& a) {
        F0R(i,sz(a)) is >> a[i];
        return is;
    }
    
    // OUTPUT 
    
    template<class T> void pr(const T& x) { cout << x << '\n'; }
    template<class Arg, class... Args> void pr(const Arg& first, const Args&... rest) { 
        cout << first << ' '; pr(rest...); 
    }
    template<class T1, class T2> ostream& operator<<(ostream& os, const pair<T1,T2>& a) {
        os << '{' << a.f << ", " << a.s << '}'; return os;
    }
    
    template<class T> ostream& printArray(ostream& os, const T& a, int SZ) {
        os << '{';
        F0R(i,SZ) {
            if (i) {
                os << ", ";
                if (is_like_array(a[i])) cout << "\n";
            }
            os << a[i];
        }
        os << '}';
        return os;
    }
    
    template<class T, size_t SZ> ostream& operator<<(ostream& os, const array<T,SZ>& a) {
        return printArray(os,a,SZ);
    }
    template<class T> ostream& operator<<(ostream& os, const vector<T>& a) {
        return printArray(os,a,sz(a));
    }
}

using namespace io;

int N,D;
int ans = MOD, cum[2][2001][2001];
int mx = 0;
vpi A,B;
map<pi,int> co;

int getSum(int k, int x0, int x1, int y0, int y1) {
    return cum[k][x1+1][y1+1]-cum[k][x0][y1+1]-cum[k][x1+1][y0]+cum[k][x0][y0];
}

bool ok(int x, int y, int mid) {
    if (getSum(1,x,x+mid,y,y+mid) != sz(B)) return 0;
    if (getSum(0,x+mid+1,x+D-1,y+mid+1,y+D-1) != 0) return 0;
    return 1;
}

void test(int x, int y) {
    int lo = 0, hi = D-1;
    while (lo < hi) {
        int mid = (lo+hi)/2;
        if (ok(x,y,mid)) hi = mid;
        else lo = mid+1;
    }
    ckmin(ans,lo);
}

int main() {
    // you should actually read the stuff at the bottom
    setIO(); re(N,D); 
    F0R(i,N) {
        int a,b; re(a,b);
        co[{a%D,b%D}] ++;
    }
    
    for (auto a: co) {
        int x = 1, y = 1;
        while (x*y < a.s) {
            if (x == y) y++;
            else x++;
        }
        if (mx < y) {
            A.clear(), B.clear();
            mx = y;
        }
        if (mx == y) {
            if (x == y) B.pb(a.f);
            else A.pb(a.f);
        }
    }
    for (auto t: A) {
        cum[0][t.f+1][t.s+1] ++;
        cum[0][t.f+D+1][t.s+1] ++;
        cum[0][t.f+1][t.s+1+D] ++;
        cum[0][t.f+1+D][t.s+1+D] ++;
    }
    for (auto t: B) {
        cum[1][t.f+1][t.s+1] ++;
        cum[1][t.f+D+1][t.s+1] ++;
        cum[1][t.f+1][t.s+1+D] ++;
        cum[1][t.f+1+D][t.s+1+D] ++;
    }
    F0R(k,2) FOR(i,1,2001) FOR(j,1,2001) cum[k][i][j] += cum[k][i-1][j]+cum[k][i][j-1]-cum[k][i-1][j-1];
    F0R(i,D) F0R(j,D) test(i,j);
    cout << ans+(mx-1)*D;
}

/* stuff you should look for
    * int overflow, array bounds
    * special cases (n=1?), set tle
    * do smth instead of nothing and stay organized
*/


Measured memory_bytes: 38648