#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 10;
int a[N];
int main() {
  int n, m, d;
  scanf("%d%d%d", &n, &m, &d);
  for (int i = 0; i < n * m; ++i) scanf("%d", &a[i]);
  sort(a, a + n * m);
  int ans = 0, t = a[n * m / 2];
  for (int i = 0; i < n * m; ++i) {
    if (abs(t - a[i]) % d != 0) {
      puts("-1");
      return 0;
    }
    ans += abs(t - a[i]) / d;
  }
  printf("%d\n", ans);
  return 0;
}
