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.io.BufferedReader;
import java.lang.*;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.StringTokenizer;
 
public class Practice{
	
	
	 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;
	        }
	    }
	 
	 static class Pair{
			int min;
			int max;
			Pair(int x,int y){
				min=x;max=y;
			}
		}
			
	 
	    public static void main(String[] args)
	    {
	        FastReader sc = new FastReader();
	        	int n=sc.nextInt();
	        	int m=sc.nextInt();
	        	int k=sc.nextInt();
	        	int arr[][]=new int[n][m];
	        	int ans[]=new int[n*m];
	        	for(int i=0;i<n;i++) {
	        		for(int j=0;j<m;j++) {
	        			arr[i][j]=sc.nextInt();
	        		}
	        	}
	        	int base=arr[0][0];
	        	boolean wrong=false;
	        	int index=0;
	        	
	        	for(int i=0;i<n;i++) {
	        		for(int j=0;j<m;j++) {
	        			double diff=(arr[i][j]-base);
	        			diff=diff/k;
	        			if((int)diff==diff) {
	        				ans[index]=(int)diff;
	        				index++;
	        				
	        			}
	        			else {
	        				wrong=true;
	        				break;
	        			}
	        		}
	        		if(wrong) {
	        			break;
	        			
	        		}
	        	}
	        	
	        	
	        	if(wrong) {			  System.out.print(-1);
}
 
	        	else {
	        		long no=0;
	        		Arrays.sort(ans);
	        		int median=(n*m)/2;
	        		median=ans[median];
	        		for(int i=0;i<n*m;i++) {
	        			no+=Math.abs(ans[i]-median);
	        			}
//	        		for(int i:ans) {
//		        		System.out.print(i+" ");
//
//	        			}
	        		System.out.print(no);
	        	}
	     }
	     
 
}
     
	        	
		