#include <bits/stdc++.h>
const long long MOD = 1e9 + 7;
using namespace std;
long long a[100 * 100 + 1];
set<long long> s;
long long readInt() {
  bool minus1 = false;
  long long result = 0;
  char ch;
  ch = getchar();
  while (true) {
    if (ch == '-') break;
    if (ch >= '0' && ch <= '9') break;
    ch = getchar();
  }
  if (ch == '-')
    minus1 = true;
  else
    result = ch - '0';
  while (true) {
    ch = getchar();
    if (ch < '0' || ch > '9') break;
    result = result * 10 + (ch - '0');
  }
  if (minus1)
    return -result;
  else
    return result;
}
int main() {
  long double beg = clock();
  long long m = readInt(), n = m * readInt(), d = readInt();
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    s.insert(a[i] % d);
  }
  if (s.size() > 1) {
    cout << -1;
    return 0;
  }
  sort(a + 1, a + 1 + n);
  long long mid = a[(n + 1) / 2], ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += abs(mid - a[i]);
  }
  cout << ans / d;
  fprintf(stderr, "\n*** Total time = %.3lf ***\n",
          (clock() - beg) / CLOCKS_PER_SEC);
}
