#include <bits/stdc++.h>
using namespace std;
class PoloThePenguinAndMatrix {
 public:
  void solve(std::istream& in, std::ostream& out) {
    int i, tot_dif = 0, n, m, d, x, mid_value;
    vector<int> v;
    cin >> n >> m >> d;
    for (i = 0; i < n * m; i++) {
      cin >> x;
      v.push_back(x);
    }
    sort(v.begin(), v.end());
    mid_value = v[(m * n) / 2];
    for (i = 0; i < n * m; i++) {
      tot_dif += abs(mid_value - v[i]);
    }
    if (tot_dif % d != 0) {
      cout << "-1" << endl;
    } else {
      cout << tot_dif / d << endl;
    }
  }
};
int main() {
  std::ios_base::sync_with_stdio(0);
  std::cin.tie(0);
  PoloThePenguinAndMatrix Solver;
  std::istream& in(std::cin);
  std::ostream& out(std::cout);
  Solver.solve(in, out);
  return 0;
}
