#include <bits/stdc++.h>
using namespace std;
int arr[10003];
int main() {
  int N, M, D;
  cin >> N >> M >> D;
  N = N * M;
  for (int i = 0; i < N; i++) cin >> arr[i];
  sort(arr, arr + N);
  int mid = arr[N / 2];
  int res = 0;
  bool flag = true;
  for (int i = 0; i < N && flag; i++) {
    int tmp = (arr[i] - mid) < 0 ? -(arr[i] - mid) : (arr[i] - mid);
    if (tmp % D == 0)
      res += tmp / D;
    else
      flag = false;
  }
  if (flag)
    cout << res << endl;
  else
    cout << -1 << endl;
  return 0;
}
