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 aij.

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.

Input

The first line contains three integers n, m and d (1 ≤ n, m ≤ 100, 1 ≤ d ≤ 104) — 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 aij (1 ≤ aij ≤ 104).

Output

In a single line print a single integer — the minimum number of moves the penguin needs to make all matrix elements equal. If that is impossible, print "-1" (without the quotes).

Examples

Input

2 2 2
2 4
6 8


Output

4


Input

1 2 7
6 7


Output

-1
#ishaans1
#NIT-P 2023
# ==================================================Import Libraries=================================================
import sys
import time
import os
from math import sqrt, log, log2, ceil, log10, gcd, floor, pow, sin, cos, tan, pi, inf, factorial
from copy import copy, deepcopy
from sys import exit, stdin, stdout
from collections import Counter, defaultdict, deque
from itertools import permutations
import heapq
from bisect import bisect_left as bl
# If the element is already present in the list,
# the left most position where element has to be inserted is returned.
from bisect import bisect_right as br
from bisect import bisect
# If the element is already present in the list,
# the right most position where element has to be inserted is r
#=====================================================Global Variables==================================================
# sys.setrecursionlimit(100000000)
mod = 1000000007
# ===================================================Helper Functions=================================================
iinp = lambda: int(sys.stdin.readline())
inp = lambda: sys.stdin.readline().strip()
strl = lambda: list(inp().strip().split(" "))
intl = lambda: list(map(int, inp().split(" ")))
mint = lambda: map(int, inp().split())
flol = lambda: list(map(float, inp().split(" ")))
flush = lambda: stdout.flush()
#====================================================File I/O read=====================================================
# sys.stdin=open('input.txt','r')
# sys.stdout=open('output.txt','w')
#======================================================================================================================
# def permute(nums):
#     def fun(arr, nums, cur, v):
#         if len(cur) == len(nums):
#             arr.append(cur.copy())
#         i = 0
#         while i < len(nums):
#             if v[i]:
#                 i += 1
#                 continue
#             else:
#                 cur.append(nums[i])
#                 v[i] = 1
#                 fun(arr, nums, cur, v)
#                 cur.pop()
#                 v[i] = 0
#                 i += 1
#             # while i<len(nums) and nums[i]==nums[i-1]:i+=1    # Uncomment for unique permutations
#         return arr
 
#     res = []
#     nums.sort()
#     v = [0] * len(nums)
#     return fun(res, nums, [], v)
 
 
# def subsets(res, index, arr, cur):
#     res.append(cur.copy())
#     for i in range(index, len(arr)):
#         cur.append(arr[i])
#         subsets(res, i + 1, arr, cur)
#         cur.pop()
#     return res
 
 
# def sieve(N):
#     root = int(sqrt(N))
#     primes = [1] * (N + 1)
#     primes[0], primes[1] = 0, 0
#     for i in range(2, root + 1):
#         if primes[i]:
#             for j in range(i * i, N + 1, i):
#                 primes[j] = 0
#     return primes
 

# ========================================================Functions====================================================
def solve():

	n,m,d=mint()
	oldmat=[intl() for i in range(n)]
	newmat=[]

	for i in oldmat:
		for j in i:
			newmat.append(j)

	newmat.sort()

	k=newmat[0]%d

	for x in newmat:
		if x%d!=k:
			print(-1)
			return


	comp=newmat[(n*m)//2] #midele

	sm=0

	for i in newmat:
		dif=abs(comp-i)
		sm+=dif//d
	print(sm)




    
    




 
# ========================================================Main Code=====================================================
# t=iinp()
t=1
for _ in range(t):
        solve()
