Little penguin Polo has an n × m matrix, consisting of integers. Let's index the matrix rows from 1 to n from top to bottom and let's index the columns from 1 to m from left to right. Let's represent the matrix element on the intersection of row i and column j as aij.

In one move the penguin can add or subtract number d from some matrix element. Find the minimum number of moves needed to make all matrix elements equal. If the described plan is impossible to carry out, say so.

Input

The first line contains three integers n, m and d (1 ≤ n, m ≤ 100, 1 ≤ d ≤ 104) — the matrix sizes and the d parameter. Next n lines contain the matrix: the j-th integer in the i-th row is the matrix element aij (1 ≤ aij ≤ 104).

Output

In a single line print a single integer — the minimum number of moves the penguin needs to make all matrix elements equal. If that is impossible, print "-1" (without the quotes).

Examples

Input

2 2 2
2 4
6 8


Output

4


Input

1 2 7
6 7


Output

-1
#include <bits/stdc++.h>
using namespace std;
void file_io();
void PLAY();
long long power(long long b, long long n, long long MOD);
bool is_prime(int n);
bool is_prime(long long n);
vector<int> sieve_of_Erato(int n);
long long max_subarray_sum(vector<int> A);
int main() {
  PLAY();
  int n, m, d;
  cin >> n >> m >> d;
  int g[n][m];
  set<int> st;
  for (int i = 0; i < int(n); i++)
    for (int j = 0; j < int(m); j++) {
      cin >> g[i][j];
      st.insert(g[i][j]);
    }
  bool solved = false;
  int ans = (2e9 + 100);
  for (auto num : st) {
    int f = 1;
    int cnt = 0;
    for (int i = 0; i < int(n); i++) {
      for (int j = 0; j < int(m); j++) {
        if ((num - g[i][j]) % d != 0) {
          f = 0;
          break;
        } else
          cnt += abs(num - g[i][j]) / d;
      }
      if (!f) break;
    }
    if (f) ans = min(ans, cnt), solved = true;
  }
  if (solved)
    cout << ans;
  else
    cout << -1;
}
void file_io() {
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
}
void PLAY() {
  ios_base::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
}
vector<int> sieve_of_Erato(int n) {
  vector<bool> number(n + 1, true);
  vector<int> primes;
  for (int i = 2; i * i <= n; i++) {
    if (number[i]) {
      for (int j = i * i; j <= n; j += i) number[j] = false;
    }
  }
  for (int i = 2; i <= n; i++)
    if (number[i]) primes.push_back(i);
  return primes;
}
bool is_prime(int n) {
  if (n <= 3)
    return n > 1;
  else if (n % 2 == 0 || n % 3 == 0)
    return false;
  int i = 5;
  while (i * i <= n) {
    if (n % i == 0 || n % (i + 2) == 0) return false;
    i += 6;
  }
  return true;
}
bool is_prime(long long n) {
  if (n <= 3)
    return n > 1;
  else if (n % 2 == 0 || n % 3 == 0)
    return false;
  long long i = 5ll;
  while (i * i <= n) {
    if (n % i == 0 || n % (i + 2ll) == 0) return false;
    i += 6ll;
  }
  return true;
}
long long max_subarray_sum(vector<int> A) {
  int n = A.size();
  vector<long long> local(n);
  long long global = A[0];
  local[0] = A[0];
  for (int i = 1; i < n; i++) {
    local[i] = max(local[i - 1] + A[i], (long long)(A[i]));
    global = max(global, local[i]);
  }
  return global;
}
long long power(long long b, long long n, long long MOD) {
  if (n < 0) return cerr << "n must be non-negative\n", 0;
  if (n == 0) return 1;
  if (n == 1) return b % MOD;
  if (n % 2 == 0) {
    b %= MOD;
    b *= b;
    b %= MOD;
    return power(b, n / 2, MOD) % MOD;
  }
  b %= MOD;
  return ((b) * (power(b * b, --n / 2, MOD) % MOD)) % MOD;
}
