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
from os import path
from math import gcd,floor,sqrt,log,ceil
from bisect import bisect_left, bisect_right
from heapq import heappop, heappush, heapify

############ ---- Input Functions ---- ####################
def inp():
    return(int(input()))
def inlt():
    return(list(map(int,input().split())))
def insr():
    s = input()
    return(list(s[:len(s) - 1]))
def invr():
    return(map(int,input().split()))
def inmatrix(n,m):
    arr=[]
    for i in range(n):
        temp=inlt()
        arr.append(temp)
    return arr
############################################################
def cuberoot(x):
    if 0<=x: return x**(1./2.)
    return -(-x)**(1./2.)
def is_anagram(str1, str2):
    return sorted(str1) == sorted(str2)
############################################################
mod=1000000007
############################################################
if(path.exists('input.txt')):
    sys.stdin = open("input.txt","r")
    sys.stdout = open("output.txt","w")
    sys.stderr = open("error.txt", "w")
else:
    input = sys.stdin.readline
############################################################
# class Graph:
 
#     # init function to declare class variables
#     def __init__(self, V):
#         self.V = V
#         self.adj = [[] for i in range(V)]
#         self.cyclenumber=0
#         self.cycles=[[] for i in range(V)]
 
#     def DFSUtil(self, temp, v, visited):
 
#         # Mark the current vertex as visited
#         visited[v] = True
 
#         # Store the vertex to list
#         temp.append(v)
 
#         # Repeat for all vertices adjacent
#         # to this vertex v
#         for i in self.adj[v]:
#             if visited[i] == False:
 
#                 # Update the list
#                 temp = self.DFSUtil(temp, i, visited)
#         return temp
 
#     # method to add an undirected edge
#     def addEdge(self, v, w):
#         self.adj[v].append(w)
#         self.adj[w].append(v)
 
#     # Method to retrieve connected components
#     # in an undirected graph
#     def connectedComponents(self):
#         visited = []
#         cc = []
#         for i in range(self.V):
#             visited.append(False)
#         for v in range(self.V):
#             if visited[v] == False:
#                 temp = []
#                 cc.append(self.DFSUtil(temp, v, visited))
#         return cc


def solve():
    n,m,d=invr()
    ar=[]
    for i in range(n):
        t=inlt()
        ar+=t
    ar.sort()
    c=0
    ele=ar[n*m//2]
    # print(ele)
    for i in range(len(ar)):
        if abs(ele-ar[i])%d!=0:
            print(-1)
            return
        else:
            c+=abs(ele-ar[i])//d
    print(c)
    

def main():
    # T=inp()
    T=1
    for i in range(1,T+1):
        #print("Case #{}:".format(i),end=" ")
        solve()

main()  