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>).
def bs(A,B):
    l,r=0,10000
    mid=(l+r)//2
    while l<r:
        mid=(l+r)//2
        if A+(mid*d)>B:r=mid
        elif A+(mid*d)==B:break
        elif A+(mid*d)<B:l=mid+1
    return mid

n,m,d=[int(x) for x in input().split()]
arr=list()
for _ in range(n):
    tmp=list(map(int,input().split()))
    arr.extend(tmp)
ans=1000000000
#print(arr)
for i in range(1,10000):
    tmp=0
    for j in range(len(arr)):
        if arr[j]>i:
            if arr[j]%d==i%d:
                mult=bs(i,arr[j])
                if i+(mult*d)==arr[j]:tmp+=mult
                elif i+(mult*d)>arr[j]:tmp+=mult-1
                else: tmp+=mult+1
            else:tmp=100000000; break
        elif arr[j]==i:continue
        else:
            if arr[j]%d==i%d:
                mult=bs(arr[j],i)
                if arr[j]+(mult*d)==i:tmp+=mult
                elif arr[j]+(mult*d)>i:tmp+=mult-1
                else: tmp+=mult+1
            else:tmp=100000000; break
    #if(tmp):print(tmp)
    ans=min(ans,tmp)
    #print(ans)
print(-1 if ans>10000 else ans)