#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 5e3 + 7;
ll a[MAXN], b[MAXN], n, k;
ll ans = LLONG_MAX;
ll x = 1;
int main(){
ios_base::sync_with_stdio(0);
cout.tie(0);
cin.tie(0);
cin >> n >> k;
for(int i = 1; i <= n; i++) cin >> a[i];
for(int i = 1; i <= 5007; i++){
ll sum = 0;
for(int j = 1; j <= n; j++) b[j] = x + (j - 1) * k;
x++;
for(int j = 1; j <= n; j++) sum += abs(b[j] - a[j]);
ans = min(ans, sum);
fill(b, b + 1, 0);
}
cout << ans;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CnR5cGVkZWYgbG9uZyBsb25nIGxsOwpjb25zdCBpbnQgTUFYTiA9IDVlMyArIDc7CmxsIGFbTUFYTl0sIGJbTUFYTl0sIG4sIGs7CmxsIGFucyA9IExMT05HX01BWDsKbGwgeCA9IDE7CmludCBtYWluKCl7CiAgICBpb3NfYmFzZTo6c3luY193aXRoX3N0ZGlvKDApOwogICAgY291dC50aWUoMCk7CiAgICBjaW4udGllKDApOwogICAgY2luID4+IG4gPj4gazsKICAgIGZvcihpbnQgaSA9IDE7IGkgPD0gbjsgaSsrKSBjaW4gPj4gYVtpXTsKICAgIGZvcihpbnQgaSA9IDE7IGkgPD0gNTAwNzsgaSsrKXsKICAgICAgICBsbCBzdW0gPSAwOwogICAgICAgIGZvcihpbnQgaiA9IDE7IGogPD0gbjsgaisrKSBiW2pdID0geCArIChqIC0gMSkgKiBrOwogICAgICAgIHgrKzsKCiAgICAgICAgZm9yKGludCBqID0gMTsgaiA8PSBuOyBqKyspIHN1bSArPSBhYnMoYltqXSAtIGFbal0pOwogICAgICAgIGFucyA9IG1pbihhbnMsIHN1bSk7CiAgICAgICAgZmlsbChiLCBiICsgMSwgMCk7CiAgICB9CiAgICBjb3V0IDw8IGFuczsKfQo=