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

public class Main
{
    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()); 
        } 
  
        long nextLong() 
        { 
            return Long.parseLong(next()); 
        } 
  
        double nextDouble() 
        { 
            return Double.parseDouble(next()); 
        } 
  
        String nextLine() 
        { 
            String str = ""; 
            try
            { 
                str = br.readLine(); 
            } 
            catch (IOException e) 
            { 
                e.printStackTrace(); 
            } 
            return str; 
        } 
    }

     public static long[] randomize(long arr[])
    {
        Random rand = new Random();
for (int i = 0; i < arr.length; ++i) {
   int index = rand.nextInt(arr.length - i);
   long tmp = arr[arr.length - 1 - i];
   arr[arr.length - 1 - i] = arr[index];
   arr[index] = tmp;
}

return arr;
    } 
    static long mod = 1000000007;

  
    public static void main (String[] args) throws java.lang.Exception
    {
        FastReader sc = new FastReader();
        PrintWriter out = new PrintWriter(System.out);
        
       int n  = sc.nextInt();
       int m   = sc.nextInt();
       int d = sc.nextInt();

       int a[][] = new int[n][m];
       int check =-1;
       boolean doable = true;
       List<Integer> list = new ArrayList<>();

       for(int i=0;i<n;i++)
       {
        for(int j=0;j<m;j++)
        {
            int temp = sc.nextInt();
            a[i][j] =temp;
            if(check==-1)
                check=temp%d;
            else
            {
                if(temp%d!=check)
                    doable = false;
            }

           list.add(temp);
        }
       }

       if(!doable)
       {
        out.println("-1");
        out.flush();
        return;
       }

Collections.sort(list);
int element = list.get(list.size()/2);
  //  System.out.println(list+" "+element);
    int ans =0;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            int diff = (int)Math.abs(a[i][j]-element);
            ans =ans + diff/d;
        }
    }

    out.println(ans);
    out.flush();

    }
    
    
    
    
}
