#include <bits/stdc++.h>
using namespace std;
typedef struct _node {
  struct _node* L;
  struct _node* R;
  long long int key;
} tree;
typedef tree* treenode;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  long long int n, m, d, x;
  cin >> n >> m >> d;
  long long int a[n * m];
  for (long long int i = 0; i < n * m; i++) cin >> a[i];
  ;
  for (long long int i = 1; i < n * m; i++) {
    if (abs(a[i] - a[0]) % d != 0) {
      cout << "-1";
      return 0;
    }
  }
  sort(a, a + (n * m));
  long long int ans = 0;
  for (long long int i = 0; i < n * m; i++) ans += (a[i] - a[0]) / d;
  long long int j = a[0] + d;
  while (j <= a[n * m - 1]) {
    long long int rem = lower_bound(a, a + n * m, j) - a;
    long long int cnt = ans + rem - (n * m - rem);
    ans = min(ans, cnt);
    j += d;
  }
  cout << ans;
  return 0;
}
