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 collections import deque,Counter,OrderedDict 
import math
import sys
import random
import bisect
from functools import reduce
from sys import stdin
import heapq
import copy
import os
import sys
import operator
from io import BytesIO, IOBase

def main():
    class DisjointSetUnion:
        def __init__(self, n):
            self.parent = list(range(n))
            self.size = [1] * n
            self.num_sets = n

        def find(self, a):
            acopy = a
            while a != self.parent[a]:
                a = self.parent[a]
            while acopy != a:
                self.parent[acopy], acopy = a, self.parent[acopy]
            return a

        def union(self, a, b):
            a, b = self.find(a), self.find(b)
            if a != b:
                if self.size[a] < self.size[b]:
                    a, b = b, a

                self.num_sets -= 1
            self.parent[b] = a
            self.size[a] += self.size[b]

        def set_size(self, a):
            return self.size[self.find(a)]

        def __len__(self):
            return self.num_sets
    n,m,d = map(int, input().split())
    arr = []
    for _ in range(n):
        arr.extend(list(map(int, input().split())))
    ans = True
    for i in arr:
        if (i%d)!=0:ans=False
    if ans:
        pika = sys.maxsize
        for i in range(len(arr)):
            moves = 0
            for j in range(len(arr)):
                if i!=j:
                    moves+=(max(arr[i],arr[j])-min(arr[i],arr[j]))//d
            pika=min(pika,moves)
        print(pika)
    else:
        print(-1)





        











            




        










'''


nnnn

wnn
ww
nnw
nwn
nnnn

5 2
AACAA
ACAAA
5 2
BABBB
ACCDD
'''




    











































# region fastio

BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._file = file
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

# endregion

if __name__ == "__main__":
    main()
