#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 5;
int vec[N];
signed main() {
  cin.tie(0), cout.tie(0), ios::sync_with_stdio(0);
  int n, m, d;
  cin >> n >> m >> d;
  n *= m;
  for (int i = 0; i < n; ++i) cin >> vec[i];
  int ans = 1e9;
  for (int i = 0; i < n; ++i) {
    int temp = 0;
    for (int j = 0; j < n; ++j)
      if (i != j) {
        if (abs(vec[i] - vec[j]) % d) {
          cout << -1 << '\n';
          exit(0);
        }
        temp += abs(vec[i] - vec[j]) / d;
      }
    ans = min(ans, temp);
  }
  if (ans == 1e9) ans = -1;
  cout << ans << '\n';
  return 0;
}
