#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  ;
  int n, m, d;
  cin >> n >> m >> d;
  int a[n * m];
  cin >> a[0];
  int mod = a[0] % d;
  int check = 0;
  for (int i = 1; i < n * m; i++) {
    cin >> a[i];
    if (a[i] % d != mod) {
      check = 1;
    }
  }
  if (check == 1) {
    cout << "-1";
    return 0;
  }
  sort(a, a + n * m);
  int mid = a[m * n / 2];
  int cnt = 0;
  for (int i = 0; i < n * m; i++) {
    cnt = cnt + abs(mid - a[i]) / d;
  }
  cout << cnt;
}
