#include <bits/stdc++.h>
using namespace std;
int main() {
  vector<int> v;
  int n, m, d, sum = 0;
  cin >> n >> m >> d;
  for (int i = 0; i < n * m; i++) {
    int a;
    cin >> a;
    v.push_back(a);
  }
  sort(v.begin(), v.end());
  for (int i = 0; i < n * m; i++) {
    sum += abs(v[i] - v[(n * m) / 2]);
  }
  if (sum % d) {
    cout << "-1" << endl;
  } else {
    cout << sum / d << endl;
  }
}
