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


public class javaTemplate {
	
// 	 public static final int M = 1000000007 ;
    public static final int M = 1000003 ;
	static FastReader sc = new FastReader();
	//	static Scanner sc = new Scanner(System.in) ;
	
	public static void main(String[] args) {
		
		
		int n = sc.nextInt() ;
		int m = sc.nextInt() ;
		int d = sc.nextInt() ;
		int arr[][] = new int[n][m] ;
		int ans[] = new int[n*m] ;
		int x = 0 ;
		for(int i = 0 ; i< n ; i++) {
			for(int j = 0 ; j< m ; j++) {
				arr[i][j] = sc.nextInt() ;
				ans[x] = arr[i][j] ;
				x ++ ;
			}
		}
		
		Arrays.sort(ans) ;
		int val = ans[(ans.length/2)] ;
		long sum = 0 ;
		boolean valu = true ;
		for(int i = 0 ; i< n ; i++) {
			for(int j = 0 ; j< m ; j++) {
				sum += Math.abs(arr[i][j]-val) ;
			}
		}
		
		System.out.println(sum%d == 0 ? sum/d : "-1");
		
		
	}
	

	





			
	
	
	
	 
	
	


	
	
//_________________________//Template//_____________________________________________________________________
	
	
		//		FAST I/O
		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;
	        }
		}
	
	
		
		
//		Check Perfect Squre
		public static boolean checkPerfectSquare(double number) {   
			
			double sqrt=Math.sqrt(number);   
		
			return ((sqrt - Math.floor(sqrt)) == 0);   
		}   
		
		
		
		
		
	static boolean isPrime(int n) {
	        
	        if (n <= 1) return false;
	  
	        for (int i = 2; i < n; i++) {
	            if (n % i == 0) return false;
	        	}
	  
	        return true;
	    }
		
		
		
		
		// Modulo
		static int mod(int a)
		{
		    return (a%M + M) % M;
		}
		
		
		
		
		
			
			
		
		
		
		//		Palindrom or Not
		static boolean isPalindrome(StringBuilder str, int low, int high) {
					
					while (low < high) {
						if (str.charAt(low) != str.charAt(high)) return false;
					 	low++;
					 	high--;
					}
					return true;
				}
	
	
		// Euler Tortient fx
	static int Phi(int n) {
		
		int count = 0 ;
		for(int i = 1 ; i< n ; i++) {
			if(GCD(i,n) == 1) count ++ ;
		}
		return count ;
	}
	
	
	
		// Integer Sort
		static void sort(int[] a) 
		   {
				ArrayList<Integer> l=new ArrayList<>();
				for (int i:a) l.add(i);
				Collections.sort(l);
				for (int i=0; i<a.length; i++) a[i]=l.get(i);
			}
	
		
		
		
		// Long Sort
		static void sort(long[] a) 
		   {
				ArrayList<Long> l=new ArrayList<>();
				for (long i:a) l.add(i);
				Collections.sort(l);
				for (int i=0; i<a.length; i++) a[i]=l.get(i);
			}
	
	
		
		// boolean Value
		static void value(boolean val)
	    {
	       System.out.println(val ? "YES" : "NO");
	       System.out.flush();
	    }
	
	
		
	
		// GCD
		public static int GCD(int a , int b) {
				if(b == 0) return a ;
				
				return GCD(b, a%b) ;
			}
	
		
		
		
		
		
		// sieve
		public static boolean [] sieveofEratosthenes(int n) {
			
			boolean isPrime[] = new boolean[n+1] ;
			Arrays.fill(isPrime, true);
			isPrime[0] = false ;
			isPrime[1] = false ;
			
			for(int i = 2 ; i * i <= n ; i++) {
				
				for(int j = 2 * i ; j<= n ; j+= i) {
					isPrime[j] = false ;
				}
			}
			
			return isPrime ;
		}
		
		
		
		
		// fastPower
		public static long fastPowerModulo(long a, long b, long n) {
			
			long res = 1 ;
			
			while(b > 0) {
			if((b&1) != 0) {
				res = (res * a % n) % n ;
			}
			
			a = (a % n * a % n) % n ;
			b = b >> 1 ;
			}
			
			return res ;
		}
		
		
		
		
		// check if sorted or not	
		public static boolean isSorted(int[] a)
	    {
	        for (int i = 0; i < a.length - 1; i++)
	        {
	            if (a[i] > a[i + 1]) {
	                return false;
	            }
	        }
	 
	        return true;
	    }
	
	
			//		Palindrom or Not
		static boolean isPalindrome(String str, int low, int high) {
					
					while (low < high) {
						if (str.charAt(low) != str.charAt(high)) return false;
					 	low++;
					 	high--;
					}
					return true;
				}
		
		
		
    	static int LCM(int a, int b)
	    {
	        return (a / GCD(a, b)) * b;
	    }
	

}











