#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, d, x;
  cin >> n >> m >> d;
  n *= m;
  vector<int> l;
  cin >> x;
  l.push_back(x);
  int mod = x % d, f = 0;
  for (int i = 1; i <= n - 1; ++i) {
    cin >> x;
    l.push_back(x);
    x %= d;
    if (x != mod) {
      f = 1;
      break;
    }
  }
  if (f) {
    cout << -1 << endl;
    return 0;
  }
  sort(l.begin(), l.end());
  int mi = l[0], ma = l[n - 1];
  long long sum = 0, mm = 1000000007;
  for (int i = mi; i <= ma; ++i) {
    sum = 0;
    if ((i - l[0]) % d) continue;
    for (int j = 0; j <= n - 1; ++j) sum += (abs(i - l[j])) / d;
    if (sum < mm) mm = sum;
  }
  cout << mm << endl;
}
