#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, d, cot = 0;     cin >> n >> m >> d;     int arr[10004] = {0};     int rn = n * m;     for (int j = 1; j <= rn; j++) cin >> arr[j];     sort(arr, arr + rn);     int ind = arr[rn / 2];     if (n * m == 1) {       cout << 0;       return 0;     }     for (int i = 1; i <= rn; i++) {       if (abs(ind - arr[i]) % d != 0) {         cout << -1;         return 0;       } else {         cot += abs(ind - arr[i]) / d;       }     }     cout << cot << endl;     return 0;   }   