#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, d;
  cin >> n >> m >> d;
  vector<int> nums(n * m);
  int k = 0;
  set<int> rem;
  set<int> nums_set;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      cin >> nums[k];
      rem.insert(abs(nums[k] % d));
      nums_set.insert(nums[k]);
      ++k;
    }
  }
  if (rem.size() != 1) {
    cout << -1 << endl;
    return 0;
  }
  if (nums_set.size() == 1) {
    cout << 0 << endl;
    return 0;
  }
  sort(nums.begin(), nums.end());
  int ans = INT_MAX;
  for (int i = 0; i < nums.size(); ++i) {
    int cur = 0;
    for (int j = 0; j < nums.size(); ++j) {
      if (nums[i] == nums[j]) continue;
      cur += abs(nums[j] - nums[i]) / d;
    }
    ans = min(ans, cur);
  }
  cout << ans << endl;
}
