#include <bits/stdc++.h>
using namespace std;
#define print(l) for(auto i:l) cout<<i<<" ";cout<<endl;
#define input(t,l,n) vector<t>l(n);for(int i = 0;i<n;i++)cin>>l[i];
#define int long long
#define pb push_back
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> 
#define all(l) l.begin(),l.end()
#define pii pair<int,int>
#define fi first
#define se second
const int M = 998244853;
int inf = 1e18;
void io(){
    #ifndef ONLINE_JUDGE
    freopen("input.in","r",stdin);
    // freopen("outputf.in","w",stdout);
    #endif
    ios::sync_with_stdio(0);//DO NOT USE IN INTERACTIVE
    cin.tie(0), cout.tie(0);
    cout << fixed<<setprecision(9);
}
int n,T;
void solve(){
     cin>>n>>T;
     input(int,l,n);
    sort(all(l));
    vector<pii>dp(n+1);
    dp[0].fi = l[0];
    dp[0].se = 0;
    for(int i = 1;i<n;i++){
        int x1,x2;
        if(dp[i-1].fi%T <= l[i]%T){
            x1 = dp[i-1].fi + (l[i]%T-dp[i-1].fi%T);
            x2 = x1-T;
        }
        else{
            x1 = dp[i-1].fi + (l[i]%T-dp[i-1].fi%T);
            x2 = x1+T;
        }
        // cout<<x1<<" "<<x2<<endl;
        dp[i].se = min((abs(dp[i-1].fi-x1)+1)/2,(abs(dp[i-1].fi-x2)+1)/2)+dp[i-1].se;
        if((abs(dp[i-1].fi-x1)+1)/2 < (abs(dp[i-1].fi-x2)+1)/2){
            dp[i].fi = dp[i-1].fi+(abs(dp[i-1].fi-x1)+1)/2;
        }
        else{
            dp[i].fi = dp[i-1].fi+(abs(dp[i-1].fi-x2)+1)/2;
        }
     }
     cout<<dp[1].se<<endl;
     //    int ans = 1e18;
     // for(int i = 1;i<=T;i++){
     //    int an = -1e18;
     //    for(int j:l){
     //        if(j >= i){
     //        int x1,x2;
     //        if(i%T <= j%T){
     //            x1 = i + (j%T-i%T);
     //            x2 = x1-T;
     //        }
     //        else{
     //            x1 = i + (j%T-i%T);
     //            x2 = x1+T;
     //        }
     //            an = max(an,min({abs(i-x1),abs(i-x2)}));
     //        }
     //        else{
     //            an = max(an,abs(i-j));
     //        }
     //    }
     //    ans = min(ans,an);
     //    an = 0;
     // }
     // cout<<ans<<endl;
}
signed main(){
    // io();
    int t = 1;
    while(t--) solve();
}
Compilation message (stderr)
Main.cpp: In function 'void io()':
Main.cpp:20:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   20 |     freopen("input.in","r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |