#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, k, ans(0);
  bool flg = true;
  cin >> n >> m >> k;
  int ar[n * m];
  for (int a = 0; a < n * m; a++) cin >> ar[a];
  sort(ar, ar + n * m);
  int median = (n * m % 2) ? ar[n * m / 2] : ar[(n * m + 1) / 2];
  for (int a = 0; a < n * m; a++) {
    if (abs(median - ar[a]) % k != 0) flg = false;
    ans += abs(median - ar[a]) / k;
  }
  if (flg)
    cout << ans;
  else
    cout << -1;
}
