#include <bits/stdc++.h>
using namespace std;
int a[111][111];
int main() {
  int n, m, d;
  while (cin >> n >> m >> d) {
    int s = 0x7fffffff, t = -0x7fffffff;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        cin >> a[i][j];
        s = s < a[i][j] ? s : a[i][j];
        t = t > a[i][j] ? t : a[i][j];
      }
    }
    int ans = 0x7fffffff;
    for (int k = s; k <= t; k += d) {
      int cnt = 0;
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
          if (abs((a[i][j] - k) % d)) {
            goto end;
          }
          cnt += abs(a[i][j] - k) / d;
        }
      }
      ans = ans < cnt ? ans : cnt;
    }
  end:;
    if (ans == 0x7fffffff)
      cout << -1 << endl;
    else
      cout << ans << endl;
  }
  return 0;
}
