#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define pdi pair<db,int>
#define mp make_pair
#define pb push_back
#define enter putchar('\n')
#define space putchar(' ')
#define eps 1e-8
//#define ivorysi
using namespace std;
typedef long long int64;
typedef double db;
template<class T>
void read(T &res) {
    res = 0;char c = getchar();T f = 1;
    while(c < '0' || c > '9') {
    if(c == '-') f = -1;
    c = getchar();
    }
    while(c >= '0' && c <= '9') {
    res = res * 10 + c - '0';
    c = getchar();
    }
    res *= f;
}
template<class T>
void out(T x) {
    if(x < 0) {x = -x;putchar('-');}
    if(x >= 10) {
    out(x / 10);
    }
    putchar('0' + x % 10);
}
int cnt[1005][1005],N,D,a;
int s2[1005][1005],s3[1005][1005];
int sum2(int x1,int y1,int x2,int y2) {
    if(x1 > x2 || y1 > y2) return 0;
    ++x1,++y1,++x2,++y2;
    return s2[x2][y2] - s2[x2][y1 - 1] - s2[x1 - 1][y2] + s2[x1 - 1][y1 - 1];
}
int sum3(int x1,int y1,int x2,int y2) {
    if(x1 > x2 || y1 > y2) return 0;
    ++x1,++y1,++x2,++y2;
    
    return s3[x2][y2] - s3[x2][y1 - 1] - s3[x1 - 1][y2] + s3[x1 - 1][y1 - 1];
}
bool check2(int x,int y,int b) {
    int d1 = sum2(max(0,b + x - D + 1),max(0,b + y - D + 1),x - 1,y - 1);
    int d2 = sum2(x + b + 1,y + b + 1,D - 1,D - 1);
    int d3 = sum2(max(0,b + x - D + 1),y + b + 1,x - 1,D - 1);
    int d4 = sum2(x + b + 1,max(0,b + y - D + 1),D - 1,y - 1);
    return !d1 && !d2 && !d3 && !d4;
}
bool check3(int x,int y,int b) {
    int d1 = sum3(x + b + 1,0,D - 1,D - 1);
    int d2 = sum3(0,y + b + 1,D - 1,D - 1);
    int d3 = sum3(max(0,b + x - D + 1),0,x - 1,D - 1);
    int d4 = sum3(0,max(0,b + y - D + 1),D - 1,y - 1);
    return !d1 && !d2 && !d3 && !d4;
}
void Solve() {
    read(N);read(D);
    int x,y;
    for(int i = 1 ; i <= N ; ++i) {
    read(x);read(y);
    cnt[x % D][y % D]++;
    }
    int t = 0;
    for(int i = 0 ; i < D ; ++i) {
    for(int j = 0 ; j < D ; ++j) {
        if(cnt[i][j]) {
        t = sqrt(cnt[i][j]);
        if(t * t < cnt[i][j]) ++t;
        a = max(t - 1,a);
        }
    }
    }
    for(int i = 0 ; i < D ; ++i) {
    for(int j = 0 ; j < D ; ++j) {
        if(cnt[i][j] > a * a && cnt[i][j] <= a * (a + 1)) s2[i + 1][j + 1]++;
        else if(cnt[i][j] > a * (a + 1)) s3[i + 1][j + 1]++;
    }
    }
    for(int i = 1 ; i <= D ; ++i) {
    for(int j = 1 ; j <= D ; ++j) {
        s2[i][j] = s2[i][j] + s2[i - 1][j] + s2[i][j - 1] - s2[i - 1][j - 1];
        s3[i][j] = s3[i][j] + s3[i - 1][j] + s3[i][j - 1] - s3[i - 1][j - 1];
    }
    }
    int b;
    for(b = 0 ; b < D ; ++b) {
    if(check2(0,0,b) && check3(0,0,b)) break; 
    }
    for(int i = 0 ; i < D ; ++i) {
    for(int j = 0 ; j < D ; ++j) {
        if(!(i + j)) continue;
        while(b) {
        if(check2(i,j,b - 1) && check3(i,j,b - 1)) --b;
        else break;
        }
    }
    }
    out(a * D + b);enter;
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
}