#include <bits/stdc++.h>
using namespace std;
void print(bool res) {
  if (res)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
int modu = 1073741824;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m, d;
  cin >> n >> m >> d;
  int a[(n * m) + 2], maxi = 0, flag = 0;
  for (int i = 1; i <= n * m; i++) cin >> a[i];
  for (int i = 2; i <= n * m; i++) {
    if (a[i] % d != a[i - 1] % d) {
      flag = 1;
      break;
    }
  }
  if (flag)
    cout << -1 << endl;
  else {
    sort(a + 1, a + (n * m) + 1);
    int index = ceil((n * m) / 2.0);
    int move = 0, value = a[index], res;
    for (int i = 1; i <= n * m; i++) {
      if (i == index) continue;
      res = abs(a[i] - value);
      move += (res / d);
    }
    cout << move << endl;
  }
  return 0;
}
