import java.util.*;
import java.io.*;
public class a
{
	public static void main(String[] arg) throws IOException
	{
		new a();
	}
	public a() throws IOException
	{
		FastScanner in = new FastScanner(System.in);
		int n = in.nextInt(); int m = in.nextInt(); int d = in.nextInt();
		int[][] mat = new int[n][m];
		ArrayList<Integer> list = new ArrayList<Integer>();
		int max = 0;
		for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) list.add(in.nextInt());
		boolean flag = false;
		for(int i = 0; i < n*m; i++)
		{
			if(list.get(0)%d != list.get(i)%d) flag = true;
			max = Math.max(list.get(i), max);
		}
		if(flag)
		{
			System.out.println("-1");
		}
		else
		{
			int ret = Integer.MAX_VALUE;
			for(int i = mat[0][0]%d; i <= max; i+=d)
			{
				int cur = 0;
				for(int j = 0; j < list.size(); j++)
				{
					cur += Math.abs(list.get(j)-i)/d;
				}
				ret = Math.min(ret, cur);
			}
			System.out.println(ret);
		}
		in.close();
	}
	class FastScanner
	{
		BufferedReader br;
		StringTokenizer st;
		public FastScanner(InputStream in)
		{
			br = new BufferedReader(new InputStreamReader(in));
			st = new StringTokenizer("");
		}
		public String next() throws IOException
		{
			while(!st.hasMoreTokens()) st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}
		public int nextInt() throws IOException
		{
			return Integer.parseInt(next());
		}
		public long nextLong() throws IOException
		{
			return Long.parseLong(next());
		}
		public void close() throws IOException
		{
			br.close();
		}
	}
}


import java.util.*;

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

        int n = s.nextInt();
        int m = s.nextInt();
        int d = s.nextInt();
        int a[] = new int[n*m];
        int k = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                int t = s.nextInt();
                a[k] = t;
                k++;
            }
        }
        Arrays.sort(a);
        int l = 0;
        int r = a.length-1;
        int mid = l + (r-l)/2;
        int flag = 0;
        int count = 0;
        int i = 0;
        while(i<mid){
            if(a[i]<=a[mid]){
                if(Math.abs(a[mid]-a[i])%d!=0){
                    flag = -1;
                    break;
                }
                else{
                    count += (a[mid]-a[i])/d;
                }
                i++;
            }
        }
         i = a.length-1;
        while(i>mid){
            if(a[i]>=a[mid]){
                if(Math.abs(a[mid]-a[i])%d!=0){
                    flag = -1;
                    break;
                }
                else{
                    count += (a[i]-a[mid])/d;
                }
                i--;
            }
        }
        if(flag == -1){
            System.out.println(-1);
        }
        else{
            System.out.println(count);
        }
    }
}


import java.util.*;

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

        int n = s.nextInt();
        int m = s.nextInt();
        int d = s.nextInt();
        int a[] = new int[n*m];
        int k = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                int t = s.nextInt();
                a[k] = t;
                k++;
            }
        }
        Arrays.sort(a);
        int l = 0;
        int r = a.length-1;
        int mid = l + (r-l)/2;
        int flag = 0;
        int count = 0;
        int i = 0;
        while(i<mid){
            if(a[i]<a[mid]){
                if(Math.abs(a[mid]-a[i])%d!=0){
                    flag = -1;
                    break;
                }
                else{
                    count += (a[mid]-a[i])/d;
                }
                i++;
            }
        }
         i = a.length-1;
        while(i>mid){
            if(a[i]>a[mid]){
                if(Math.abs(a[mid]-a[i])%d!=0){
                    flag = -1;
                    break;
                }
                else{
                    count += (a[i]-a[mid])/d;
                }
                i--;
            }
        }
        if(flag == -1){
            System.out.println(-1);
        }
        else{
            System.out.println(count);
        }
    }
}


import java.io.*;
import java.util.*;
public class PoloThePenguinAndMatrix
{
	public static void main(String[] args)
	{
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		int m=sc.nextInt();
		int d=sc.nextInt();
		int a[]=new int[n*m];
		int mat[][]=new int[n][m];
		int min=Integer.MAX_VALUE,max=Integer.MIN_VALUE,index=0;
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			{
				mat[i][j]=sc.nextInt();
				a[index++]=mat[i][j];
				if(mat[i][j]>max)
					max=mat[i][j];
				if(mat[i][j]<min)
					min=mat[i][j];
			}
		}
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			{
				if(((max-mat[i][j])%d)!=0)
				{
					System.out.println(-1);
					return;
				}
			}
		}
		int q=0;
		Arrays.sort(a);
		int el=a[a.length/2];
		for(int i=0;i<n*m;i++)
		{
			q+=Math.abs(el-a[i])/2;
		}
		System.out.println(q);
	}
}


import java.io.*;
import java.util.*;
public class PoloThePenguinAndMatrix
{
	public static void main(String[] args)
	{
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		int m=sc.nextInt();
		int d=sc.nextInt();
		int a[]=new int[n*m];
		int mat[][]=new int[n][m];
		int min=Integer.MAX_VALUE,max=Integer.MIN_VALUE,index=0;
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			{
				mat[i][j]=sc.nextInt();
				a[index++]=mat[i][j];
				if(mat[i][j]>max)
					max=mat[i][j];
				if(mat[i][j]<min)
					min=mat[i][j];
			}
		}
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			{
				if(((max-mat[i][j])%d)!=0)
				{
					System.out.println(-1);
					return;
				}
			}
		}
		int q=0;
		Arrays.sort(a);
		int el=a[a.length/2];
		for(int i=0;i<n*m;i++)
		{
			q=q+(Math.abs(el-a[i])/d);
		}
		System.out.println(q);
	}
}


import java.io.*;
import java.util.*;
public class PoloThePenguinAndMatrix
{
	public static void main(String[] args)
	{
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		int m=sc.nextInt();
		int d=sc.nextInt();
		int a[]=new int[n*m];
		int mat[][]=new int[n][m];
		int min=Integer.MAX_VALUE,max=Integer.MIN_VALUE,index=0;
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			{
				mat[i][j]=sc.nextInt();
				a[index++]=mat[i][j];
				if(mat[i][j]>max)
					max=mat[i][j];
				if(mat[i][j]<min)
					min=mat[i][j];
			}
		}
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			{
				if(((max-mat[i][j])%d)!=0)
				{
					System.out.println(-1);
					return;
				}
			}
		}
		int q=0;
		int el=a[a.length/2];
		for(int i=0;i<n*m;i++)
		{
			q+=Math.abs(el-a[i])/2;
		}
		System.out.println(q);
	}
}


import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

/**
 * Created by first on 7/19/2018.
 */
public class PoloThePenguinAndMatrix_B {
    public static void main(String [] args){
        PoloThePenguinAndMatrix_B op = new PoloThePenguinAndMatrix_B();
        op.solve();
    }
    public void  solve(){
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int m = s.nextInt();
        int k = s.nextInt();

        ArrayList<Integer> arr =  new ArrayList<>();
        int num = s.nextInt();
        arr.add(num);
        int diff = num % k;
        for(int i = 1; i < n*m; i++){
            num = s.nextInt();
            if(num%k == diff ) {
                arr.add(num);
            } else {
                System.out.println(-1);
                return;
            }
        }
        Collections.sort(arr);
        int mid = arr.get(n*m/2);
        long NumOfMoves =0;
        for(int i = 0; i < n*m ; i++){
            int f = Math.abs(arr.get(i) - mid) / k;
            NumOfMoves += Math.abs(arr.get(i) - mid) / k;
        }
        System.out.println(NumOfMoves);
    }
}


import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

/**
 * Created by first on 7/19/2018.
 */
public class PoloThePenguinAndMatrix_B {
    public static void main(String [] args){
        PoloThePenguinAndMatrix_B op = new PoloThePenguinAndMatrix_B();
        op.solve();
    }
    public void  solve(){
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int m = s.nextInt();
        int k = s.nextInt();
        ArrayList<Integer> arr =  new ArrayList<>();
        int num = s.nextInt();
        arr.add(num);
        int diff = num % k;
        for(int i = 1; i < n*m; i++){
            num = s.nextInt();
            if(num%k == diff ) {
                arr.add(num);
            } else {
                System.out.println(-1);
                return;
            }
        }
        Collections.sort(arr);
        int mid = arr.get(n*m/2);
        long NumOfMoves =0;
        for(int i = 0; i < n*m ; i++){
            int f = Math.abs(arr.get(i) - mid) / 2;
            NumOfMoves += Math.abs(arr.get(i) - mid) / 2;
        }
        System.out.println(NumOfMoves);
    }
}


import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

/**
 * Created by first on 7/19/2018.
 */
public class PoloThePenguinAndMatrix_B {
    public static void main(String [] args){
        PoloThePenguinAndMatrix_B op = new PoloThePenguinAndMatrix_B();
        op.solve();
    }
    public void  solve(){
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int m = s.nextInt();
        int k = s.nextInt();
        ArrayList<Integer> arr =  new ArrayList<>();
        int num = s.nextInt();
        arr.add(num);
        int diff = num % k;
        for(int i = 1; i < n*m; i++){
            num = s.nextInt();
            if(num%k == diff ) {
                arr.add(num);
            } else {
                System.out.println(-1);
                return;
            }
        }
        Collections.sort(arr);
        int mid = arr.get(n*m/2);
        long NumOfMoves =0;
        for(int i = 0; i < n*m ; i++){
            int f = Math.abs(arr.get(i) - mid) / k;
            NumOfMoves += Math.abs(arr.get(i) - mid) / k;
        }
        System.out.println(NumOfMoves);
    }
}


import java.io.*;
import java.util.*;
import java.math.*;
public class cb {

    InputStream is;
    PrintWriter out;

	static long mod=(long)(1e7+9);
	public static int sol(int a[],int nm,int d)
	{
		int ans=0;
		for(int i=0;i<a.length;i++)
		{
			ans+=Math.abs(a[i]-nm)/d;
		}
		return ans;
	}
	public static int gc(int a,int b)
	{
		if(a==0)
			return b;
		return gc(b%a,a);
	}

    void solve()
    {
		int n=ni(),m=ni(),d=ni();
		int a[]=new int[n*m];
		int gcd=0;
		for(int i=0;i<n*m;i++){
			a[i]=ni();
			gcd=gc(gcd,a[i]);
		}
		//out.println(gcd);
		if(gcd%d!=0){
			out.println(-1);return;}

		Arrays.sort(a);
		int tell=0;
		if(a.length%2==0)
		{
			tell=Math.min(sol(a,a[n/2],d),sol(a,a[n/2-1],d));
		}
		else
			tell=sol(a,a[n/2],d);

		out.println(tell);

	}

	public static long [][] multiply(long a[][],long b[][])
	{
		long mul[][]=new long[2][2];
		for(int i=0;i<2;i++)
		{
			for(int j=0;j<2;j++)
			{
				mul[i][j]=0;
				for(int k=0;k<2;k++)
					mul[i][j]=(mul[i][j]+(a[i][k]*b[k][j]))%mod;
			}
		}
		return mul;
	}

	/* Matrix Exponention*/
	public static long [][] power(long [][] mat,int n)
	{
		long res[][]=new long[2][2];
		res[0][0]=1;res[1][1]=1;
		while(n>0)
		{
			if(n%2==1)
				res=multiply(res,mat);
			mat=multiply(mat,mat);
			n/=2;
		}
		return res;
	}

    //---------- I/O Template ----------

    public static void main(String[] args) { new cb().run(); }
    void run() {
        is = System.in;
        out = new PrintWriter(System.out);
        solve();
        out.flush();
    }

    byte input[] = new byte[1024];
    int len = 0, ptr = 0;

    int readByte() {
        if(ptr >= len) { ptr = 0;
            try { len = is.read(input); }
            catch(IOException e) { throw new InputMismatchException(); }
            if(len <= 0) { return -1; }
        } return input[ptr++];
    }
    boolean isSpaceChar(int c) { return !( c >= 33 && c <= 126 ); }
    int skip() {
        int b = readByte();
        while(b != -1 && isSpaceChar(b)) { b = readByte(); }
        return b;
    }

    char nc() { return (char)skip(); }
    String ns() {
        int b = skip();
        StringBuilder sb = new StringBuilder();
        while(!isSpaceChar(b)) { sb.appendCodePoint(b); b = readByte(); }
        return sb.toString();
    }
    String nLine() {
        int b = skip();
        StringBuilder sb = new StringBuilder();
        while( !(isSpaceChar(b) && b != ' ') ) { sb.appendCodePoint(b); b = readByte(); }
        return sb.toString();
    }
    int ni() {
        int n = 0, b = readByte();
        boolean minus = false;
        while(b != -1 && !( (b >= '0' && b <= '9') || b == '-')) { b = readByte(); }
        if(b == '-') { minus = true; b = readByte(); }
        if(b == -1) { return -1; }  //no input
        while(b >= '0' && b <= '9') { n = n * 10 + (b - '0'); b = readByte(); }
        return minus ? -n : n;
    }
    long nl() {
        long n = 0L;    int b = readByte();
        boolean minus = false;
        while(b != -1 && !( (b >= '0' && b <= '9') || b == '-')) { b = readByte(); }
        if(b == '-') { minus = true; b = readByte(); }
        while(b >= '0' && b <= '9') { n = n * 10 + (b - '0'); b = readByte(); }
        return minus ? -n : n;
    }

    double nd() { return Double.parseDouble(ns()); }
    float nf() { return Float.parseFloat(ns()); }
    int[] na(int n) {
        int a[] = new int[n];
        for(int i = 0; i < n; i++) { a[i] = ni(); }
        return a;
    }
    char[] ns(int n) {
        char c[] = new char[n];
        int i, b = skip();
        for(i = 0; i < n; i++) {
            if(isSpaceChar(b)) { break; }
            c[i] = (char)b; b = readByte();
        } return i == n ? c : Arrays.copyOf(c,i);
    }

}


import java.io.*;
import java.util.*;
import java.math.*;
public class cb {

    InputStream is;
    PrintWriter out;

	static long mod=(long)(1e7+9);
	public static int sol(int a[],int nm,int d)
	{
		int ans=0;
		//System.out.println(nm+" k");
		//System.out.println(nm+" k");
		for(int i=0;i<a.length;i++)
		{
			ans+=Math.abs(a[i]-nm)/d;
		}
		return ans;
	}
	public static int gc(int a,int b)
	{
		if(a==0)
			return b;
		return gc(b%a,a);
	}

    void solve()
    {
		int n=ni(),m=ni(),d=ni();
		int a[]=new int[n*m];
		int gcd=0;
		for(int i=0;i<n*m;i++){
			a[i]=ni();
			gcd=gc(gcd,a[i]);
		}
		//out.println(gcd);
		if(gcd%d!=0){
			out.println(-1);return;}

		Arrays.sort(a);
		int tell=0;
		n=n*m;
		if(a.length%2==0)
		{
			//out.println(a[n/2]+" ee");
			tell=Math.min(sol(a,a[n/2],d),sol(a,a[n/2-1],d));
		}
		else
			tell=sol(a,a[n/2],d);

		out.println(tell);

	}

	public static long [][] multiply(long a[][],long b[][])
	{
		long mul[][]=new long[2][2];
		for(int i=0;i<2;i++)
		{
			for(int j=0;j<2;j++)
			{
				mul[i][j]=0;
				for(int k=0;k<2;k++)
					mul[i][j]=(mul[i][j]+(a[i][k]*b[k][j]))%mod;
			}
		}
		return mul;
	}

	/* Matrix Exponention*/
	public static long [][] power(long [][] mat,int n)
	{
		long res[][]=new long[2][2];
		res[0][0]=1;res[1][1]=1;
		while(n>0)
		{
			if(n%2==1)
				res=multiply(res,mat);
			mat=multiply(mat,mat);
			n/=2;
		}
		return res;
	}

    //---------- I/O Template ----------

    public static void main(String[] args) { new cb().run(); }
    void run() {
        is = System.in;
        out = new PrintWriter(System.out);
        solve();
        out.flush();
    }

    byte input[] = new byte[1024];
    int len = 0, ptr = 0;

    int readByte() {
        if(ptr >= len) { ptr = 0;
            try { len = is.read(input); }
            catch(IOException e) { throw new InputMismatchException(); }
            if(len <= 0) { return -1; }
        } return input[ptr++];
    }
    boolean isSpaceChar(int c) { return !( c >= 33 && c <= 126 ); }
    int skip() {
        int b = readByte();
        while(b != -1 && isSpaceChar(b)) { b = readByte(); }
        return b;
    }

    char nc() { return (char)skip(); }
    String ns() {
        int b = skip();
        StringBuilder sb = new StringBuilder();
        while(!isSpaceChar(b)) { sb.appendCodePoint(b); b = readByte(); }
        return sb.toString();
    }
    String nLine() {
        int b = skip();
        StringBuilder sb = new StringBuilder();
        while( !(isSpaceChar(b) && b != ' ') ) { sb.appendCodePoint(b); b = readByte(); }
        return sb.toString();
    }
    int ni() {
        int n = 0, b = readByte();
        boolean minus = false;
        while(b != -1 && !( (b >= '0' && b <= '9') || b == '-')) { b = readByte(); }
        if(b == '-') { minus = true; b = readByte(); }
        if(b == -1) { return -1; }  //no input
        while(b >= '0' && b <= '9') { n = n * 10 + (b - '0'); b = readByte(); }
        return minus ? -n : n;
    }
    long nl() {
        long n = 0L;    int b = readByte();
        boolean minus = false;
        while(b != -1 && !( (b >= '0' && b <= '9') || b == '-')) { b = readByte(); }
        if(b == '-') { minus = true; b = readByte(); }
        while(b >= '0' && b <= '9') { n = n * 10 + (b - '0'); b = readByte(); }
        return minus ? -n : n;
    }

    double nd() { return Double.parseDouble(ns()); }
    float nf() { return Float.parseFloat(ns()); }
    int[] na(int n) {
        int a[] = new int[n];
        for(int i = 0; i < n; i++) { a[i] = ni(); }
        return a;
    }
    char[] ns(int n) {
        char c[] = new char[n];
        int i, b = skip();
        for(i = 0; i < n; i++) {
            if(isSpaceChar(b)) { break; }
            c[i] = (char)b; b = readByte();
        } return i == n ? c : Arrays.copyOf(c,i);
    }

}


import java.io.*;
import java.util.*;
import java.math.*;
public class cb {

    InputStream is;
    PrintWriter out;

	static long mod=(long)(1e7+9);
	public static int sol(int a[],int nm,int d)
	{
		int ans=0;
		//System.out.println(nm+" k");
		//System.out.println(nm+" k");
		for(int i=0;i<a.length;i++)
		{
			ans+=Math.abs(a[i]-nm)/d;
		}
		return ans;
	}
	public static int gc(int a,int b)
	{
		if(a==0)
			return b;
		return gc(b%a,a);
	}

    void solve()
    {
		int n=ni(),m=ni(),d=ni();
		int a[]=new int[n*m];
		int gcd=0;
		for(int i=0;i<n*m;i++){
			a[i]=ni();
		}

		int f=0;
		Arrays.sort(a);
		for(int i=1;i<n*m;i++)
		{
			if((a[i]-a[i-1])%d!=0){f=1;break;}
		}

		if(f==1){
			out.println(-1);return;}

		int tell=0;
		n=n*m;
		if(a.length%2==0)
		{
			//out.println(a[n/2]+" ee");
			tell=Math.min(sol(a,a[n/2],d),sol(a,a[n/2-1],d));
		}
		else
			tell=sol(a,a[n/2],d);

		out.println(tell);

	}

	public static long [][] multiply(long a[][],long b[][])
	{
		long mul[][]=new long[2][2];
		for(int i=0;i<2;i++)
		{
			for(int j=0;j<2;j++)
			{
				mul[i][j]=0;
				for(int k=0;k<2;k++)
					mul[i][j]=(mul[i][j]+(a[i][k]*b[k][j]))%mod;
			}
		}
		return mul;
	}

	/* Matrix Exponention*/
	public static long [][] power(long [][] mat,int n)
	{
		long res[][]=new long[2][2];
		res[0][0]=1;res[1][1]=1;
		while(n>0)
		{
			if(n%2==1)
				res=multiply(res,mat);
			mat=multiply(mat,mat);
			n/=2;
		}
		return res;
	}

    //---------- I/O Template ----------

    public static void main(String[] args) { new cb().run(); }
    void run() {
        is = System.in;
        out = new PrintWriter(System.out);
        solve();
        out.flush();
    }

    byte input[] = new byte[1024];
    int len = 0, ptr = 0;

    int readByte() {
        if(ptr >= len) { ptr = 0;
            try { len = is.read(input); }
            catch(IOException e) { throw new InputMismatchException(); }
            if(len <= 0) { return -1; }
        } return input[ptr++];
    }
    boolean isSpaceChar(int c) { return !( c >= 33 && c <= 126 ); }
    int skip() {
        int b = readByte();
        while(b != -1 && isSpaceChar(b)) { b = readByte(); }
        return b;
    }

    char nc() { return (char)skip(); }
    String ns() {
        int b = skip();
        StringBuilder sb = new StringBuilder();
        while(!isSpaceChar(b)) { sb.appendCodePoint(b); b = readByte(); }
        return sb.toString();
    }
    String nLine() {
        int b = skip();
        StringBuilder sb = new StringBuilder();
        while( !(isSpaceChar(b) && b != ' ') ) { sb.appendCodePoint(b); b = readByte(); }
        return sb.toString();
    }
    int ni() {
        int n = 0, b = readByte();
        boolean minus = false;
        while(b != -1 && !( (b >= '0' && b <= '9') || b == '-')) { b = readByte(); }
        if(b == '-') { minus = true; b = readByte(); }
        if(b == -1) { return -1; }  //no input
        while(b >= '0' && b <= '9') { n = n * 10 + (b - '0'); b = readByte(); }
        return minus ? -n : n;
    }
    long nl() {
        long n = 0L;    int b = readByte();
        boolean minus = false;
        while(b != -1 && !( (b >= '0' && b <= '9') || b == '-')) { b = readByte(); }
        if(b == '-') { minus = true; b = readByte(); }
        while(b >= '0' && b <= '9') { n = n * 10 + (b - '0'); b = readByte(); }
        return minus ? -n : n;
    }

    double nd() { return Double.parseDouble(ns()); }
    float nf() { return Float.parseFloat(ns()); }
    int[] na(int n) {
        int a[] = new int[n];
        for(int i = 0; i < n; i++) { a[i] = ni(); }
        return a;
    }
    char[] ns(int n) {
        char c[] = new char[n];
        int i, b = skip();
        for(i = 0; i < n; i++) {
            if(isSpaceChar(b)) { break; }
            c[i] = (char)b; b = readByte();
        } return i == n ? c : Arrays.copyOf(c,i);
    }

}


import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.*;

public final class PoloThePenguinAndMatrix
{
    public static void main(String[] args)
    {
        new PoloThePenguinAndMatrix(System.in, System.out);
    }

    private static class Solver implements Runnable
    {
        // BufferedReader in;
        int n, m, d, size;
        int[][] mat;
        int[] arr, cnt;
        PriorityQueue<Integer> minQueue, maxQueue;
        InputReader in;
        PrintWriter out;

        void solve() throws IOException
        {
            n = in.nextInt();
            m = in.nextInt();
            d = in.nextInt();
            size = n * m;
            mat = new int[n][m];
            arr = new int[size];
            cnt = new int[(int) 1e4 + 5];
            minQueue = new PriorityQueue<>();
            maxQueue = new PriorityQueue<>(Collections.reverseOrder());

            int ctr = 0;

            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < m; j++)
                {
                    mat[i][j] = in.nextInt();
                    arr[ctr++] = mat[i][j];
                    cnt[mat[i][j]]++;
                    minQueue.add(mat[i][j]);
                    maxQueue.add(mat[i][j]);
                }
            }

            Arrays.sort(arr);

            int med = arr[size / 2];
            int ans = 0;

            System.out.println("med : " + med);

            for (int i = 0; i < size; i++)
            {
                int diff = Math.abs(med - arr[i]);
//                System.out.println("i : " + i + ", arr[i] : " + arr[i] + ", diff : " + diff);

                if (diff % d != 0)
                {
                    out.println(-1);

                    return;
                }

                ans += diff / d;
            }

            out.println(ans);
        }

        // uncomment below line to change to BufferedReader
        // public Solver(BufferedReader in, PrintWriter out)
        Solver(InputReader in, PrintWriter out)
        {
            this.in = in;
            this.out = out;
        }

        @Override
        public void run()
        {
            try
            {
                solve();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }

    private static class InputReader
    {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;

        int read()
        {
            if (numChars == -1)
                throw new InputMismatchException();

            if (curChar >= numChars)
            {
                curChar = 0;
                try
                {
                    numChars = stream.read(buf);
                }
                catch (IOException e)
                {
                    throw new InputMismatchException();
                }
                if (numChars <= 0)
                    return -1;
            }

            return buf[curChar++];
        }

        int nextInt()
        {
            int c = read();

            while (isSpaceChar(c))
                c = read();

            int sgn = 1;

            if (c == '-')
            {
                sgn = -1;
                c = read();
            }

            int res = 0;

            do
            {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();

                res *= 10;
                res += c & 15;

                c = read();
            } while (!isSpaceChar(c));

            return res * sgn;
        }

        boolean isSpaceChar(int c)
        {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        void close()
        {
            try
            {
                stream.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }

        InputReader(InputStream stream)
        {
            this.stream = stream;
        }

    }

    private PoloThePenguinAndMatrix(InputStream inputStream, OutputStream outputStream)
    {
        // uncomment below line to change to BufferedReader
        // BufferedReader in = new BufferedReader(new
        // InputStreamReader(inputStream));
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        Thread thread = new Thread(null, new Solver(in, out), "Solver", 1 << 29);

        thread.start();
        try
        {
            thread.join();
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }

        in.close();
        out.flush();
        out.close();
    }

}


import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.*;

public final class PoloThePenguinAndMatrix
{
    public static void main(String[] args)
    {
        new PoloThePenguinAndMatrix(System.in, System.out);
    }

    private static class Solver implements Runnable
    {
        // BufferedReader in;
        int n, m, d, size;
        int[] arr;
        InputReader in;
        PrintWriter out;

        void solve() throws IOException
        {
            n = in.nextInt();
            m = in.nextInt();
            d = in.nextInt();
            size = n * m;
            arr = new int[size];

            int ctr = 0;

            for (int i = 0; i < n; i++)
                for (int j = 0; j < m; j++)
                    arr[ctr++] = in.nextInt();

            Arrays.sort(arr);

            int med = arr[size / 2];
            int ans = 0;

            for (int i = 0; i < size; i++)
            {
                int diff = Math.abs(med - arr[i]);

                if (diff % d != 0)
                {
                    out.println(-1);

                    return;
                }

                ans += diff / d;
            }

            out.println(ans);
        }

        // uncomment below line to change to BufferedReader
        // public Solver(BufferedReader in, PrintWriter out)
        Solver(InputReader in, PrintWriter out)
        {
            this.in = in;
            this.out = out;
        }

        @Override
        public void run()
        {
            try
            {
                solve();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }

    private static class InputReader
    {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;

        int read()
        {
            if (numChars == -1)
                throw new InputMismatchException();

            if (curChar >= numChars)
            {
                curChar = 0;
                try
                {
                    numChars = stream.read(buf);
                }
                catch (IOException e)
                {
                    throw new InputMismatchException();
                }
                if (numChars <= 0)
                    return -1;
            }

            return buf[curChar++];
        }

        int nextInt()
        {
            int c = read();

            while (isSpaceChar(c))
                c = read();

            int sgn = 1;

            if (c == '-')
            {
                sgn = -1;
                c = read();
            }

            int res = 0;

            do
            {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();

                res *= 10;
                res += c & 15;

                c = read();
            } while (!isSpaceChar(c));

            return res * sgn;
        }

        boolean isSpaceChar(int c)
        {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        void close()
        {
            try
            {
                stream.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }

        InputReader(InputStream stream)
        {
            this.stream = stream;
        }

    }

    private PoloThePenguinAndMatrix(InputStream inputStream, OutputStream outputStream)
    {
        // uncomment below line to change to BufferedReader
        // BufferedReader in = new BufferedReader(new
        // InputStreamReader(inputStream));
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        Thread thread = new Thread(null, new Solver(in, out), "Solver", 1 << 29);

        thread.start();
        try
        {
            thread.join();
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }

        in.close();
        out.flush();
        out.close();
    }

}


import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.*;

public final class PoloThePenguinAndMatrix
{
    public static void main(String[] args)
    {
        new PoloThePenguinAndMatrix(System.in, System.out);
    }

    private static class Solver implements Runnable
    {
        // BufferedReader in;
        int n, m, d, size;
        int[][] mat;
        int[] arr, cnt;
        PriorityQueue<Integer> minQueue, maxQueue;
        InputReader in;
        PrintWriter out;

        void solve() throws IOException
        {
            n = in.nextInt();
            m = in.nextInt();
            d = in.nextInt();
            size = n * m;
            mat = new int[n][m];
            arr = new int[size];
            cnt = new int[(int) 1e4 + 5];
            minQueue = new PriorityQueue<>();
            maxQueue = new PriorityQueue<>(Collections.reverseOrder());

            int ctr = 0;

            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < m; j++)
                {
                    mat[i][j] = in.nextInt();
                    arr[ctr++] = mat[i][j];
                    cnt[mat[i][j]]++;
                    minQueue.add(mat[i][j]);
                    maxQueue.add(mat[i][j]);
                }
            }

            Arrays.sort(arr);

            int med = arr[size / 2];
            int ans = 0;

//            System.out.println("med : " + med);

            for (int i = 0; i < size; i++)
            {
                int diff = Math.abs(med - arr[i]);
//                System.out.println("i : " + i + ", arr[i] : " + arr[i] + ", diff : " + diff);

                if (diff % d != 0)
                {
                    out.println(-1);

                    return;
                }

                ans += diff / d;
            }

            out.println(ans);
        }

        // uncomment below line to change to BufferedReader
        // public Solver(BufferedReader in, PrintWriter out)
        Solver(InputReader in, PrintWriter out)
        {
            this.in = in;
            this.out = out;
        }

        @Override
        public void run()
        {
            try
            {
                solve();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }

    private static class InputReader
    {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;

        int read()
        {
            if (numChars == -1)
                throw new InputMismatchException();

            if (curChar >= numChars)
            {
                curChar = 0;
                try
                {
                    numChars = stream.read(buf);
                }
                catch (IOException e)
                {
                    throw new InputMismatchException();
                }
                if (numChars <= 0)
                    return -1;
            }

            return buf[curChar++];
        }

        int nextInt()
        {
            int c = read();

            while (isSpaceChar(c))
                c = read();

            int sgn = 1;

            if (c == '-')
            {
                sgn = -1;
                c = read();
            }

            int res = 0;

            do
            {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();

                res *= 10;
                res += c & 15;

                c = read();
            } while (!isSpaceChar(c));

            return res * sgn;
        }

        boolean isSpaceChar(int c)
        {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        void close()
        {
            try
            {
                stream.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }

        InputReader(InputStream stream)
        {
            this.stream = stream;
        }

    }

    private PoloThePenguinAndMatrix(InputStream inputStream, OutputStream outputStream)
    {
        // uncomment below line to change to BufferedReader
        // BufferedReader in = new BufferedReader(new
        // InputStreamReader(inputStream));
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        Thread thread = new Thread(null, new Solver(in, out), "Solver", 1 << 29);

        thread.start();
        try
        {
            thread.join();
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }

        in.close();
        out.flush();
        out.close();
    }

}


import java.io.*;
import java.lang.reflect.Array;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException {
        InputReader in = new InputReader();
        PrintWriter out = new PrintWriter(System.out);
        int test_cases = 1;
        Solver s = new Solver();
        for (int i = 1; i <= test_cases; i++) {
            s.solve(i, in, out);
        }
        out.close();
    }
}

class Solver {
    int n, m, d;
    int[][] a;
    void solve(int test_number, InputReader in, PrintWriter out) throws IOException {
        long start = System.currentTimeMillis();
        n = in.nextInt();
        m = in.nextInt();
        d = in.nextInt();
        a = new int[n][m];
        ArrayList<Integer> v = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                a[i][j] = in.nextInt();
                v.add(a[i][j]);
            }
        }
        Collections.sort(v);
        int median = 0;
        if (v.size() > 2) {
            median = (v.get(v.size() / 2) + v.get(v.size() / 2 - 1)) / 2;
        }
        else {
            if (v.size() == 1) {
                median = v.get(0);
            } else {
                median = (v.get(0) + v.get(1)) / 2;
            }
        }
        long ans = Long.MAX_VALUE;
        for (int i = Math.max(median - 200, 1); i <= median + 200; i++) {
            ans = Math.min(ans, func(i));
        }
        if (ans == Long.MAX_VALUE) ans = -1;
        out.println(ans);
    }

    private long func(int val) {
        long need = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (Math.abs(a[i][j] - val) % d != 0) {
                    return Long.MAX_VALUE;
                }
                need += Math.abs(a[i][j] - val) / d;
            }
        }
        return need;
    }
}

class InputReader {
    BufferedReader br;
    StringTokenizer st;

    public InputReader()
    {
        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;
    }
}


import java.io.*;
import java.lang.reflect.Array;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException {
        InputReader in = new InputReader();
        PrintWriter out = new PrintWriter(System.out);
        int test_cases = 1;
        Solver s = new Solver();
        for (int i = 1; i <= test_cases; i++) {
            s.solve(i, in, out);
        }
        out.close();
    }
}

class Solver {
    int n, m, d;
    int[][] a;
    void solve(int test_number, InputReader in, PrintWriter out) throws IOException {
        long start = System.currentTimeMillis();
        n = in.nextInt();
        m = in.nextInt();
        d = in.nextInt();
        a = new int[n][m];
        ArrayList<Integer> v = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                a[i][j] = in.nextInt();
                v.add(a[i][j]);
            }
        }
        Collections.sort(v);
        int median = (v.get(v.size() / 2) + v.get(v.size() / 2 - 1)) / 2;
        long ans = Long.MAX_VALUE;
        for (int i = Math.max(median - 100, 0); i <= median + 100; i++) {
            ans = Math.min(ans, func(i));
        }
        if (ans == Long.MAX_VALUE) ans = -1;
        out.println(ans);
    }

    private long func(int val) {
        long need = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (Math.abs(a[i][j] - val) % d != 0) {
                    return Long.MAX_VALUE;
                }
                need += Math.abs(a[i][j] - val) / d;
            }
        }
        return need;
    }
}

class InputReader {
    BufferedReader br;
    StringTokenizer st;

    public InputReader()
    {
        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;
    }
}


import java.io.*;
import java.lang.reflect.Array;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException {
        InputReader in = new InputReader();
        PrintWriter out = new PrintWriter(System.out);
        int test_cases = 1;
        Solver s = new Solver();
        for (int i = 1; i <= test_cases; i++) {
            s.solve(i, in, out);
        }
        out.close();
    }
}

class Solver {
    int n, m, d;
    int[][] a;
    void solve(int test_number, InputReader in, PrintWriter out) throws IOException {
        n = in.nextInt();
        m = in.nextInt();
        d = in.nextInt();
        a = new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                a[i][j] = in.nextInt();
            }
        }
        long ans = Long.MAX_VALUE;
        for (int i = 1; i <= 1e4; i++) {
            ans = Math.min(ans, func(i));
        }
        if (ans == Long.MAX_VALUE) ans = -1;
        out.println(ans);
    }

    private long func(int val) {
        long need = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (Math.abs(a[i][j] - val) % d != 0) {
                    return Long.MAX_VALUE;
                }
                need += Math.abs(a[i][j] - val) / d;
            }
        }
        return need;
    }
}

class InputReader {
    BufferedReader br;
    StringTokenizer st;

    public InputReader()
    {
        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;
    }
}


import java.io.*;
import java.lang.reflect.Array;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException {
        InputReader in = new InputReader();
        PrintWriter out = new PrintWriter(System.out);
        int test_cases = 1;
        Solver s = new Solver();
        for (int i = 1; i <= test_cases; i++) {
            s.solve(i, in, out);
        }
        out.close();
    }
}

class Solver {
    int n, m, d;
    int[][] a;
    void solve(int test_number, InputReader in, PrintWriter out) throws IOException {
        long start = System.currentTimeMillis();
        n = in.nextInt();
        m = in.nextInt();
        d = in.nextInt();
        a = new int[n][m];
        ArrayList<Integer> v = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                a[i][j] = in.nextInt();
                v.add(a[i][j]);
            }
        }
        Collections.sort(v);
        int median = (v.get(v.size() / 2) + v.get(v.size() / 2 - 1)) / 2;
        long ans = Long.MAX_VALUE;
        for (int i = Math.max(median - 200, 1); i <= median + 200; i++) {
            ans = Math.min(ans, func(i));
        }
        if (ans == Long.MAX_VALUE) ans = -1;
        out.println(ans);
    }

    private long func(int val) {
        long need = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (Math.abs(a[i][j] - val) % d != 0) {
                    return Long.MAX_VALUE;
                }
                need += Math.abs(a[i][j] - val) / d;
            }
        }
        return need;
    }
}

class InputReader {
    BufferedReader br;
    StringTokenizer st;

    public InputReader()
    {
        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;
    }
}


/**
 * Created by mypc on 8/3/17.
 */

import java.io.*;
import java.util.*;

public class Q95 {
    static PrintWriter pw = new PrintWriter(System.out);

    public static void main(String args[]) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        //BufferedReader br = new BufferedReader(new FileReader("testcase.txt"));
        StringTokenizer st;
        st = new StringTokenizer(br.readLine());
        int n = Integer.parseInt(st.nextToken());
        int m = Integer.parseInt(st.nextToken());
        int d = Integer.parseInt(st.nextToken());

        int k = n*m;
        int a[] = new int[k];
        int c = 0;
        for(int i = 0 ; i < n ; i++)
        {
            st = new StringTokenizer(br.readLine());
            for(int j = 0 ; j < m ; j++)
            a[c++] = Integer.parseInt(st.nextToken());
        }

        int rem = a[0] % d;
        boolean ans = true;
        for(int i = 0 ; i < k ; i++)
            if(a[i] % d != rem){
                ans = false;
                break;
            }

        if(ans)
        {
            int total = 0;
            int equal = a[k/2];
            for(int i = 0 ; i < k ; i++)
                total += Math.abs(a[i] - equal);
            total /= d;
            pw.println(total);
        }
        pw.println(-1);

        pw.flush();
    }
}


/**
 * Created by mypc on 8/3/17.
 */

import java.io.*;
import java.util.*;

public class Q95 {
    static PrintWriter pw = new PrintWriter(System.out);

    public static void main(String args[]) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        //BufferedReader br = new BufferedReader(new FileReader("testcase.txt"));
        StringTokenizer st;
        st = new StringTokenizer(br.readLine());
        int n = Integer.parseInt(st.nextToken());
        int m = Integer.parseInt(st.nextToken());
        int d = Integer.parseInt(st.nextToken());

        int k = n*m;
        int a[] = new int[k];
        int c = 0;
        for(int i = 0 ; i < n ; i++)
        {
            st = new StringTokenizer(br.readLine());
            for(int j = 0 ; j < m ; j++)
            a[c++] = Integer.parseInt(st.nextToken());
        }

        int rem = a[0] % d;
        boolean ans = true;
        for(int i = 0 ; i < k ; i++)
            if(a[i] % d != rem){
                ans = false;
                break;
            }

        if(ans)
        {
            int total = 0;
            int equal = a[k/2];
            for(int i = 0 ; i < k ; i++)
                total += Math.abs(a[i] - equal);
            total /= d;
            pw.println(total);
        }
        else
        pw.println(-1);

        pw.flush();
    }
}


/**
 * Created by mypc on 8/3/17.
 */

import java.io.*;
import java.util.*;

public class Q95 {
    static PrintWriter pw = new PrintWriter(System.out);

    public static void main(String args[]) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        //BufferedReader br = new BufferedReader(new FileReader("testcase.txt"));
        StringTokenizer st;
        st = new StringTokenizer(br.readLine());
        int n = Integer.parseInt(st.nextToken());
        int m = Integer.parseInt(st.nextToken());
        int d = Integer.parseInt(st.nextToken());

        int k = n*m;
        int a[] = new int[k];
        int c = 0;
        for(int i = 0 ; i < n ; i++)
        {
            st = new StringTokenizer(br.readLine());
            for(int j = 0 ; j < m ; j++)
            a[c++] = Integer.parseInt(st.nextToken());
        }

        int rem = a[0] % d;
        boolean ans = true;
        for(int i = 0 ; i < k ; i++)
            if(a[i] % d != rem){
                ans = false;
                break;
            }

        Arrays.sort(a);
        if(ans)
        {
            int total = 0;
            int equal = a[k/2];
            for(int i = 0 ; i < k ; i++)
                total += Math.abs(a[i] - equal);
            total /= d;
            pw.println(total);
        }
        else
        pw.println(-1);

        pw.flush();
    }
}


import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main{
	public static void main(String[]ar0gs)throws IOException{
		BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
		String s=bf.readLine();
		String[]sa=s.split(" ");
		int n=Integer.parseInt(sa[0]);
		int m=Integer.parseInt(sa[1]);
		int d=Integer.parseInt(sa[2]);
		int[][]a=new int[n][m];
		int[][] b=new int[m*n+1][m*n+1];
		boolean flag=true;
		for(int i=0;i<n;i++){
			s=bf.readLine();
			sa=s.split(" ");
			for(int j=0;j<m;j++){
				a[i][j]=Integer.parseInt(sa[j]);
				b[i*m+j+1][0]=a[i][j];
				b[0][i*m+j+1]=a[i][j];
				if((a[i][j]-a[0][0])%d!=0){
					flag=false;
					break;
				}
			}
			if(!flag)
				break;
		}
		if(!flag)
			System.out.println(-1);
		else{
			int r=100000000;
			for(int i=1;i<=m*n;i++){
				int sum=0;
				for(int j=1;j<=m*n;j++){
					b[i][j]=Math.abs(b[i][0]-b[0][j])/d;
					sum+=b[i][j];
				}
				if(sum<r)
					r=sum;
			}
			System.out.println(r);
		}
	}
}


import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main{
	public static void main(String[]ar0gs)throws IOException{
		BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
		String s=bf.readLine();
		String[]sa=s.split(" ");
		int n=Integer.parseInt(sa[0]);
		int m=Integer.parseInt(sa[1]);
		int d=Integer.parseInt(sa[2]);
		int[] b=new int[m*n];
		boolean flag=true;
		for(int i=0;i<n;i++){
			s=bf.readLine();
			sa=s.split(" ");
			for(int j=0;j<m;j++){
				b[i*m+j]=Integer.parseInt(sa[j]);
				if((b[i*m+j]-b[0])%d!=0){
					flag=false;
					break;
				}
			}
			if(!flag)
				break;
		}
		if(!flag)
			System.out.println(-1);
		else{
			int r=100000000;
			for(int i=0;i<m*n;i++){
				int sum=0;
				for(int j=0;j<m*n;j++){
					sum+=Math.abs(b[j]-b[i])/d;
				}
				if(sum<r)
					r=sum;
			}
			System.out.println(r);
		}
	}
}


import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main{
	public static void main(String[]ar0gs)throws IOException{
		BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
		String s=bf.readLine();
		String[]sa=s.split(" ");
		int n=Integer.parseInt(sa[0]);
		int m=Integer.parseInt(sa[1]);
		int d=Integer.parseInt(sa[2]);
		int[][]a=new int[n][m];
		double sum=0;
		boolean flag=true;
		for(int i=0;i<n;i++){
			s=bf.readLine();
			sa=s.split(" ");
			for(int j=0;j<m;j++){
				a[i][j]=Integer.parseInt(sa[j]);
				if((a[i][j]-a[0][0])%d!=0){
					flag=false;
					break;
				}
				sum+=a[i][j];
			}
			if(!flag)
				break;
		}
		if(!flag)
			System.out.println(-1);
		else{
			double avg=sum/(m*n);
			double min=Math.abs(a[0][0]-avg);
			int small=a[0][0];
			for(int i=0;i<n;i++){
				for(int j=0;j<m;j++){
					if(Math.abs(avg-a[i][j])<min){
						min=Math.abs(avg-a[i][j]);
						small=a[i][j];
					}
				}
			}
			long r=0;
			for(int i=0;i<n;i++){
				for(int j=0;j<m;j++){
					r+=(Math.abs(small-a[i][j]))/d;
				}
			}
			System.out.println(r);
		}
	}
}


import java.util.Arrays;
import java.util.Scanner;

 public class solve {
	 static int n,m ;
	 static int k ;
	 static int arr [];
	public static void main(String args[]){
		Scanner sc= new Scanner(System.in);
		 n = sc.nextInt();
		 m =sc.nextInt();
		 k =sc.nextInt();
		 arr=new int[n*m];

		for(int i =0 ;i <n*m;i++)arr[i]=sc.nextInt();
		Arrays.sort(arr);
		for(int i =0 ;i <n*m-1;i++)
		{
			if(((arr[i+1]-arr[i])%k !=0)){System.out.println(-1); return ;}
		}
		int min = Integer.MAX_VALUE;
		for(int i =0 ; i<n*m ; i++){

			min = Math.min(min, f(arr[i]));

	}
		System.out.println(min);

	}

	public static int f(int x){
		int sum =0 ;

		for(int  i=0 ; i<n*m;i++){
			sum+=Math.abs(arr[i]-x)/k ;
		}
		return sum ;

	}

 }


import java.util.Arrays;
import java.util.Scanner;

 public class solve {
	 static int n,m ;
	 static int k ;
	 static int arr [];
	public static void main(String args[]){
		Scanner sc= new Scanner(System.in);
		 n = sc.nextInt();
		 m =sc.nextInt();
		 k =sc.nextInt();
		 arr=new int[n*m];

		for(int i =0 ;i <n*m;i++)arr[i]=sc.nextInt();
		Arrays.sort(arr);
		for(int i =0 ;i <n*m-1;i++)
		{
			if(((arr[i+1]-arr[i])%k !=0)){System.out.println(-1); return ;}
		}

		int l = 0 ; int r= n*m-1 ;int mid =(l+r)/2 ;
		int L=l +(r-l)/3 ; int R = l+2*(r-l)/3;
		int emid = arr[mid];
		for(int i = 1; i<=200;i++){
			L=l +(r-l)/3 ;  R = l+2*(r-l)/3;
			if(f(arr[L])<f(arr[R]))
			{
				r =R ;

			}else l = L ;

		}
		int ans = f(arr[(l+r)/2]);
		System.out.println(ans);

		/*for(int i = l ; i <=r;i++)
			ans=Math.min(ans, f(arr[i]));
		System.out.println(ans);
*/

	}

	public static int f(int x){
		int sum =0 ;

		for(int  i=0 ; i<n*m;i++){
			sum+=Math.abs(arr[i]-x)/k ;
		}
		return sum ;

	}

 }


import java.util.Arrays;
import java.util.Scanner;

 public class solve {

	 public static int TernarySearch(int a,int b){
		 int min = a ; int max= b;
		 for(int i=1;i<=400; i++)
		 {
			 int g= min+(max-min)/3;
			 int h = min + 2*(max-min)/3;
			 if(f(arr[h])>f(arr[g]))
				 max=h;
			 else
				 min=g;

		 }
		 return (min+max)/2 ;

	 }

	 static int n,m ;
	 static int k ;
	 static int arr [];
	public static void main(String args[]){
		Scanner sc= new Scanner(System.in);
		 n = sc.nextInt();
		 m =sc.nextInt();
		 k =sc.nextInt();
		 arr=new int[n*m];

		for(int i =0 ;i <n*m;i++)arr[i]=sc.nextInt();
		Arrays.sort(arr);
		for(int i =0 ;i <n*m-1;i++)
		{
			if(((arr[i+1]-arr[i])%k !=0)){System.out.println(-1); return ;}
		}

		int ans = TernarySearch(0,(m*n)-1);
		System.out.println(f(arr[ans]));

	}

	public static int f(int x){
		int sum =0 ;

		for(int  i=0 ; i<n*m;i++){
			sum+=Math.abs(arr[i]-x)/k ;
		}
		return sum ;

	}

 }


import java.util.Arrays;
import java.util.Scanner;

 public class solve {
	 static int n,m ;
	 static int k ;
	 static int arr [];
	public static void main(String args[]){
		Scanner sc= new Scanner(System.in);
		 n = sc.nextInt();
		 m =sc.nextInt();
		 k =sc.nextInt();
		 arr=new int[n*m];

		for(int i =0 ;i <n*m;i++)arr[i]=sc.nextInt();
		Arrays.sort(arr);
		for(int i =0 ;i <n*m-1;i++)
		{
			if(((arr[i+1]-arr[i])%k !=0)){System.out.println(-1); return ;}
		}

		int l = 0 ; int r= n*m-1 ;int mid =(l+r)/2 ;
		int L=l +(r-l)/3 ; int R = l+2*(r-l)/3;
		int emid = arr[mid];
		for(int i = 1; i<=400;i++){
			L=l +(r-l)/3 ;  R = l+2*(r-l)/3;
			if(f(arr[L])<f(arr[R]))
			{
				r =R ;

			}else l = L ;

		}
		int ans = f(arr[l]);

		for(int i = l ; i <=r;i++)
			ans=Math.min(ans, f(arr[i]));
		System.out.println(ans);

	}

	public static int f(int x){
		int sum =0 ;

		for(int  i=0 ; i<n*m;i++){
			sum+=Math.abs(arr[i]-x)/k ;
		}
		return sum ;

	}

 }


import java.util.Arrays;
import java.util.Scanner;

 public class solve {
	 static int n,m ;
	 static int k ;
	 static int arr [];
	public static void main(String args[]){
		Scanner sc= new Scanner(System.in);
		 n = sc.nextInt();
		 m =sc.nextInt();
		 k =sc.nextInt();
		 arr=new int[n*m];

		for(int i =0 ;i <n*m;i++)arr[i]=sc.nextInt();
		Arrays.sort(arr);
		for(int i =0 ;i <n*m-1;i++)
		{
			if(((arr[i+1]-arr[i])%k !=0)){System.out.println(-1); return ;}
		}
		int min = 1232345;
		for(int i =0 ; i<n*m ; i++){

			min = Math.min(min, f(arr[i]));

	}
		System.out.println(min);

	}

	public static int f(int x){
		int sum =0 ;

		for(int  i=0 ; i<n*m;i++){
			sum+=Math.abs(arr[i]-x)/k ;
		}
		return sum ;

	}

 }


import java.util.Arrays;
import java.util.Scanner;

 public class solve {
	 static int n,m ;
	 static int k ;
	 static int arr [];
	public static void main(String args[]){
		Scanner sc= new Scanner(System.in);
		 n = sc.nextInt();
		 m =sc.nextInt();
		 k =sc.nextInt();
		 arr=new int[n*m];

		for(int i =0 ;i <n*m;i++)arr[i]=sc.nextInt();
		Arrays.sort(arr);
		for(int i =0 ;i <n*m-1;i++)
		{
			if(((arr[i+1]-arr[i])%k !=0)){System.out.println(-1); return ;}
		}

		int l = 0 ; int r= n*m-1 ;int mid =(l+r)/2 ;
		int L=l +(r-l)/3 ; int R = l+2*(r-l)/3;
		int emid = arr[mid];
		for(int i = 1; i<=200;i++){
			L=l +(r-l)/3 ;  R = l+2*(r-l)/3;
			if(f(arr[L])<f(arr[R]))
			{
				r =R ;

			}else l = L ;

		}
		int ans = f(arr[l]);

		for(int i = l ; i <=r;i++)
			ans=Math.min(ans, f(arr[i]));
		System.out.println(ans);

	}

	public static int f(int x){
		int sum =0 ;

		for(int  i=0 ; i<n*m;i++){
			sum+=Math.abs(arr[i]-x)/k ;
		}
		return sum ;

	}

 }


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.BufferedReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Gabriel Carrillo
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        OutputWriter out = new OutputWriter(outputStream);
        BPoloThePenguinAndMatrix solver = new BPoloThePenguinAndMatrix();
        solver.solve(1, in, out);
        out.close();
    }

    static class BPoloThePenguinAndMatrix {
        public void solve(int testNumber, InputReader in, OutputWriter out) {
            int n = in.nextInt(), m = in.nextInt(), d = in.nextInt();
            int[] vals = new int[n * m];
            for (int i = 0; i < n * m; i++)
                vals[i] = in.nextInt();
            Arrays.sort(vals);
            int prev = vals[0];
            for (int i = 1; i < n * m; i++) {
                if ((vals[i] - prev) % d != 0) {
                    out.println(-1);
                    return;
                }
                prev = vals[i];
            }

            int mid = (n * m) / 2;
            int med = vals[mid];
            int ans = 0;
            for (int val : vals)
                ans += Math.abs((med - val)) / d;
            out.println(ans);

        }

    }

    static class OutputWriter {
        private final PrintWriter writer;

        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

        public void close() {
            writer.close();
        }

        public void println(int i) {
            writer.println(i);
        }

    }

    static class InputReader {
        BufferedReader br;
        StringTokenizer st;

        public InputReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        public InputReader(InputStream in) {
            br = new BufferedReader(new InputStreamReader(in));
        }

        public String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.BufferedReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Gabriel Carrillo
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        OutputWriter out = new OutputWriter(outputStream);
        BPoloThePenguinAndMatrix solver = new BPoloThePenguinAndMatrix();
        solver.solve(1, in, out);
        out.close();
    }

    static class BPoloThePenguinAndMatrix {
        public void solve(int testNumber, InputReader in, OutputWriter out) {
            int n = in.nextInt(), m = in.nextInt(), d = in.nextInt();
            int[] vals = new int[n * m];
            int div = 0;
            for (int i = 0; i < n * m; i++) {
                vals[i] = in.nextInt();
                div = gcd(vals[i], div);
            }
            if (div < d) {
                out.println(-1);
                return;
            }

            Arrays.sort(vals);
            int mid = (n * m) / 2;
            int med = vals[mid];
            int ans = 0;
            for (int val : vals)
                ans += Math.abs((med - val)) / div;
            out.println(ans);

        }

        int gcd(int a, int b) {
            if (b == 0) return a;
            return gcd(b, a % b);
        }

    }

    static class OutputWriter {
        private final PrintWriter writer;

        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

        public void close() {
            writer.close();
        }

        public void println(int i) {
            writer.println(i);
        }

    }

    static class InputReader {
        BufferedReader br;
        StringTokenizer st;

        public InputReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        public InputReader(InputStream in) {
            br = new BufferedReader(new InputStreamReader(in));
        }

        public String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.BufferedReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Gabriel Carrillo
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        OutputWriter out = new OutputWriter(outputStream);
        BDimaAndToDoList solver = new BDimaAndToDoList();
        solver.solve(1, in, out);
        out.close();
    }

    static class BDimaAndToDoList {
        public void solve(int testNumber, InputReader in, OutputWriter out) {
            int n = in.nextInt(), k = in.nextInt();
            int[] vals = new int[n];
            int[] mod = new int[k];
            int min = 0;
            for (int i = 0; i < n; i++) vals[i] = in.nextInt();
            for (int i = 0; i < n; i++) mod[(i % k)] += vals[i];
            for (int i = 0; i < k; i++) if (mod[min] > mod[i]) min = i;
            out.println(min + 1);
        }

    }

    static class OutputWriter {
        private final PrintWriter writer;

        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

        public void close() {
            writer.close();
        }

        public void println(int i) {
            writer.println(i);
        }

    }

    static class InputReader {
        BufferedReader br;
        StringTokenizer st;

        public InputReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        public InputReader(InputStream in) {
            br = new BufferedReader(new InputStreamReader(in));
        }

        public String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.BufferedReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Gabriel Carrillo
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        OutputWriter out = new OutputWriter(outputStream);
        BPoloThePenguinAndMatrix solver = new BPoloThePenguinAndMatrix();
        solver.solve(1, in, out);
        out.close();
    }

    static class BPoloThePenguinAndMatrix {
        public void solve(int testNumber, InputReader in, OutputWriter out) {
            int n = in.nextInt(), m = in.nextInt(), d = in.nextInt();
            int[] vals = new int[n * m];
            int div = 0;
            for (int i = 0; i < n * m; i++) {
                vals[i] = in.nextInt();
                div = gcd(vals[i], div);
            }
            if (div < d) {
                out.println(-1);
                return;
            }

            int mid = (n * m) / 2;
            int med = vals[mid];
            int ans = 0;
            for (int val : vals)
                ans += Math.abs((med - val)) / div;
            out.println(ans);

        }

        int gcd(int a, int b) {
            if (b == 0) return a;
            return gcd(b, a % b);
        }

    }

    static class OutputWriter {
        private final PrintWriter writer;

        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

        public void close() {
            writer.close();
        }

        public void println(int i) {
            writer.println(i);
        }

    }

    static class InputReader {
        BufferedReader br;
        StringTokenizer st;

        public InputReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        public InputReader(InputStream in) {
            br = new BufferedReader(new InputStreamReader(in));
        }

        public String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

    }
}


/*
    Aman Agarwal
    algo.java
*/

import java.util.*;
import java.io.*;

public class B289
{
    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 FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int ni()throws IOException{return sc.nextInt();}
    static long nl()throws IOException{return sc.nextLong();}
    static int[] nai(int N,int start)throws IOException{int[]A=new int[N+start];for(int i=start;i!=(N+start);i++){A[i]=ni();}return A;}
    static Integer[] naI(int N,int start)throws IOException{Integer[]A=new Integer[N+start];for(int i=start;i!=(N+start);i++){A[i]=ni();}return A;}
    static long[] nal(int N)throws IOException{long[]A=new long[N];for(int i=0;i!=N;i++){A[i]=nl();}return A;}
    static void print(int arr[]){for(int i=0;i<arr.length;i++)out.print(arr[i]+" ");out.println();}
    static void print(long arr[]){for(int i=0;i<arr.length;i++)out.print(arr[i]+" ");out.println();}
    static long gcd(long a, long b)throws IOException{return (b==0)?a:gcd(b,a%b);}
    static int gcd(int a, int b)throws IOException{return (b==0)?a:gcd(b,a%b);}
    static int bit(long n)throws IOException{return (n==0)?0:(1+bit(n&(n-1)));} // return the number of set bits.
    static boolean isPrime(int number){if(number==1) return false;if (number == 2 || number == 3){return true;}if (number % 2 == 0) {return false;}int sqrt = (int) Math.sqrt(number) + 1;for(int i = 3; i < sqrt; i += 2){if (number % i == 0){return false;}}return true;}
    static boolean isPrime(long number){if(number==1) return false;if (number == 2 || number == 3){return true;}if (number % 2 == 0) {return false;}long sqrt = (long) Math.sqrt(number) + 1;for(int i = 3; i < sqrt; i += 2){if (number % i == 0){return false;}}return true;}
    static int power(int n,int p){if(p==0)return 1;int a = power(n,p/2);a = a*a;int b = p & 1;if(b!=0){a =  n*a;}return a;}
    static long power(long n,long p){if(p==0)return 1;long a = power(n,p/2);a = a*a;long b = p & 1;if(b!=0){a =  n*a;}return a;}
    static void reverse(int[] a) {int b;for (int i = 0, j = a.length - 1; i < j; i++, j--) {b = a[i];a[i] = a[j];a[j] = b;}}
    static void reverse(long[] a) {long b;for (int i = 0, j = a.length - 1; i < j; i++, j--) {b = a[i];a[i] = a[j];a[j] = b;}}
    static void swap(int a[],int i,int j){int temp = a[i];a[i] = a[j];a[j] = temp;}
    static void swap(long a[],int i,int j){long temp = a[i];a[i] = a[j];a[j] = temp;}
    static int count(int n){int c=0;while(n>0){c++;n = n/10;}return c;}
    static int[] prefix_sum(int a[],int n){int s[] = new int[n];s[0] = a[0];for(int i=1;i<n;i++){s[i] = a[i]+s[i-1];}return s;}
    static long[] prefix_sum_int(int a[],int n){long s[] = new long[n];s[0] = (long)a[0];for(int i=1;i<n;i++){s[i] = ((long)a[i])+s[i-1];}return s;}
    static long[] prefix_sum_Integer(Integer a[],int n){long s[] = new long[n];s[0] = (long)a[0];for(int i=1;i<n;i++){s[i] = ((long)a[i])+s[i-1];}return s;}
    static long[] prefix_sum_long(long a[],int n){long s[] = new long[n];s[0] = a[0];for(int i=1;i<n;i++){s[i] = a[i]+s[i-1];}return s;}
    static boolean isPerfectSquare(double x){double sr = Math.sqrt(x);return ((sr - Math.floor(sr)) == 0);}
    static ArrayList<Integer> sieve(int n) {int k=0; boolean prime[] = new boolean[n+1];ArrayList<Integer> p_arr = new ArrayList<>();for(int i=0;i<n;i++) prime[i] = true;for(int p = 2; p*p <=n; p++){ k=p;if(prime[p] == true) { p_arr.add(p);for(int i = p*2; i <= n; i += p) prime[i] = false; } }for(int i = k+1;i<=n;i++){if(prime[i]==true)p_arr.add(i);}return p_arr;}
    static boolean[] seive_check(int n) {boolean prime[] = new boolean[n+1];for(int i=0;i<n;i++) prime[i] = true;for(int p = 2; p*p <=n; p++){ if(prime[p] == true) { for(int i = p*2; i <= n; i += p) prime[i] = false; } }prime[1]=false;return prime;}
    static int get_bits(int n){int p=0;while(n>0){p++;n = n>>1;}return p;}
    static int get_bits(long n){int p=0;while(n>0){p++;n = n>>1;}return p;}
    static int get_2_power(int n){if((n & (n-1))==0)return get_bits(n)-1;return -1;}
    static int get_2_power(long n){if((n & (n-1))==0)return get_bits(n)-1;return -1;}
    static void close(){out.flush();}

    /*-------------------------Main Code Starts(algo.java)----------------------------------*/

    public static void main(String[] args) throws IOException
    {
        int t = 1;
        //t = sc.nextInt();
        while(t-- > 0)
        {
            int n = ni();
            int m = ni();
            int d = ni();
            int flg=0;
            int arr[] = new int[n*m];
            for(int i=0;i<n*m;i++)
            {
                arr[i] = ni();
                if(arr[i] % d !=0 )
                    flg=1;
            }
            if(flg==1)
                out.println(-1);
            else
            {
                Arrays.sort(arr);
                int p = (n*m)/2;
                long ans = 0;
                for(int i=0;i<n*m;i++)
                {
                    int x = Math.abs(arr[p]-arr[i]);
                    //out.print(x/d+" ");
                    ans = ans + x/d;
                }
                out.println(ans);
            }
        }

        close();
    }
}


/*
    Aman Agarwal
    algo.java
*/

import java.util.*;
import java.io.*;

public class B289
{
    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 FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int ni()throws IOException{return sc.nextInt();}
    static long nl()throws IOException{return sc.nextLong();}
    static int[] nai(int N,int start)throws IOException{int[]A=new int[N+start];for(int i=start;i!=(N+start);i++){A[i]=ni();}return A;}
    static Integer[] naI(int N,int start)throws IOException{Integer[]A=new Integer[N+start];for(int i=start;i!=(N+start);i++){A[i]=ni();}return A;}
    static long[] nal(int N)throws IOException{long[]A=new long[N];for(int i=0;i!=N;i++){A[i]=nl();}return A;}
    static void print(int arr[]){for(int i=0;i<arr.length;i++)out.print(arr[i]+" ");out.println();}
    static void print(long arr[]){for(int i=0;i<arr.length;i++)out.print(arr[i]+" ");out.println();}
    static long gcd(long a, long b)throws IOException{return (b==0)?a:gcd(b,a%b);}
    static int gcd(int a, int b)throws IOException{return (b==0)?a:gcd(b,a%b);}
    static int bit(long n)throws IOException{return (n==0)?0:(1+bit(n&(n-1)));} // return the number of set bits.
    static boolean isPrime(int number){if(number==1) return false;if (number == 2 || number == 3){return true;}if (number % 2 == 0) {return false;}int sqrt = (int) Math.sqrt(number) + 1;for(int i = 3; i < sqrt; i += 2){if (number % i == 0){return false;}}return true;}
    static boolean isPrime(long number){if(number==1) return false;if (number == 2 || number == 3){return true;}if (number % 2 == 0) {return false;}long sqrt = (long) Math.sqrt(number) + 1;for(int i = 3; i < sqrt; i += 2){if (number % i == 0){return false;}}return true;}
    static int power(int n,int p){if(p==0)return 1;int a = power(n,p/2);a = a*a;int b = p & 1;if(b!=0){a =  n*a;}return a;}
    static long power(long n,long p){if(p==0)return 1;long a = power(n,p/2);a = a*a;long b = p & 1;if(b!=0){a =  n*a;}return a;}
    static void reverse(int[] a) {int b;for (int i = 0, j = a.length - 1; i < j; i++, j--) {b = a[i];a[i] = a[j];a[j] = b;}}
    static void reverse(long[] a) {long b;for (int i = 0, j = a.length - 1; i < j; i++, j--) {b = a[i];a[i] = a[j];a[j] = b;}}
    static void swap(int a[],int i,int j){int temp = a[i];a[i] = a[j];a[j] = temp;}
    static void swap(long a[],int i,int j){long temp = a[i];a[i] = a[j];a[j] = temp;}
    static int count(int n){int c=0;while(n>0){c++;n = n/10;}return c;}
    static int[] prefix_sum(int a[],int n){int s[] = new int[n];s[0] = a[0];for(int i=1;i<n;i++){s[i] = a[i]+s[i-1];}return s;}
    static long[] prefix_sum_int(int a[],int n){long s[] = new long[n];s[0] = (long)a[0];for(int i=1;i<n;i++){s[i] = ((long)a[i])+s[i-1];}return s;}
    static long[] prefix_sum_Integer(Integer a[],int n){long s[] = new long[n];s[0] = (long)a[0];for(int i=1;i<n;i++){s[i] = ((long)a[i])+s[i-1];}return s;}
    static long[] prefix_sum_long(long a[],int n){long s[] = new long[n];s[0] = a[0];for(int i=1;i<n;i++){s[i] = a[i]+s[i-1];}return s;}
    static boolean isPerfectSquare(double x){double sr = Math.sqrt(x);return ((sr - Math.floor(sr)) == 0);}
    static ArrayList<Integer> sieve(int n) {int k=0; boolean prime[] = new boolean[n+1];ArrayList<Integer> p_arr = new ArrayList<>();for(int i=0;i<n;i++) prime[i] = true;for(int p = 2; p*p <=n; p++){ k=p;if(prime[p] == true) { p_arr.add(p);for(int i = p*2; i <= n; i += p) prime[i] = false; } }for(int i = k+1;i<=n;i++){if(prime[i]==true)p_arr.add(i);}return p_arr;}
    static boolean[] seive_check(int n) {boolean prime[] = new boolean[n+1];for(int i=0;i<n;i++) prime[i] = true;for(int p = 2; p*p <=n; p++){ if(prime[p] == true) { for(int i = p*2; i <= n; i += p) prime[i] = false; } }prime[1]=false;return prime;}
    static int get_bits(int n){int p=0;while(n>0){p++;n = n>>1;}return p;}
    static int get_bits(long n){int p=0;while(n>0){p++;n = n>>1;}return p;}
    static int get_2_power(int n){if((n & (n-1))==0)return get_bits(n)-1;return -1;}
    static int get_2_power(long n){if((n & (n-1))==0)return get_bits(n)-1;return -1;}
    static void close(){out.flush();}

    /*-------------------------Main Code Starts(algo.java)----------------------------------*/

    public static void main(String[] args) throws IOException
    {
        int t = 1;
        //t = sc.nextInt();
        while(t-- > 0)
        {
            int n = ni();
            int m = ni();
            int d = ni();
            int flg=0;
            int arr[] = new int[n*m];
            for(int i=0;i<n*m;i++)
            {
                arr[i] = ni();
            }
            int mod = arr[0]%d;

            for(int i=1;i<n*m;i++)
                if(arr[i]%d!=mod)
                    flg=1;

            if(flg==1)
                out.println(-1);
            else
            {
                Arrays.sort(arr);
                int p = (n*m)/2;
                long ans = 0;
                for(int i=0;i<n*m;i++)
                {
                    int x = Math.abs(arr[p]-arr[i]);
                    //out.print(x/d+" ");
                    ans = ans + x/d;
                }
                out.println(ans);
            }
        }

        close();
    }
}


/*
    Aman Agarwal
    algo.java
*/

import java.util.*;
import java.io.*;

public class B289
{
    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 FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int ni()throws IOException{return sc.nextInt();}
    static long nl()throws IOException{return sc.nextLong();}
    static int[] nai(int N,int start)throws IOException{int[]A=new int[N+start];for(int i=start;i!=(N+start);i++){A[i]=ni();}return A;}
    static Integer[] naI(int N,int start)throws IOException{Integer[]A=new Integer[N+start];for(int i=start;i!=(N+start);i++){A[i]=ni();}return A;}
    static long[] nal(int N)throws IOException{long[]A=new long[N];for(int i=0;i!=N;i++){A[i]=nl();}return A;}
    static void print(int arr[]){for(int i=0;i<arr.length;i++)out.print(arr[i]+" ");out.println();}
    static void print(long arr[]){for(int i=0;i<arr.length;i++)out.print(arr[i]+" ");out.println();}
    static long gcd(long a, long b)throws IOException{return (b==0)?a:gcd(b,a%b);}
    static int gcd(int a, int b)throws IOException{return (b==0)?a:gcd(b,a%b);}
    static int bit(long n)throws IOException{return (n==0)?0:(1+bit(n&(n-1)));} // return the number of set bits.
    static boolean isPrime(int number){if(number==1) return false;if (number == 2 || number == 3){return true;}if (number % 2 == 0) {return false;}int sqrt = (int) Math.sqrt(number) + 1;for(int i = 3; i < sqrt; i += 2){if (number % i == 0){return false;}}return true;}
    static boolean isPrime(long number){if(number==1) return false;if (number == 2 || number == 3){return true;}if (number % 2 == 0) {return false;}long sqrt = (long) Math.sqrt(number) + 1;for(int i = 3; i < sqrt; i += 2){if (number % i == 0){return false;}}return true;}
    static int power(int n,int p){if(p==0)return 1;int a = power(n,p/2);a = a*a;int b = p & 1;if(b!=0){a =  n*a;}return a;}
    static long power(long n,long p){if(p==0)return 1;long a = power(n,p/2);a = a*a;long b = p & 1;if(b!=0){a =  n*a;}return a;}
    static void reverse(int[] a) {int b;for (int i = 0, j = a.length - 1; i < j; i++, j--) {b = a[i];a[i] = a[j];a[j] = b;}}
    static void reverse(long[] a) {long b;for (int i = 0, j = a.length - 1; i < j; i++, j--) {b = a[i];a[i] = a[j];a[j] = b;}}
    static void swap(int a[],int i,int j){int temp = a[i];a[i] = a[j];a[j] = temp;}
    static void swap(long a[],int i,int j){long temp = a[i];a[i] = a[j];a[j] = temp;}
    static int count(int n){int c=0;while(n>0){c++;n = n/10;}return c;}
    static int[] prefix_sum(int a[],int n){int s[] = new int[n];s[0] = a[0];for(int i=1;i<n;i++){s[i] = a[i]+s[i-1];}return s;}
    static long[] prefix_sum_int(int a[],int n){long s[] = new long[n];s[0] = (long)a[0];for(int i=1;i<n;i++){s[i] = ((long)a[i])+s[i-1];}return s;}
    static long[] prefix_sum_Integer(Integer a[],int n){long s[] = new long[n];s[0] = (long)a[0];for(int i=1;i<n;i++){s[i] = ((long)a[i])+s[i-1];}return s;}
    static long[] prefix_sum_long(long a[],int n){long s[] = new long[n];s[0] = a[0];for(int i=1;i<n;i++){s[i] = a[i]+s[i-1];}return s;}
    static boolean isPerfectSquare(double x){double sr = Math.sqrt(x);return ((sr - Math.floor(sr)) == 0);}
    static ArrayList<Integer> sieve(int n) {int k=0; boolean prime[] = new boolean[n+1];ArrayList<Integer> p_arr = new ArrayList<>();for(int i=0;i<n;i++) prime[i] = true;for(int p = 2; p*p <=n; p++){ k=p;if(prime[p] == true) { p_arr.add(p);for(int i = p*2; i <= n; i += p) prime[i] = false; } }for(int i = k+1;i<=n;i++){if(prime[i]==true)p_arr.add(i);}return p_arr;}
    static boolean[] seive_check(int n) {boolean prime[] = new boolean[n+1];for(int i=0;i<n;i++) prime[i] = true;for(int p = 2; p*p <=n; p++){ if(prime[p] == true) { for(int i = p*2; i <= n; i += p) prime[i] = false; } }prime[1]=false;return prime;}
    static int get_bits(int n){int p=0;while(n>0){p++;n = n>>1;}return p;}
    static int get_bits(long n){int p=0;while(n>0){p++;n = n>>1;}return p;}
    static int get_2_power(int n){if((n & (n-1))==0)return get_bits(n)-1;return -1;}
    static int get_2_power(long n){if((n & (n-1))==0)return get_bits(n)-1;return -1;}
    static void close(){out.flush();}

    /*-------------------------Main Code Starts(algo.java)----------------------------------*/

    public static void main(String[] args) throws IOException
    {
        int t = 1;
        //t = sc.nextInt();
        while(t-- > 0)
        {
            int n = ni();
            int m = ni();
            int d = ni();
            int flg=0;
            int arr[] = new int[n*m];
            for(int i=0;i<n*m;i++)
            {
                arr[i] = ni();
            }
            int mod = arr[0]%d;

            for(int i=1;i<n*m;i++)
                if(arr[i]%d!=m)
                    flg=1;

            if(flg==1)
                out.println(-1);
            else
            {
                Arrays.sort(arr);
                int p = (n*m)/2;
                long ans = 0;
                for(int i=0;i<n*m;i++)
                {
                    int x = Math.abs(arr[p]-arr[i]);
                    //out.print(x/d+" ");
                    ans = ans + x/d;
                }
                out.println(ans);
            }
        }

        close();
    }
}


import java.io.*;
import java.util.*;
import java.text.DecimalFormat;
import java.math.*;
public class Dfs {

    public static void main(String[] args)throws Exception {

     reader in = new reader(System.in);
     int n=in.nextInt();
     int m=in.nextInt();
     int d=in.nextInt();
     Integer arr []= new Integer[n*m];
     boolean flag = true;

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

     }

     Arrays.sort(arr);
     for(int i=1;i<n*m;i++){
      if((arr[i]-arr[i-1])%d==0)
          continue;
      else{
          System.out.println(-1);System.exit(0);
      }
     }
      if(arr.length==1){
          System.out.println("0");
          System.exit(0);
      }
      int mid=arr[m*n/2-1];
      int mid2;boolean bo=false;
      long sum =0 ;long sum2=0;
      for(int i= 0;i<n*m;i++)
          sum+=(Math.abs(arr[i]-mid)/d);
      if(m*n/2<arr.length){
          mid2=arr[m*n/2];bo=true;
          for(int i= 0;i<n*m;i++)
          sum2+=(Math.abs(arr[i]-mid2)/d);
      }
      if(bo)
            System.out.println(Math.min(sum, sum2));
      else
        System.out.println(sum);

  }

    static class reader{
      BufferedReader read ;
      StringTokenizer tok;
      public reader(InputStream stream){
          read= new BufferedReader(new InputStreamReader(stream));
          tok=null;

      }
       String next()throws Exception{

          while(tok==null||!tok.hasMoreElements()){
              tok= new StringTokenizer(read.readLine());

          }
          return tok.nextToken();
      }

       int nextInt()throws Exception{
           return Integer.parseInt(next());

       }
    }

}


import java.io.*;
import java.util.*;
import java.text.DecimalFormat;
import java.math.*;
public class Dfs {

    public static void main(String[] args)throws Exception {

     reader in = new reader(System.in);
     int n=in.nextInt();
     int m=in.nextInt();
     int d=in.nextInt();
     Integer arr []= new Integer[n*m];
     boolean flag = true;
     for(int i=0 ;i<n*m;i++){
         arr[i]=in.nextInt();
         if(arr[i]%d!=0)
             flag=false;
     }
     if(!flag){
         System.out.println("-1");
         System.exit(0);
     }
     Arrays.sort(arr);
      int mid=arr[m*n/2-1];
      int mid2;boolean bo=false;
      long sum =0 ;long sum2=0;
      for(int i= 0;i<n*m;i++)
          sum+=(Math.abs(arr[i]-mid)/d);
      if(m*n/2<arr.length){
          mid2=arr[m*n/2];bo=true;
          for(int i= 0;i<n*m;i++)
          sum2+=(Math.abs(arr[i]-mid2)/d);
      }
      if(bo)
            System.out.println(Math.min(sum, sum2));
      else
        System.out.println(sum);

  }

    static class reader{
      BufferedReader read ;
      StringTokenizer tok;
      public reader(InputStream stream){
          read= new BufferedReader(new InputStreamReader(stream));
          tok=null;

      }
       String next()throws Exception{

          while(tok==null||!tok.hasMoreElements()){
              tok= new StringTokenizer(read.readLine());

          }
          return tok.nextToken();
      }

       int nextInt()throws Exception{
           return Integer.parseInt(next());

       }
    }

}


import java.io.*;
import java.util.*;
import java.text.DecimalFormat;
import java.math.*;
public class Dfs {

    public static void main(String[] args)throws Exception {

     reader in = new reader(System.in);
     int n=in.nextInt();
     int m=in.nextInt();
     int d=in.nextInt();
     Integer arr []= new Integer[n*m];
     boolean flag = true;

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

     }

     Arrays.sort(arr);
     for(int i=1;i<n;i++){
      if((arr[i]-arr[i-1])%d==0)
          continue;
      else{
          System.out.println(-1);System.exit(0);
      }
     }
      int mid=arr[m*n/2-1];
      int mid2;boolean bo=false;
      long sum =0 ;long sum2=0;
      for(int i= 0;i<n*m;i++)
          sum+=(Math.abs(arr[i]-mid)/d);
      if(m*n/2<arr.length){
          mid2=arr[m*n/2];bo=true;
          for(int i= 0;i<n*m;i++)
          sum2+=(Math.abs(arr[i]-mid2)/d);
      }
      if(bo)
            System.out.println(Math.min(sum, sum2));
      else
        System.out.println(sum);

  }

    static class reader{
      BufferedReader read ;
      StringTokenizer tok;
      public reader(InputStream stream){
          read= new BufferedReader(new InputStreamReader(stream));
          tok=null;

      }
       String next()throws Exception{

          while(tok==null||!tok.hasMoreElements()){
              tok= new StringTokenizer(read.readLine());

          }
          return tok.nextToken();
      }

       int nextInt()throws Exception{
           return Integer.parseInt(next());

       }
    }

}


import java.io.*;
import java.util.*;
import java.text.DecimalFormat;
import java.math.*;
public class Dfs {

    public static void main(String[] args)throws Exception {

     reader in = new reader(System.in);
     int n=in.nextInt();
     int m=in.nextInt();
     int d=in.nextInt();
     Integer arr []= new Integer[n*m];
     boolean flag = true;

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

     }

     Arrays.sort(arr);
     for(int i=1;i<n*m;i++){
      if((arr[i]-arr[i-1])%d==0)
          continue;
      else{
          System.out.println(-1);System.exit(0);
      }
     }
      int mid=arr[m*n/2-1];
      int mid2;boolean bo=false;
      long sum =0 ;long sum2=0;
      for(int i= 0;i<n*m;i++)
          sum+=(Math.abs(arr[i]-mid)/d);
      if(m*n/2<arr.length){
          mid2=arr[m*n/2];bo=true;
          for(int i= 0;i<n*m;i++)
          sum2+=(Math.abs(arr[i]-mid2)/d);
      }
      if(bo)
            System.out.println(Math.min(sum, sum2));
      else
        System.out.println(sum);

  }

    static class reader{
      BufferedReader read ;
      StringTokenizer tok;
      public reader(InputStream stream){
          read= new BufferedReader(new InputStreamReader(stream));
          tok=null;

      }
       String next()throws Exception{

          while(tok==null||!tok.hasMoreElements()){
              tok= new StringTokenizer(read.readLine());

          }
          return tok.nextToken();
      }

       int nextInt()throws Exception{
           return Integer.parseInt(next());

       }
    }

}


/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
public class bbg
{
	public static int result;

public static ArrayList<Integer> [] graph;

public static int[]cats;

public static int vizitat[];
public static int x;

	public static void main (String[] args) throws java.lang.Exception
{

Scanner input=new Scanner(System.in);

HashMap<String, Integer> map1= new HashMap<String, Integer>();
HashMap<String, Integer> map2= new HashMap<String, Integer>();

	int n=input.nextInt();
	int m=input.nextInt();
	int d=input.nextInt();

	int sum=0;

	int mat[][]=new int[n][m];

	int v[]=new int[10002];

	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++) {

			mat[i][j]=input.nextInt();

			v[mat[i][j]]++;

	sum++;

		}

		int media=sum/(n*m);

		int contor=0;
		int x=1;

		int max=Integer.MAX_VALUE;
		int poz=-1;

		int rasp=Integer.MAX_VALUE;

		for(int centru=Math.max(0,sum/2);centru<=Math.min(10000,sum*2);centru++){

		    rasp=0;
		    x=1;

		    for(int j=1;j<=10000;j++){

		        if(v[j]>0) {

		            //System.out.println(j+" "+v[j]);

		            if(Math.abs(j-centru)%d!=0) {x=0;j=10001;}

		            else {rasp+=Math.abs(centru-j)*v[j]/d;
		          //  System.out.println(centru+" "+j+" "+rasp);
		            }

		    }

		}

		  if(x==1){contor++;  System.out.println("contor="+contor);
		    max=Math.min(max,rasp);}

		}

		if(contor==0) System.out.println(-1);
		else
		System.out.println(max);

}

}


/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
public class bbg
{
	public static int result;

public static ArrayList<Integer> [] graph;

public static int[]cats;

public static int vizitat[];
public static int x;

	public static void main (String[] args) throws java.lang.Exception
{

Scanner input=new Scanner(System.in);

HashMap<String, Integer> map1= new HashMap<String, Integer>();
HashMap<String, Integer> map2= new HashMap<String, Integer>();

	int n=input.nextInt();
	int m=input.nextInt();
	int d=input.nextInt();

	int sum=0;

	int mat[][]=new int[n][m];

	int v[]=new int[10002];

	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++) {

			mat[i][j]=input.nextInt();

			v[mat[i][j]]++;

		}

		int contor=0;
		int x=1;

		int max=Integer.MAX_VALUE;
		int poz=-1;

		int rasp=Integer.MAX_VALUE;

		for(int centru=1;centru<=10000;centru++){

		    rasp=0;
		    x=1;

		    for(int j=1;j<=10000;j++){

		        if(v[j]>0) {

		            //System.out.println(j+" "+v[j]);

		            if(Math.abs(j-centru)%d!=0) {x=0;j=10001;}

		            else {rasp+=Math.abs(centru-j)*v[j]/d;
		          //  System.out.println(centru+" "+j+" "+rasp);
		            }

		    }

		}

		  if(x==1){contor++;
		    max=Math.min(max,rasp);}

		}

		if(contor==0) System.out.println(-1);
		else
		System.out.println(max);

}

}