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

public class PolothePenguinandMatrix {
    public static void main(String[] args) throws java.lang.Exception {
        // your code goes here
        try {
            // Scanner sc=new Scanner(System.in);
            FastReader sc = new FastReader();
            int t =1;// sc.nextInt();
            while (t-- > 0) {
                int n=sc.nextInt();
                int m=sc.nextInt();
                int d=sc.nextInt();
                int[] arr=new int[n*m+1];
                boolean dDiff=true;
                for(int i=1;i<=n*m;i++){
                    arr[i]=sc.nextInt();
                    if(i>1 && Math.abs(arr[i]-arr[i-1])%d!=0){
                        dDiff=false;
                    }
                }
                if(n*m==1){
                    System.out.println(0);
                    continue;
                }
                Arrays.sort(arr);
                int mid1=-1;
                int mid2=-1;
                int move=0;
                if(dDiff){
                    if(arr.length%2==0){
                        mid1=arr.length/2;
                        for(int i=1;i<arr.length;i++){
                            move+=((Math.abs(arr[mid1]-arr[i]))/d);
                        }
                        System.out.println(move);
    
                    }
                    else{
                        mid1=arr.length/2;
                        mid2=mid1+1;
                        for(int i=1;i<arr.length;i++){
                            move+=((Math.abs(arr[mid1]-arr[i]))/d);
                        }
                        int move2=0;
                        for(int i=1;i<arr.length;i++){
                            move2+=((Math.abs(arr[mid2]-arr[i]))/d);
                        }
                        move=Math.min(move,move2);
                        System.out.println(move);
                        
                    }
                }
                else{
                    System.out.println(-1);
                }
            }
        } catch (Exception e) {
            return;
        }

    }
    

    static int BS(int[] arr, int l, int r, int element) {
        int low = l;
        int high = r;
        while (high - low > 1) {
            int mid = low + (high - low) / 2;
            if (arr[mid] < element) {
                low = mid + 1;
            } else {
                high = mid;
            }
        }
        if (arr[low] == element) {
            return low;
        } else if (arr[high] == element) {
            return high;
        }
        return -1;
    }

    static int lower_bound(int[] arr, int l, int r, int element) {
        int low = l;
        int high = r;
        while (high - low > 1) {
            int mid = low + (high - low) / 2;
            if (arr[mid] < element) {
                low = mid + 1;
            } else {
                high = mid;
            }
        }
        if (arr[low] >= element) {
            return low;
        } else if (arr[high] >= element) {
            return high;
        }
        return -1;
    }

    static int upper_bound(int[] arr, int l, int r, int element) {
        int low = l;
        int high = r;
        while (high - low > 1) {
            int mid = low + (high - low) / 2;
            if (arr[mid] <= element) {
                low = mid + 1;
            } else {
                high = mid;
            }
        }
        if (arr[low] > element) {
            return low;
        } else if (arr[high] > element) {
            return high;
        }
        return -1;
    }

    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        String nextLine() {
            String s = "";
            try {
                s = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return s;
        }

        Long nextLong() {
            return Long.parseLong(next());
        }
    }
}

/*
 * public static boolean lie(int n,int m,int k){ if(n==1 && m==1 && k==0){
 * return true; } if(n<1 || m<1 || k<0){ return false; } boolean
 * tc=lie(n-1,m,k-m); boolean lc=lie(n,m-1,k-n); if(tc || lc){ return true; }
 * return false; }
 */

