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.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.Arrays;
import java.util.InputMismatchException;

public class PenguinAndMatrix {

    public static void main(String[] args) {
    InputReader in = new InputReader(System.in);
    OutputWriter out = new OutputWriter(System.out);
    int n = in.readInt();
    int m = in.readInt();
    int d = in.readInt();
    int[] arr = new int[n * m];
    int len = arr.length;
    int mid = len / 2;
    for (int i = 0; i < len; i++) {
        arr[i] = in.readInt();
    }
    Arrays.sort(arr);
    int sum = 0;
    boolean check = true;
    /*for (int i = 0; i < mid; i++) {
        int res = arr[mid] - arr[i];
        if (res % d == 0) {
        sum += res / d;
        } else {
        check = false;
        break;
        }
    }
    if (check) {
        for (int i = len - 1; i > mid; i--) {
        int res = arr[i] - arr[mid];
        if (res % d == 0) {
            sum += res / d;
        } else {
            check = false;
            break;
        }

        }
    }*/
    for (int i = 0, j = len - 1; (i < mid || j > mid); i++, j--) {
        if (i < mid) {
        int res = arr[mid] - arr[i];
        if (res % d == 0) {
            sum += res/d;
        } else {
            check = false;
            break;
        }
        }

        if (j > mid) {
        int res = arr[j] - arr[mid];
        if (res % d == 0) {
            sum += res/d;
        } else {
            check = false;
            break;
        }
        }
    }
    if (check)
        out.print(sum);
    else
        out.print(-1);
    out.close();
    }

    static class InputReader {
    private InputStream stream;
    private byte[] buf = new byte[1024];
    private int curChar;
    private int numChars;

    public InputReader(InputStream stream) {
        this.stream = stream;
    }

    public int read() {
        if (numChars == -1)
        throw new InputMismatchException();
        if (curChar >= numChars) {
        curChar = 0;
        try {
            numChars = stream.read(buf);
        } catch (IOException e) {
            throw new InputMismatchException();
        }
        if (numChars <= 0)
            return -1;
        }
        return buf[curChar++];
    }

    public String readLine() {
        int c = read();
        while (isSpaceChar(c))
        c = read();
        StringBuilder res = new StringBuilder();
        do {
        res.appendCodePoint(c);
        c = read();
        } while (!isEndOfLine(c));
        return res.toString();
    }

    public String readString() {
        int c = read();
        while (isSpaceChar(c))
        c = read();
        StringBuilder res = new StringBuilder();
        do {
        res.appendCodePoint(c);
        c = read();
        } while (!isSpaceChar(c));
        return res.toString();
    }

    public long readLong() {
        int c = read();
        while (isSpaceChar(c))
        c = read();
        int sgn = 1;
        if (c == '-') {
        sgn = -1;
        c = read();
        }
        long res = 0;
        do {
        if (c < '0' || c > '9')
            throw new InputMismatchException();
        res *= 10;
        res += c - '0';
        c = read();
        } while (!isSpaceChar(c));
        return res * sgn;
    }

    public int readInt() {
        int c = read();
        while (isSpaceChar(c))
        c = read();
        int sgn = 1;
        if (c == '-') {
        sgn = -1;
        c = read();
        }
        int res = 0;
        do {
        if (c < '0' || c > '9')
            throw new InputMismatchException();
        res *= 10;
        res += c - '0';
        c = read();
        } while (!isSpaceChar(c));
        return res * sgn;
    }

    public boolean isSpaceChar(int c) {
        return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
    }

    public boolean isEndOfLine(int c) {
        return c == '\n' || c == '\r' || c == -1;
    }
    }

    static class OutputWriter {
    private final PrintWriter writer;

    public OutputWriter(OutputStream outputStream) {
        writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
            outputStream)));
    }

    public OutputWriter(Writer writer) {
        this.writer = new PrintWriter(writer);
    }

    public void print(Object... objects) {
        for (int i = 0; i < objects.length; i++) {
        if (i != 0)
            writer.print(' ');
        writer.print(objects[i]);
        }
    }

    public void printLine(Object... objects) {
        print(objects);
        writer.println();
    }

    public void close() {
        writer.close();
    }
    }

}
