#include <bits/stdc++.h>
using namespace std;
int main() {
  long int n, m, d, a, left[10003], right[10003], k, bisa = 1,
                                                     mini = 2000000000;
  vector<long int> num;
  cin >> n >> m >> d;
  for (int i = 0; i < n * m; i++) {
    cin >> a;
    num.push_back(a);
  }
  sort(num.begin(), num.end());
  left[0] = 0;
  for (int i = 1; i < num.size(); i++) {
    k = (num[i] - num[i - 1]) / d;
    if ((num[i] - num[i - 1]) % d != 0) {
      bisa = 0;
      break;
    }
    left[i] = left[i - 1] + (i)*k;
  }
  right[num.size() - 1] = 0;
  for (int i = num.size() - 2; i >= 0; i--) {
    k = (num[i + 1] - num[i]) / d;
    right[i] = right[i + 1] + (num.size() - i - 1) * k;
  }
  for (int i = 0; i < num.size(); i++) {
    mini = min(mini, left[i] + right[i]);
  }
  if (!bisa)
    cout << "-1\n";
  else
    cout << mini << endl;
  return 0;
}
