#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, d;
  int a[105][105];
  int sum = 0;
  vector<int> b;
  cin >> n >> m >> d;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      cin >> a[i][j];
      b.push_back(a[i][j]);
      sum += a[i][j];
    }
  }
  sort(b.begin(), b.end());
  int ans = 1E8;
  for (int k = (n * m) / 2 - 1; k <= (n * m) / 2 + 1; ++k) {
    if (k < 0 || k >= (int)b.size()) {
      continue;
    }
    int cnt = 0;
    bool flag = true;
    for (int i = 0; i < n && flag; ++i) {
      for (int j = 0; j < m && flag; ++j) {
        if (abs(a[i][j] - b[k]) % d != 0) {
          flag = false;
        } else {
          cnt += abs(a[i][j] - b[k]) / d;
        }
      }
    }
    if (flag) {
      ans = min(ans, cnt);
    }
  }
  cout << (ans == 1E8 ? -1 : ans) << endl;
  return 0;
}
