#include <bits/stdc++.h>
#define int long long
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pi pair<int, int>
#define f first
#define s second
using namespace std;
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, t;
cin >> n >> t;
vector<int> a(n), md;
vector<int> tp;
for (int i = 0; i < n; ++i) {
cin >> a[i];
if (a[i] < t) {
md.push_back(a[i] % t);
} else {
tp.push_back(a[i] % t);
}
}
sort(all(md));
sort(all(tp));
if (tp.empty()) {
cout << (md.back() - md[0] + 1) / 2 << "\n";
return 0;
}
int res = 1e18;
int mn = 1e9, mx = -1e9;
if (!md.empty()) {
mn = md[0];
mx = md.back();
}
int mm = min(mn, tp[0]), mxx = max(mx, tp.back());
res = min(res, (mxx - mm + 1) / 2);
for (int i = 0; i < (int)tp.size(); ++i) {
mx = tp[i] + 1;
mm = md.empty() ? 1e18 : md[0];
if (i + 1 < tp.size()) {
mm = min(mm, tp[i + 1]);
}
if (mm == 1e18) continue;
res = min(res, (mx + t - mm + 1) / 2);
}
cout << res << "\n";
}