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
/**
 * Created by ankeet on 12/30/16.
 */
import java.io.*;
import java.util.*;

public class B289 {

    static FastReader in = null;
    static PrintWriter out = null;

    public static void solve()
    {
        int n = in.nextInt();
        int m = in.nextInt();
        int d = in.nextInt();
        int[][] a = new int[n+1][m+1];
        for(int i = 1; i<=n; i++){
            for(int j=1; j<=m; j++){
                a[i][j] = in.nextInt();
            }
        }
        int md = a[1][1] % d;
        boolean can  = true;
        for(int i=1; i<=n; i++) for(int j=1; j<=m; j++) if(a[i][j]%d != md) can = false;
        if (!can) {

            out.println("-1");
            return;
        }

        int[] occ = new int[10010];
        for(int i=1; i<=n; i++ )for(int j=1; j<=m;j++) occ[a[i][j]]++;

        long rsum = 0;
        for(int i=1; i<=10000; i++) rsum += (occ[i]*1L*i);
        long minops = 1<<30;
        long lsum = 0;
        long rocc = n*m;
        long locc = 0;

        for(int i=1; i<=10000; i++){
            if(i%d == md){
                //make all elements equal to i
                long ops = (rsum - rocc * i + locc*i - lsum)/d;
                //out.println(i + " " + ops);
                minops = Math.min(minops, ops);
                lsum += (occ[i] * 1L * i);
                rsum -= (occ[i] * 1L * i);
                rocc -= occ[i];
                locc += occ[i];
            }
        }

        out.println(minops);
    }

    public static void main(String[] args)
    {
        in = new FastReader(System.in);
        out = new PrintWriter(System.out);
        solve();
        out.flush();
        out.close();

    }

    static class FastReader {

        BufferedReader read;
        StringTokenizer tokenizer;

        public FastReader(InputStream in)
        {
            read = new BufferedReader(new InputStreamReader(in));
        }

        public String next()
        {
            while(tokenizer == null || !tokenizer.hasMoreTokens())
            {
                try{
                    tokenizer = new StringTokenizer(read.readLine());
                }catch(Exception e){
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }
        public String nextLine(){

            try
            {
                return read.readLine();
            }
            catch(Exception e)
            {
                throw new RuntimeException(e);
            }
        }
        public int nextInt()
        {
            return Integer.parseInt(next());
        }
        public long nextLong()
        {
            return Long.parseLong(next());
        }
        public double nextDouble()
        {
            return Double.parseDouble(next());
        }

        public int[] nextIntArr(int n)
        {
            int[] a = new int[n];
            for(int i=0; i<n; ++i)
            {
                a[i] = nextInt();
            }
            return a;
        }

        public long[] nextLongArr(int n)
        {
            long[] a = new long[n];
            for(int i=0; i<n; ++i)
            {
                a[i] = nextLong();
            }
            return a;
        }
    }


}


