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 os import path
import sys,time, collections as c , math as m , pprint as p
maxx , localsys , mod = float('inf'), 0 , int(1e9 + 7) 
if (path.exists('input.txt')):  sys.stdin=open('input.txt','r') ;   sys.stdout=open('output.txt','w')
input = sys.stdin.readline
n , m , k = map(int , input().split()) ; arr = [list(map(int , input().split())) for _ in range(m)]
su , x=sum(sum(i) for i in arr)/ (n*m) , 0
for i in range(n):
    for j in range(m):
        x+= abs(su - arr[i][j])
print(-1 if x % k else int(x//k))



        



























# def ok(p , s):
#     cnt , need =0 , 0
#     for i in s:
#         if p[need] == i:
#             cnt+=1 ; need ^= 1
#     if cnt % 2 and p[0] != p[1]:
#         cnt-=1
#     return cnt

# for _ in range(int(input())):
#     s = input().rstrip('\n') ; n , ans = len(s) , maxx
#     for i in range(10):
#         for j in range(10):
#             ans = min(ans , n - ok((str(i) , str(j)), s))
#     print(ans)
#This problem was so easy oh gawd , so you can only make left cyclic shift = right cyclic shift , when there are at most
#2 characters
#(incase of 1 ch) at the same time if total string has only character it is valid no matter how you see it 
#(incase of 2 ch) all the characters at odd positions must be equal , and all the characters at even position must be equal





# n , k = map(int , input().split()) ; s = input().rstrip('\n')
# ans = a = b = j = 0
# for i in range(n):
#     a , b = (a+1 , b) if s[i] == 'a' else (a , b+1 )
#     if min(a, b) > k :
#         a , b = (a -1 , b) if s[j] == 'a' else (a , b -1) ; j+=1
#     else:
#         ans+=1
# print(ans)
# #two - pointer method , if at any point min(a , b )> k then keep on decreasing from the beginning untill and unless you get min(a , b) 
# #less than k