#include <bits/stdc++.h>
using namespace std;
void file_io();
void PLAY();
long long power(long long b, long long n, long long MOD);
bool is_prime(int n);
bool is_prime(long long n);
vector<int> sieve_of_Erato(int n);
long long max_subarray_sum(vector<int> A);
int main() {
  PLAY();
  int n, m, d;
  cin >> n >> m >> d;
  int g[n][m];
  set<int> st;
  for (int i = 0; i < int(n); i++)
    for (int j = 0; j < int(m); j++) {
      cin >> g[i][j];
      st.insert(g[i][j]);
    }
  bool solved = false;
  int ans = (2e9 + 100);
  for (auto num : st) {
    int f = 1;
    int cnt = 0;
    for (int i = 0; i < int(n); i++) {
      for (int j = 0; j < int(m); j++) {
        if ((num - g[i][j]) % d != 0) {
          f = 0;
          break;
        } else
          cnt += abs(num - g[i][j]) / d;
      }
      if (!f) break;
    }
    if (f) ans = min(ans, cnt), solved = true;
  }
  if (solved)
    cout << ans;
  else
    cout << -1;
}
void file_io() {
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
}
void PLAY() {
  ios_base::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
}
vector<int> sieve_of_Erato(int n) {
  vector<bool> number(n + 1, true);
  vector<int> primes;
  for (int i = 2; i * i <= n; i++) {
    if (number[i]) {
      for (int j = i * i; j <= n; j += i) number[j] = false;
    }
  }
  for (int i = 2; i <= n; i++)
    if (number[i]) primes.push_back(i);
  return primes;
}
bool is_prime(int n) {
  if (n <= 3)
    return n > 1;
  else if (n % 2 == 0 || n % 3 == 0)
    return false;
  int i = 5;
  while (i * i <= n) {
    if (n % i == 0 || n % (i + 2) == 0) return false;
    i += 6;
  }
  return true;
}
bool is_prime(long long n) {
  if (n <= 3)
    return n > 1;
  else if (n % 2 == 0 || n % 3 == 0)
    return false;
  long long i = 5ll;
  while (i * i <= n) {
    if (n % i == 0 || n % (i + 2ll) == 0) return false;
    i += 6ll;
  }
  return true;
}
long long max_subarray_sum(vector<int> A) {
  int n = A.size();
  vector<long long> local(n);
  long long global = A[0];
  local[0] = A[0];
  for (int i = 1; i < n; i++) {
    local[i] = max(local[i - 1] + A[i], (long long)(A[i]));
    global = max(global, local[i]);
  }
  return global;
}
long long power(long long b, long long n, long long MOD) {
  if (n < 0) return cerr << "n must be non-negative\n", 0;
  if (n == 0) return 1;
  if (n == 1) return b % MOD;
  if (n % 2 == 0) {
    b %= MOD;
    b *= b;
    b %= MOD;
    return power(b, n / 2, MOD) % MOD;
  }
  b %= MOD;
  return ((b) * (power(b * b, --n / 2, MOD) % MOD)) % MOD;
}
