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 *a*<sub class="lower-index">*ij*</sub>.

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.
The first line contains three integers *n*, *m* and *d* (1 ≤ *n*, *m* ≤ 100, 1 ≤ *d* ≤ 10<sup class="upper-index">4</sup>) — 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 *a*<sub class="lower-index">*ij*</sub> (1 ≤ *a*<sub class="lower-index">*ij*</sub> ≤ 10<sup class="upper-index">4</sup>).
import sys
import math
from sys import stdin, stdout
 
# TAKE INPUT
def get_ints_in_variables():
    return map(int, sys.stdin.readline().strip().split())
def get_int(): return int(input())
def get_ints_in_list(): return list(
    map(int, sys.stdin.readline().strip().split()))
def get_list_of_list(n): return [list(
    map(int, sys.stdin.readline().strip().split())) for _ in range(n)]
def get_string(): return sys.stdin.readline().strip()
 
def main():
    # Write Your Code Here
    n, m , d = get_ints_in_variables()
    matrix = get_list_of_list(n)
    res = sys.maxsize
    # check for range 0, 10e4
    for i in range(0, 10001):
        flag = True
        moves = 0
        for j in range(0, n):
            for k in range(0, m):
                if (abs(matrix[j][k]-i)%d) != 0:
                    flag = False
                    break
                else:
                    moves += int(abs(matrix[j][k]-i)/d)
            if not flag:
                break
        if flag:
            res = min(res, moves)
    
    if res == sys.maxsize:
        print(-1)
    else:
        print(res)

#  calling main Function
if __name__ == "__main__":
    main()