#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, d, i, j, p, c = 0, min = 1000000000, flag = 0;
  cin >> n >> m >> d;
  p = n * m;
  int a[p];
  memset(a, 0, sizeof(a));
  for (i = 0; i < p; i++) cin >> a[i];
  for (i = 0; i < p; i++) {
    c = 0;
    for (j = 0; j < p; j++) {
      if (abs(a[j] - a[i]) % d != 0) {
        cout << "-1"
             << "\n";
        flag = 1;
        break;
      } else
        c += abs(a[j] - a[i]) / d;
    }
    if (c < min) min = c;
    if (flag == 1) break;
  }
  if (flag == 0) cout << min << "\n";
  return 0;
}
