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>).
from sys import stdout
from sys import stdin
def get():
    return stdin.readline().strip()
def getf(sp = " "):
    return [int(i) for i in get().split(sp)]
def put(a, end = "\n"):
    stdout.write(str(a) + end)
def putf(a, sep = " ", end = "\n"):
    stdout.write(sep.join([str(i) for i in a]) + end)
     
#from collections import defaultdict as dd, deque
#from random import randint, shuffle, sample
#from functools import cmp_to_key, reduce
#from math import factorial as fac, acos, asin, atan2, gcd, log, e
#from bisect import bisect_right as br, bisect_left as bl, insort

from bisect import bisect_right as br, bisect_left as bl

def main():
    n, m, d = getf()
    q = [getf() for i in range(n)]
    rem = q[0][0] % d
    f = True
    a = []
    for i in range(n):
        for j in range(m):
            if(q[i][j] % d != rem):
                f = False
                break
            a += [q[i][j]]
    if(f == False):
        put(-1)
        return 0
    a.sort()
    pr = [0]
    k = n * m
    for i in range(k):
        pr += [pr[i] + a[i]]
    min_el = min(a)
    max_el = max(a)
    res = pr[k]
    for targ in range(min_el, max_el + d, d):
        l = bl(a, targ)
        r = br(a, targ)
        d1 = l * targ - pr[l]
        d2 = pr[k] - pr[r] - (k - r) * targ
        res = min(res, (d1 + d2) // d)
    put(res)
main()
