#include <bits/stdc++.h>
using namespace std;
int a[100005];
int main() {
  int n, m, d;
  cin >> n >> m >> d;
  cin >> a[0];
  long long tmp = a[0] % d, ans = 214748364777;
  long long p = 0;
  for (int i = 1; i < n * m; i++) {
    cin >> a[i];
    if (a[i] % d != tmp) {
      return cout << "-1", 0;
    }
  }
  for (int i = 0; i < n * m; i++) {
    p = 0;
    for (int j = 0; j < n * m; j++) {
      p += (abs(a[j] - a[i])) / d;
    }
    ans = min(ans, p);
  }
  cout << ans << '\n';
  return 0;
}
