#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  long long int ans = 1000000000000, n, m, i, d, j, a, fo = 0;
  vector<long long int> v;
  set<long long int> mset;
  cin >> n >> m >> d;
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      cin >> a;
      mset.insert(a);
      v.emplace_back(a);
    }
  }
  sort(v.begin(), v.end());
  for (auto x : mset) {
    long long int counter = 0;
    for (j = 0; j < v.size(); j++) {
      a = abs(x - v[j]);
      if (a % d != 0 || counter >= ans) {
        counter = 1000000000000;
        break;
      }
      counter += (a / d);
    }
    if (ans > counter) {
      ans = counter;
    }
  }
  if (ans == 1000000000000)
    cout << "-1" << endl;
  else
    cout << ans << endl;
}
