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
import java.io.*;
import java.util.*;
import java.math.*;

public class Main implements Runnable{
    boolean withFiles = false;


    private void solve() throws IOException{
        int n = in.nextInt();
        int m = in.nextInt();
        int d = in.nextInt();

        int a[][] = new int[n][m];

        for (int i = 0 ; i < n ;i++)
            for (int j = 0; j < m; j++)
                a[i][j] = in.nextInt();


        for (int i = 0; i < n; i++) {
            if (i > 0 && Math.abs(a[i][0] - a[i - 1][0]) % d != 0) {
                System.out.println(-1);
                System.exit(0);
            }
            for (int j = 1; j < m; j++)
                if (Math.abs(a[i][j] - a[i][j - 1]) % d != 0) {
                    System.out.println(-1);
                    System.exit(0);
                }
        }

        int min = 0;
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
                min = Math.min(min, a[i][j]);

        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
                a[i][j] = (a[i][j] - min) / d;

        int ans = 1000000000;
        for (int v = 0; v <= 10000; v++) {
            int cur = 0;
            for (int i = 0; i < n; i++)
                for (int j = 0; j < m; j++)
                    cur += Math.abs(a[i][j] - v);
            ans = Math.min(ans, cur);
        }

        out.println(ans);
    }

    private static class FastScanner{
        private BufferedReader reader;
        private StringTokenizer tokenizer;
        String cache;
        int cur;

        FastScanner(Reader r) throws IOException{
            reader = new BufferedReader(r);
            tokenizer = new StringTokenizer("");
	    cache = null;
	    cur = 0;
        }

        public boolean hasNext(){
            if (cache != null && cur < cache.length())
                return true;
            try {
                while (!tokenizer.hasMoreTokens()){
                    String line = reader.readLine();
                    if (line == null)
                        return false;
                    tokenizer = new StringTokenizer(line);
                }
                cache = tokenizer.nextToken();
		cur = 0;
            }
            catch(IOException e) {
                return false;
            }
            return true;
        }
        
        public char nextChar() throws IOException{
            if (!hasNext())
		throw new EOFException();
	    return cache.charAt(cur++);
        }

        public String next() throws IOException{
            if (!hasNext())
                throw new EOFException();
            String result = (cur == 0) ? cache : cache.substring(cur);
            cache = null;
            return result;
        }

        public int nextInt() throws IOException{
            return Integer.parseInt(this.next());
        }

        public long nextLong() throws IOException{
            return Long.parseLong(this.next());
        }

        public double nextDouble() throws IOException{
            return Double.parseDouble(this.next());
        }

        public BigInteger nextBigInteger() throws IOException{
            return new BigInteger(this.next());
        }

        public void close() throws IOException{
            reader.close();
            tokenizer = new StringTokenizer("");
        }
    }


    public static void main(String args[]) throws IOException{
        new Thread(null, new Main(), "Thread #228", 64 * 1024 * 1024).start();
    }

    public void run(){
        try{
	    if (withFiles) {
		in = new FastScanner(new FileReader("input.txt"));
		out = new PrintWriter(new FileWriter("output.txt"));
	    }
	    else {
		in = new FastScanner(new InputStreamReader(System.in));
		out = new PrintWriter(System.out);
	    }
            solve();
            in.close();
            out.close();
        }
        catch(IOException e){
            e.printStackTrace();
        }
    }

    FastScanner in;
    PrintWriter out;
}
