#include <bits/stdc++.h>
using namespace std;
int a[10004];
int sum = 0;
int n, m, d;
int main() {
  bool p = true;
  int i;
  cin >> n >> m >> d;
  for (i = 0; i < m * n; i++) {
    cin >> a[i];
  }
  sort(a, a + m * n);
  int b = a[n * m / 2];
  for (i = 0; i < n * m; i++) {
    int t = abs(b - a[i]);
    if (t % d != 0)
      p = false;
    else
      sum += t / d;
  }
  if (p == false)
    cout << "-1" << endl;
  else
    cout << sum << endl;
}
