#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int N;
ll X;
vector<ll> A;
struct Data {
vector<ll> cost; // cost[d][i] stored in a single vector per depth later
vector<ll> prefix; // prefix sum for that depth
};
vector<Data> depthData; // for each d = 0...H, store cost array and prefix sums
int H; // maximum recursion depth needed
// Factorizes n and returns its prime divisors.
vector<ll> getPrimeFactors(ll n) {
vector<ll> factors;
for (ll i = 2; i*i <= n; i++) {
if(n % i == 0){
factors.push_back(i);
while(n % i == 0)
n /= i;
}
}
if(n > 1)
factors.push_back(n);
return factors;
}
// Precompute cost arrays and prefix sums for each depth d from 0 to H.
// For parameter Y = X + d, for each element A[i] we want:
// cost = min_{p|Y} { (p - (A[i] mod p)) mod p }
void precomputeCosts() {
depthData.resize(H+1);
for (int d = 0; d <= H; d++) {
ll Y = X + d;
vector<ll> primes = getPrimeFactors(Y);
// Create cost array for current d:
vector<ll> costArr(N, 0);
for (int i = 0; i < N; i++){
ll best = LLONG_MAX;
for (ll p : primes) {
ll mod = A[i] % p;
ll need = (p - mod) % p; // if mod==0, need 0 operations.
best = min(best, need);
}
costArr[i] = best;
}
// Build prefix sum array for costArr.
vector<ll> prefixArr(N+1, 0);
for (int i = 0; i < N; i++){
prefixArr[i+1] = prefixArr[i] + costArr[i];
}
depthData[d].cost = move(costArr);
depthData[d].prefix = move(prefixArr);
}
}
// returns the total cost to "fix" indices [l, r) at depth d using the precomputed prefix sums.
ll rangeCost(int l, int r, int d) {
return depthData[d].prefix[r] - depthData[d].prefix[l];
}
// Recursive function that returns minimum operations to make the subarray A[l, r) (r exclusive)
// into an (X+d)-ordinary array.
ll solveRec(int l, int r, int d) {
// Base case: segment of length 1.
if(r - l == 1) {
return depthData[d].cost[l];
}
// Determine the unique allowed split.
int len = r - l;
int m = l + (len / 2); // first half: [l, m), second half: [m, r)
// Option 1: fix first half at current parameter (cost computed at depth d)
// and process second half recursively with parameter incremented.
ll cost1 = rangeCost(l, m, d) + solveRec(m, r, d+1);
// Option 2: fix second half at current parameter and process first half recursively.
ll cost2 = rangeCost(m, r, d) + solveRec(l, m, d+1);
return min(cost1, cost2);
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> N >> X;
A.resize(N);
for (int i = 0; i < N; i++){
cin >> A[i];
}
// Maximum recursion depth H is at most ceil(log2(N)).
H = ceil(log2(N));
// Precompute cost arrays for each depth d (0...H).
precomputeCosts();
ll ans = solveRec(0, N, 0);
cout << ans << "\n";
return 0;
}