// 基本テンプレート
// #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;

#define debug(...) fprintf(stderr, __VA_ARGS__)
#define int long long int
 
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
 
typedef pair<int, int> pii;
typedef long long ll;
 
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const ll INF = 1001001001001001LL;
const ll MOD = 1000000007LL;

const int SIZE = 1010;
int N, D, cnt[SIZE][SIZE];

vector< vector<int> > get_table(int target) {
    vector< vector<int> > res(2*D+2, vector<int>(2*D+2));

    for(int i=0; i<D; i++) {
        for(int j=0; j<D; j++) {
            if(cnt[i][j] > target) {
                res[i+1  ][j+1  ]++;
                res[i+1+D][j+1+D]++;
                res[i+1  ][j+1+D]++;
                res[i+1+D][j+1  ]++;
            }
        }
    }

    for(int i=0; i<=2*D; i++) {
        for(int j=0; j<=2*D; j++) {
            res[i][j+1] += res[i][j];
        }
    }

    for(int j=0; j<=2*D; j++) {
        for(int i=0; i<=2*D; i++) {
            res[i+1][j] += res[i][j];
        }
    }

    return res;
}

int get_sum(const vector< vector<int> >& sum, int lx, int ly, int rx, int ry) {
    int res = 0;
    res += sum[rx][ry] + sum[lx][ly];
    res -= sum[lx][ry] + sum[rx][ly];
    return res;
}

void print_table(const vector< vector<int> >& sum) {
    for(int i=1; i<=2*D; i++) {
        for(int j=1; j<=2*D; j++) {
            cerr << get_sum(sum, i-1, j-1, i, j) << " ";
        }
        cerr << endl;
    }
}

signed main() {
    cin >> N >> D;

    for(int i=0; i<N; i++) {
        int x, y; cin >> x >> y;
        x %= D, y %= D;
        cnt[x][y]++;
    }

    int ansA = -1, ansB = -1;
    for(int i=0; i<D; i++) {
        for(int j=0; j<D; j++) {
            // fprintf(stderr, "cnt[%lld][%lld] = %lld\n", i, j, cnt[i][j]);
            int deg = 0;
            for(; (deg+1)*(deg+1)<cnt[i][j]; ) deg++;
            ansA = max(ansA, deg);
        }
    }

    vector< vector<int> > sum1 = get_table((ansA + 1) * (ansA + 1));
    vector< vector<int> > sum2 = get_table( ansA * ansA );
    vector< vector<int> > sum3 = get_table((ansA + 1) * ansA);

    /*
    fprintf(stderr, "# sum1 (target = %lld)\n", (ansA + 1) * (ansA + 1));
    print_table(sum1);

    fprintf(stderr, "# sum2 (target = %lld)\n", ansA * ansA);
    print_table(sum2);

    fprintf(stderr, "# sum3 (target = %lld)\n", ansA * (ansA + 1));
    print_table(sum3);
    */

    int ub = D-1, lb = -1;
    while(ub - lb > 1) {
        int b = (ub + lb) / 2;
        bool can = false;

        // (i, j) が正方形の左下
        for(int i=0; i<D; i++) {
            for(int j=0; j<D; j++) {
                bool ok = true;
                ok &= (get_sum(sum1, i    , j    , i+b+1, j+b+1) == 0);
                ok &= (get_sum(sum3, i+b+1, j    , i+D  , j+b+1) == 0);
                ok &= (get_sum(sum3, i    , j+b+1, i+b+1, j+D  ) == 0);
                ok &= (get_sum(sum2, i+b+1, j+b+1, i+D  , j+D  ) == 0); 
                
                can |= ok;
            }
        }

        if(can) ub = b;
        else lb = b;
    }
    ansB = ub;

    // fprintf(stderr, "ansA = %lld, ansB = %lld\n", ansA, ansB);
    cout << ansA * D + ansB << endl;
    return 0;
}


Measured memory_bytes: 102272