#include <bits/stdc++.h>
using namespace std;
int n, m, d, num, s[10005];
void doit() {
  int i, j, ans, k = s[1] % d;
  for (i = 2; i <= num; i++)
    if (s[i] % d != k) {
      printf("-1\n");
      return;
    }
  ans = 1000000007;
  sort(s + 1, s + 1 + num);
  for (i = 1; i <= num; i++) {
    k = 0;
    for (j = 1; j <= num; j++) {
      k += abs(s[i] - s[j]) / d;
      if (k >= ans) break;
    }
    if (k < ans) ans = k;
  }
  printf("%d\n", ans);
  return;
}
int main() {
  int i, j;
  scanf("%d%d%d", &n, &m, &d);
  num = 0;
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++) scanf("%d", &s[++num]);
  doit();
  return 0;
}
