#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000000;
const int MOD = 1000000007;
const int N = 300111;
int a[N];
void solve() {
  int n, m, d;
  cin >> n >> m >> d;
  n *= m;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  sort(a + 1, a + n + 1);
  for (int i = 2; i <= n; i++) {
    if ((a[i] - a[i - 1]) % d != 0) {
      cout << -1;
      return;
    }
  }
  int ans = INF;
  for (int i = 1; i <= n; i++) {
    int z = 0;
    for (int j = 1; j <= n; j++) {
      z += abs(a[i] - a[j]) / d;
    }
    ans = min(ans, z);
  }
  cout << ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  int T = 1;
  while (T--) {
    solve();
  }
  return 0;
}
