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 org.w3c.dom.ls.LSOutput;

import java.util.*;
import java.util.function.IntBinaryOperator;


public class Example {
    static long[][] dp;
    //  static    int[]  ar;
    static int mod = 1000000000 + 7;
    static long[] ans = new long[100000 + 1];
    static List<List<Integer>> adj;
    static int[] vis;
    static int[] dis;
    static int row;
    static boolean[] chosen;
    static List<Integer> permutation;
    static int k;
    static int count;
    static int[][] arr;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        int m = sc.nextInt();
      int[] arr= new int[n*m];
      int d=sc.nextInt();
      for(int i=0;i<(m*n);i++){
          arr[i]=sc.nextInt();

      }

      int tot=n*m;
      if(tot==1){
          System.out.println(0);
          return;
      }
    Arrays.sort(arr);
      int ans=0;
      int a=arr[arr.length/2];
      for(int i:arr){
          int x=Math.abs(i-a);
          if(x%d==0){
              ans=ans+x/d;
          }else{
              System.out.println(-1);
              return;
          }
      }
        System.out.println(ans);


    }












    private static void swap(int i, int j, int[] arrr) {
        int temp=arrr[i];
        arrr[i]=arrr[j];
        arrr[j]=temp;
    }

    private static long getsum(int[][] h, int i, int p,long[][] dp) {

        if(i>=h[0].length){
            return 0;
        }
        if(dp[i][p]!=-1){
            return   dp[i][p];
        }

        if(p==0){
            long a=getsum(h,i+1,p,dp);
            a=Math.max(a,h[1][i]+getsum(h,i+1,1,dp));
          a=  Math.max(a,h[2][i]+getsum(h,i+1,2,dp));
            return   dp[i][p]=a;
        }else if(p==1){
            long a=getsum(h,i+1,p,dp);
            //a=Math.max(a,h[i][1]+getsum(h,i+1,1));
            a=  Math.max(a,h[2][i]+getsum(h,i+1,2,dp));
            return   dp[i][p]=a;
        }else{
            long  a=getsum(h,i+1,p,dp);
            a=Math.max(a,h[1][i]+getsum(h,i+1,1,dp));
          //  a=  Math.max(a,h[i][2]+getsum(h,i+1,2));
            return   dp[i][p]=a;
        }

    }

    private static int getans(int[] arr, int i) {
        if(i<0)
            return 0;
        else{
            int min=mod;
            for(int j=0;j<arr.length;j++){
                if(arr[j]==1){
                    arr[j]=2;
                    for(int k=0;k<j;k++){
                        if(arr[k]==0){
                            arr[k]=2;
                            min=Math.min(min,Math.abs(j-k)+getans(arr,i-1));
                        }
                    }
                }
            }
            return min;
        }
    }

    private static int ncr(int notsure, int i) {
        int a=1;
        a=(factorail(notsure))/(factorail(notsure-1)*(factorail(i)));
        return a;
    }

    private static int factorail(int notsure) {
        int fac=1;
        for(int i=2;i<=notsure;i++){
            fac=fac+i;
        }
        return fac;

    }

    private static void allPerMutation(int n) {
        if(permutation.size()==n){
            //   System.out.println(permutation);
            long sum=arr[1][permutation.get(0)];
            for(int i=0;i<permutation.size()-1;i++){
                sum+=arr[permutation.get(i)][permutation.get(i+1)];
            }
            sum=sum+arr[permutation.get(n-1)][1];
            if(sum==k){
                count++;
            }

        }else{
            for(int i=2;i<=(n+1);i++){
                if(chosen[i]){
                    continue;
                }
                permutation.add(i);
                chosen[i]=true;
                allPerMutation(n);
                permutation.remove(permutation.size()-1);
                chosen[i]=false;
            }


        }




    }


    public static int Check(int[] arr,int k){
        int sum=0;
        int j=1;
        int total=0;
        for(int i=1;i<arr.length;i++){
            sum+=arr[i];
            if(sum==k){
                total+=(i-j);
                j=i+1;
                sum=0;
            }if(sum>k){
                return arr.length;
            }
        }
        return total;


    }
    public static  char[][] rotateTheBox(char[][] box) {
        Stack<Character> st;
        int m=box.length;
        int n=box[0].length;
        char[][] cc= new char[box[0].length][box.length];
        int k=0;
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                cc[i][j]='.';
            }
        }
        for(int i=m-1;i>=0;i--){

            st=new Stack<>();
            for(int j=0;j<n;j++){
                char c=box[i][j];
                if(c=='#'){
                    st.push(c);
                }else if(c=='*'){
                    cc[j][k]='*';
                    for(int p=j-1;p>=0;p--){
                        if(!st.isEmpty())
                        cc[p][k]=st.pop();
                        else
                            break;
                    }
                    st=new Stack<>();
                }
            }
            if(!st.isEmpty()){
                for(int p=n-1;p>=0;p--){
                    if(!st.isEmpty())
                        cc[p][k]=st.pop();
                    else
                        break;
                }
            }
            k++;


        }
        return cc;


    }







    private static long findans(int[] arr, int i, int s) {
        if (i < 0 && s == 0)
            return 0;
        if (i < 0 && s == 1)
            return 0;
        if (dp[i][s] != -1) {
            return dp[i][s];
        } else {


            if (s == 0)
                return dp[i][s] = Math.max(findans(arr, i - 1, s), -1 * arr[i] + findans(arr, i - 1, 1 - s));
            else
                return dp[i][s] = Math.max(findans(arr, i - 1, s), arr[i] + findans(arr, i - 1, 1 - s));
        }
    }

    public static void dfs(int v, int di) {
        dis[v] = di;
        vis[v] = 1;
        for (int i = 0; i < adj.get(v).size(); i++) {
            int x = adj.get(v).get(i);
            if (vis[x] == 0) {
                dfs(x, di + 1);
            }
        }
    }

    private static boolean isPrime(long a) {
        if (a == 1)
            return true;

        for (int i = 2; i * i <= a; i++) {
            if (a % i == 0)
                return false;

        }
        return true;
    }

    private static int gcd(int aa, int bb) {
        if (bb == 0)
            return aa;
        return gcd(bb, aa % bb);

    }

    private static int lowerBound(int[] ar, int a) {
        int res = 0;
        int s = 1;
        int e = ar.length - 1;
        while (s <= e) {
            int mid = (s) + (e - s) / 2;
            if (ar[mid] <= a) {
                res = mid;
                s = mid + 1;

            } else {
                e = mid - 1;
            }
        }
        return res;
    }

    public static int getXORSum(int[] arr1, int[] arr2) {

        long c = 0;
        int n = arr1.length;
        int m = arr2.length;
        if (n == 1 && m == 1) {
            return arr1[0] & arr2[0];
        }
        int aa = n + m;
        for (int i = 0; i < 32; i++) {

            int z = 0;
            int o = 0;
            int cc = 0;
            for (int a : arr1) {


                int x = (a & (1 << i));
                if (x == 0)
                    z++;
                else
                    o++;


            }
            for (int a : arr2) {
                int x = (a & (1 << i));
                if (x == 0)
                    z++;
                else
                    o++;
            }

            System.out.println(z + " " + o);

            if (z != aa && o != aa) {
                c = c + (1 << i);

            }


        }
        return (int) c;


    }


}