#include <bits/stdc++.h>
using namespace std;
int N, M, T;
int tb[10005];
int main() {
  int i, j, k;
  scanf("%d%d%d", &N, &M, &k);
  N *= M;
  scanf("%d", &j);
  int r = j % k;
  tb[j]++;
  for (i = 1; i < N; i++) {
    scanf("%d", &j);
    if (j % k != r) {
      printf("-1\n");
      return 0;
    }
    tb[j]++;
  }
  int ans = 1000000000, co;
  for (i = r; i <= 10000; i += k) {
    co = 0;
    for (j = r; j <= 10000; j += k) {
      co += (tb[j] * abs(j - i) / k);
    }
    ans = min(ans, co);
  }
  printf("%d\n", ans);
}
