#include <bits/stdc++.h>
using namespace std;
int main() {
  int a[11111], b[11111], n, m, d, i, j, max;
  scanf("%d %d %d", &n, &m, &d);
  for (i = 0; i < n * m; i++) {
    scanf("%d", &a[i]);
  }
  sort(a, a + n * m);
  max = a[n * m - 1];
  int fla = 0;
  for (i = 0; i < n * m; i++) {
    if ((a[i] - a[0]) % d != 0) fla = 1;
    if (fla) break;
    b[i] = (a[i] - a[0]) / d;
  }
  if (fla) {
    printf("-1\n");
    return 0;
  }
  int num = (max - a[0]) / d, sum = 0;
  int min = 999999999;
  for (j = 0; j <= num; j++) {
    sum = 0;
    for (i = 0; i < n * m; i++) {
      if (b[i] > j)
        sum += b[i] - j;
      else
        sum += j - b[i];
    }
    if (sum < min) min = sum;
  }
  printf("%d\n", min);
  return 0;
}
