#include <bits/stdc++.h>
using namespace std;
int a, b, d, c[109][109];
long long mn = 1000000009, co;
bool e;
int main() {
  cin >> a >> b >> d;
  for (int i = 1; i <= a; i++)
    for (int j = 1; j <= b; j++) cin >> c[i][j];
  for (int i = 1; i <= 10000; i++) {
    for (int j = 1; j <= a; j++) {
      for (int k = 1; k <= b; k++) {
        if ((max(c[j][k], i) - min(c[j][k], i)) % d == 0)
          co += (max(c[j][k], i) - min(c[j][k], i)) / d;
        else {
          e = 1;
          break;
        }
      }
    }
    if (e == 0) mn = min(co, mn);
    co = 0, e = 0;
  }
  if (mn == 1000000009)
    cout << -1 << endl;
  else
    cout << mn << endl;
  return 0;
}
