#include <bits/stdc++.h>
using namespace std;
int main(int argc, const char* argv[]) {
  int n, k, b;
  long long s = 0, t;
  cin >> n >> k;
  int* a = new int[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n - 1; i++) {
    b = i;
    if (a[i] > a[i + 1]) {
      a[i] = a[i] + a[i + 1];
      a[i + 1] = a[i] - a[i + 1];
      a[i] = a[i] - a[i + 1];
      b = 0;
    }
    if ((a[i + 1] - a[i]) % k != 0) {
      cout << -1 << endl;
      return 0;
    }
    t = (a[i + 1] - a[i]) / k;
    a[i + 1] = a[i];
    s += (i + 1 - b) * t;
  }
  cout << s << endl;
  return 0;
}
