#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, m, k;
  cin >> n >> m >> k;
  int arr[n * m];
  cin >> arr[0];
  long long int rem = arr[0] % k;
  int flag = 0;
  for (int i = 1; i < n * m; i++) {
    cin >> arr[i];
    if (rem != arr[i] % k) flag = -1;
  }
  sort(arr, arr + (m * n));
  long long int max = arr[(m * n) / 2];
  if (flag == -1) {
    cout << -1;
    return 0;
  }
  long long int sum = 0;
  for (int i = 0; i < m * n; i++) {
    long long int s = abs(arr[i] - max);
    s = s / k;
    sum += s;
  }
  cout << sum;
}
