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

public class SolutionB {
   BufferedReader in;
   StringTokenizer str;
   PrintWriter out;
   String SK;
   String next() throws IOException {
      while ((str == null) || (!str.hasMoreTokens())) {
         SK = in.readLine();
         if (SK == null)
              return null;
         str = new StringTokenizer(SK);
      }
      return str.nextToken();
   }
   int nextInt() throws IOException {
      return Integer.parseInt(next());
   }
   double nextDouble() throws IOException {
      return Double.parseDouble(next());
   }
   long nextLong() throws IOException {
      return Long.parseLong(next());
   }
   char[] charArray() throws IOException{
      return next().toCharArray();
   }
   public static void main(String[] args) throws IOException {
      new SolutionB().run();
   }
   void run() throws IOException {
      in = new BufferedReader(new InputStreamReader(System.in));
      out = new PrintWriter(System.out);
      //in = new BufferedReader(new FileReader("input.txt"));
      //out = new PrintWriter("output.txt");
      solve();
      out.close();
   }
   void solve() throws IOException {
      int n=nextInt();
      int m=nextInt();
      int d=nextInt();
      int mat[][]=new int[n][m];
      int max=0;
     

      //int aa=1;
      for(int i=0;i<n;i++){
         for(int j=0;j<m;j++){
            int a=nextInt();
            max=Math.max(a,max);
            mat[i][j]=a;
            
         }
      }
   
          int min=Integer.MAX_VALUE;
          for(int v=1;v<=max;v++){
             int sum=0;
            boolean ok=false;
            for(int i=0;i<n;i++){
               for(int j=0;j<m;j++){
                  int t=Math.abs(mat[i][j]-v);
                  if(t%d==0){
                     sum+=t/d;
                  }
                  else{
                     ok=true;
                     break;
                  }
               }
               if(ok){
                  break;
               }
            }
            if(ok){
               continue;
            }
            min=Math.min(min, sum);
         }
         if(min==Integer.MAX_VALUE)
            out.println(-1);
         else out.println(min);
   
}
   

}
