#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2 * 1e5 + 10;
ll res;
ll a[N];
void solve() {
  ll n, m, d, x, ans = 0;
  cin >> n >> m >> d;
  std::vector<ll> v;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> x;
      v.push_back(x);
    }
  }
  sort(v.begin(), v.end());
  ll k = v[v.size() / 2];
  for (auto i : v) {
    ll t = abs(i - k);
    if (t % d == 0) {
      ans += t / d;
    } else {
      cout << -1;
      return;
    }
  }
  cout << ans;
}
int main(int argc, char const *argv[]) {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  ll t = 1;
  while (t--) {
    solve();
  }
}
