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.util.*;

import java.io.*;

public class B289 {

	public static void main(String[] args) {

		// TODO Auto-generated method stub

		FastInputReader s = new FastInputReader();
		int n = s.nextInt();
		int m = s.nextInt();
		int d = s.nextInt();
		int[][] arr = new int[n][m];
		int[] freq = new int[n * m];
		int k = 0;
		long sum = 0;
		int a = 0;
		boolean flag = true;
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				arr[i][j] = s.nextInt();
				if(i==0 && j==0) {
					a=arr[i][j]%d;
				}
				if (arr[i][j] % d != a) {
					flag = false;
				} else {
					freq[k++] = arr[i][j];

				}
				
			}
		}
		if (flag == false) {
			System.out.println(-1);
			return;
		}
		Arrays.sort(freq);
		int diff = 0;
		if ((n * m) % 2 == 0) {
			diff = freq[((n * m) / 2) - 1];
		} else {
			diff = freq[(n * m) / 2];
		}
		
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (d == 1) {
					sum += Math.abs(arr[i][j] - diff);
				} else {
					sum += Math.abs((arr[i][j] / d) - (diff/d));
				}
			}
		}
		System.out.println(sum);

	}

	public static class FastInputReader {

		private byte[] buf = new byte[16384];

		private int curChar;

		private int numChars;

		public int read() {

			if (numChars == -1)

				throw new InputMismatchException();

			if (curChar >= numChars) {

				curChar = 0;

				try {

					numChars = System.in.read(buf);

				} catch (IOException e) {

					throw new InputMismatchException();

				}

				if (numChars <= 0)

					return -1;

			}

			return buf[curChar++];

		}

		public String nextLine() {

			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 nextString() {

			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 nextLong() {

			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 nextInt() {

			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 int[] nextIntArray(int n) {

			int[] arr = new int[n];

			for (int i = 0; i < n; i++) {

				arr[i] = nextInt();

			}

			return arr;

		}

		public long[] nextLongArray(int n) {

			long[] arr = new long[n];

			for (int i = 0; i < n; i++) {

				arr[i] = nextLong();

			}

			return arr;

		}

		private boolean isSpaceChar(int c) {

			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;

		}

		private boolean isEndOfLine(int c) {

			return c == '\n' || c == '\r' || c == -1;

		}

	}

	public static class MyScanner {

		BufferedReader reader;

		public MyScanner() {

			this.reader = new BufferedReader(new InputStreamReader(System.in));

		}

		public void close() throws IOException {

			this.reader.close();

		}

		int nextInt() {

			return Integer.parseInt(this.next());

		}

		long nextLong() {

			return Long.parseLong(this.next());

		}

		float nextFloat() {

			return Float.parseFloat(this.next());

		}

		double nextDouble() {

			return Double.parseDouble(this.next());

		}

		String next() {

			String str = "";

			try {

				str = this.reader.readLine().trim();

			} catch (IOException e) {

				e.printStackTrace();

			}

			return str;

		}

		String[] nextStringArray() {

			String[] str = null;

			try {

				str = this.reader.readLine().trim().split(" ");

			} catch (IOException e) {

				e.printStackTrace();

			}

			return str;

		}

		int[] nextIntArray() {

			String[] data = nextStringArray();

			int[] a = new int[data.length];

			for (int i = 0; i < a.length; i++) {

				a[i] = Integer.parseInt(data[i]);

			}

			return a;

		}

		Integer[] nextIntegerArray() {

			String[] data = nextStringArray();

			Integer[] a = new Integer[data.length];

			for (int i = 0; i < a.length; i++) {

				a[i] = Integer.parseInt(data[i]);

			}

			return a;

		}

		long[] nextLongArray() {

			String[] data = nextStringArray();

			long[] a = new long[data.length];

			for (int i = 0; i < a.length; i++) {

				a[i] = Long.parseLong(data[i]);

			}

			return a;

		}

		float[] nextFloatArray() {

			String[] data = nextStringArray();

			float[] a = new float[data.length];

			for (int i = 0; i < a.length; i++) {

				a[i] = Float.parseFloat(data[i]);

			}

			return a;

		}

		double[] nextDoubleArray() {

			String[] data = nextStringArray();

			double[] a = new double[data.length];

			for (int i = 0; i < a.length; i++) {

				a[i] = Double.parseDouble(data[i]);

			}

			return a;

		}

	}

}
