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, io, os
import math
import bisect
import heapq
import string
from collections import defaultdict,Counter,deque
input = sys.stdin.readline
 
def I():
    return input()
 
def II():
    return int(input())
 
def MII():
    return map(int, input().split())
 
def LI():
    return list(input().split())
 
def LII():
    return list(map(int, input().split()))
 
def GMI():
    return map(lambda x: int(x) - 1, input().split())
 
def LGMI():
    return list(map(lambda x: int(x) - 1, input().split()))
 
def WRITE(out):
  return print('\n'.join(map(str, out)))
 
def WS(out):
  return print(' '.join(map(str, out)))
 
def WNS(out):
  return print(''.join(map(str, out)))

def WSNOPRINT(out):
  return ''.join(map(str, out))

'''
all nums%d must be the same
take total sum and find the closest multiple of d and subtract
'''
def solve():
    n, m, d = MII()
    ok = True
    rem = -1
    nums = []
    for _ in range(n):
        for x in LII():
            if rem == -1:
                rem = x%d
            elif rem != x%d:
                ok = False
                break
            nums.append(x)
    
    if not ok:
        print(-1)
    else:
        total = sum(nums)
        avg = total/(n*m)
        avg = math.floor(avg)

        ans = 10**9
        for i in range(avg-d, avg+d):
            if abs(i-nums[0])%d == 0:
                ans = min(ans, sum([abs(x-i) for x in nums]))
        
        print(ans//d)

solve()