#include <bits/stdc++.h>
using namespace std;
int a[10001];
int best = 100000000;
int main() {
  int n, m, d;
  scanf("%d %d %d", &n, &m, &d);
  scanf("%d", &a[0]);
  int c = a[0] % d;
  int lll = n * m;
  for (int i = 1; i < lll; i++) {
    scanf("%d", &a[i]);
    if (a[i] % d != c) {
      cout << -1 << endl;
      return 0;
    }
  }
  for (int i = 0; i < lll; i++) {
    int ans = 0;
    int lo = a[i];
    for (int j = 0; j < lll; j++) {
      ans += abs(a[j] - lo) / d;
    }
    best = min(ans, best);
  }
  cout << best << endl;
  return 0;
}
