#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, d;
  cin >> n >> m >> d;
  int ans = INT_MAX;
  vector<int> arr(n * m);
  for (int j = 0; j < n * m; j++) {
    cin >> arr[j];
  }
  for (int j = 0; j < n * m; j++) {
    int temp = 0, i = 0;
    for (; i < n * m; i++) {
      if (i != j) {
        if ((abs(arr[j] - arr[i])) % d == 0) {
          int p = (abs(arr[j] - arr[i])) / d;
          temp += p;
        } else {
          break;
        }
      }
    }
    if (i >= n * m) {
      ans = min(ans, temp);
    }
  }
  if (ans == INT_MAX)
    cout << -1;
  else
    cout << ans;
}
