import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Scanner;

public class B {
	int MAX = 200001;

	public int[] getPrimeTable(){
		boolean[] ret = new boolean[MAX+1];
		for(int i = 2; i <=  MAX; i++){
			ret[i] = true;
		}
		for(int i = 2; i <=  MAX; i++){
			if( ret[i] ){
				int j = i+i;
				while(j <= MAX){
					ret[j] = false;
					j = j + i;
				}
			}
		}

		int size = 0;
		for(int i = 1; i <=  MAX; i++){
			if(ret[i]) size++;
		}
		int[] rets = new int[size];
		int current = 0;
		for(int i = 1; i <=  MAX; i++){
			if(ret[i]){
				rets[current++] = i;
			}
		}
		return rets;
	}

	public int bs(int n, int[] prime){
		int start = 0;
		int end = prime.length-1;
		int mid = (start+end)/2;
		while( start < end ){
			if( n > prime[mid] ){
				start = mid+1;
			}
			else{
				end = mid;
			}
			mid = (start+end)/2;
		}
		return prime[mid];
	}

	public void solve(){
		PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
		Scanner sc = new Scanner(System.in);

		int N = sc.nextInt();
		int M = sc.nextInt();
		int[][] a = new int[N][M];

		int[] prime = getPrimeTable();
		for(int i = 0; i < N; i++){
			for(int j = 0; j < M; j++){
				int n = sc.nextInt();
				a[i][j] = bs(n, prime) - n;
//				System.out.println( a[i][j]);
			}
//			System.out.println();
		}

		long ans = Integer.MAX_VALUE;
		for(int i = 0; i < N; i++){
			long tmp = 0;
			for(int j = 0; j < M; j++){
				tmp = tmp + a[i][j];
			}
			ans = Math.min(ans, tmp);
		}

		for(int j = 0; j < M; j++){
			long tmp = 0;
			for(int i = 0; i < N; i++){
				tmp = tmp + a[i][j];
			}
			ans = Math.min(ans, tmp);
		}

//		for(int i = 0; i < 20; i++){
//			System.out.println(  prime[i]);
//		}

		out.println(ans);
		out.close();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new B().solve();
	}

}


import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Scanner;

public class P_271_B {

    static BitSet Primes = new BitSet(101000);
    static ArrayList<Integer> primes = new ArrayList<Integer>();
    static int arr[] = new int[1230];

    public static void main(String[] args) {
        Gen();

        int n, m;
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        m = in.nextInt();
        int[][] off = new int[n][m];
        int t;
        int[] rows = new int[n];
        int[] cols = new int[m];
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++) {
                t = in.nextInt();
                if (!Primes.get(t)) {
                    int s = Math.abs(Arrays.binarySearch(arr, t));
                    off[i][j] = arr[s - 1] - t;
                    // System.out.println(i+" "+j+" "+off[i][j]);
                    rows[i] += off[i][j];
                    cols[j] += off[i][j];
                }
            }
        int mini = rows[0];
        for (int i = 0; i < n; i++)
            mini = Math.min(mini, rows[i]);
        for (int i = 0; i < m; i++)
            mini = Math.min(mini, cols[i]);
        System.out.println(mini);

    }

    private static void Gen() {
        Primes.set(2, 101000 - 1);
        int n = 10100 - 1;
        primes.add(2);
        for (int i = 4; i <= n; i += 2)
            Primes.clear(i);
        for (int i = 3; i <= n; i++)
            if (Primes.get(i)) {
                int j = 2 * i;
                for (; j <= n; j += i)
                    Primes.clear(j);
                primes.add(i);
            }
        for (int i = 0; i < 1230; i++)
            arr[i] = primes.get(i);

    }

}


import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Scanner;

public class P_271_B {

    static BitSet Primes = new BitSet(101000);
    static ArrayList<Integer> primes = new ArrayList<Integer>();
    static int arr[] = new int[9598];

    public static void main(String[] args) {
        Gen();

        int n, m;
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        m = in.nextInt();

        int[][] off = new int[n][m];
        int t;
        int[] rows = new int[n];
        int[] cols = new int[m];
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++) {
                t = in.nextInt();

                if (!Primes.get(t)) {

                    int s = Math.abs(Arrays.binarySearch(arr, t));
                    off[i][j] = arr[s - 1] - t;

                    // System.out.println(i+" "+j+" "+off[i][j]);
                    rows[i] += off[i][j];
                    cols[j] += off[i][j];
                }
            }
        int mini = rows[0];
        for (int i = 0; i < n; i++)
            mini = Math.min(mini, rows[i]);
        for (int i = 0; i < m; i++)
            mini = Math.min(mini, cols[i]);
        System.out.println(mini);

    }

    private static void Gen() {
        Primes.set(2, 101000 - 1);
        int n = 100100 - 1;
        primes.add(2);
        for (int i = 4; i <= n; i += 2)
            Primes.clear(i);
        for (int i = 3; i <= n; i++)
            if (Primes.get(i)) {
                int j = 2 * i;
                for (; j <= n; j += i)
                    Primes.clear(j);
                primes.add(i);
            }

        for (int i = 0; i < 9598; i++)
            arr[i] = primes.get(i);

    }

}


import java.io.BufferedInputStream;
import java.util.*;
import static java.lang.Math.*;

public class C271B {

	boolean[] isPrime = new boolean[100042];

	public void solve() throws Exception {
		Arrays.fill(isPrime, true);
		isPrime[0] = isPrime[1] = false;
		for (int i = 2; i * i <= isPrime.length; i++) {
			if (isPrime[i]) {
				for (int j = i * i; j < isPrime.length; j += i) {
					isPrime[j] = false;
				}
			}
		}

		int n = nextInt();
		int m = nextInt();
		int[][] arr = new int[n][m];
		for (int i = 0; i < arr.length; i++) {
			for (int j = 0; j < arr[0].length; j++) {
				arr[i][j] = 0;
				int a = nextInt();
				for (int k = a; !isPrime[k]; k++) {
					arr[i][j]++;
				}
			}
		}
		int res = Integer.MAX_VALUE;
		for (int i = 0; i < arr.length; i++) {
			int ans = 0;
			for (int j = 0; j < arr[0].length; j++) {
				ans += arr[i][j];
			}
			res = min(res, ans);
		}

		for (int j = 0; j < arr[0].length; j++) {
			int ans = 0;
			for (int i = 0; i < arr.length; i++) {
				ans += arr[i][j];
			}
			res = min(res, ans);
		}
		println(res);

	}

	// ------------------------------------------------------

	void debug(Object... os) {
		System.err.println(Arrays.deepToString(os));
	}

	void print(Object... os) {
		if (os != null && os.length > 0)
			System.out.print(os[0].toString());
		for (int i = 1; i < os.length; ++i)
			System.out.print(" " + os[i].toString());
	}

	void println(Object... os) {
		print(os);
		System.out.println();
	}

	BufferedInputStream bis = new BufferedInputStream(System.in);

	String nextWord() throws Exception {
		char c = (char) bis.read();
		while (c <= ' ')
			c = (char) bis.read();
		StringBuilder sb = new StringBuilder();
		while (c > ' ') {
			sb.append(c);
			c = (char) bis.read();
		}
		return new String(sb);
	}

	String nextLine() throws Exception {
		char c = (char) bis.read();
		while (c <= ' ')
			c = (char) bis.read();
		StringBuilder sb = new StringBuilder();
		while (c != '\n' && c != '\r') {
			sb.append(c);
			c = (char) bis.read();
		}
		return new String(sb);
	}

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

	long nextLong() throws Exception {
		return Long.parseLong(nextWord());
	}

	public static void main(String[] args) throws Exception {
		new C271B().solve();
	}
}


import java.util.Scanner;

public class A {

    public static boolean a[] = new boolean[100050];

    public static void sieveAlgorithm() {

        for (int i = 2; i < a.length; i++) {
            a[i] = true;
        }
        boolean finished = false;
        for (int i = 2; i < a.length; i++) { // System.out.println("here");

            if (a[i] == false) {
                continue;
            }
            if (finished == true) {
                break;
            }
            finished = true;

                for (int j = 2 * i; j < a.length; j += i) {
                    if (a[j] == true) {
                        // System.out.println("here");
                        a[j] = false;
                         finished =false;
                    }
                }

        }

    }

    public static boolean isPrime(int x) {

        if (a[x] == true)
            return true;
        else {
            return false;
        }

    }

    public static int f(int a[][]) {
        boolean temp = false;
        int min =Integer.MAX_VALUE;
        int tempMin = 0;
        for (int r = 0; r < a.length; r++) {

            for (int c = 0; c < a[r].length; c++) {

                while (temp == false) {
                    // System.out.println("here");

                    temp = isPrime(a[r][c]);
                    // System.out.println("mm"+temp);

                    if (temp == false) {
                        a[r][c] += 1;
                        // System.out.println("here");

                        tempMin++;
                    } else
                        break;
                }
                temp = false;

            }
            if (tempMin < min) {
                min = tempMin;
            }
            tempMin = 0;
        }

        return min;
    }

    public static int f2(int a[][]) {
        boolean temp = false;
        int min =Integer.MAX_VALUE;
        int tempMin = 0;
        for (int c = 0; c < a[0].length; c++) {

            for (int r = 0; r < a.length; r++) {

                while (temp == false) {
                    // System.out.println("here");

                    temp = isPrime(a[r][c]);
                    // System.out.println("mm"+temp);

                    if (temp == false) {
                        a[r][c] += 1;
                        // System.out.println("here");

                        tempMin++;
                    } else
                        break;
                }
                temp = false;

            }
            if (tempMin < min) {
                min = tempMin;
            }
            tempMin = 0;
        }

        return min;
    }

    public static void main(String[] args) {
        sieveAlgorithm();

        Scanner in = new Scanner(System.in);

        int n = in.nextInt();
        int m = in.nextInt();
// 2 identical arrays to carry the input
        int array[][] = new int[n][m];
        int copy[][] = new int[n][m];
        // reading the input
        for (int r = 0; r < array.length; r++) {
            for (int c = 0; c < array[r].length; c++) {
                array[r][c] = in.nextInt();
            }
        }
//  copying the array
        for (int r = 0; r < array.length; r++) {
            for (int c = 0; c < array[r].length; c++) {
                copy[r][c] = array[r][c];
            }
        }
// checking the min number of operation due looping on all rows
        int x = f(array);
    // checking the mi num of operation due looping thriugh all coulomns
        int y = f2(copy);
// choosing the minimum between them
        int result = (x > y) ? y : x;
// finally printing the result
        System.out.println(result);

        // System.out.println(isPrime(2));

    }

}


import java.util.Scanner;

public class A {

    public static boolean isPrime(int x){

        if(x==1||x==0){

            return false;
        }

        for(int i=2;i<x;i++){

            if((x%i)==0){

                return false;

            }

        }

        return true;

    }

    public static int f(int a [][]){
        boolean temp=false;
        int min=10^5;
        int tempMin=0;
        for (int r = 0; r < a.length; r++) {

            for (int c = 0; c < a[r].length; c++) {

                while(temp==false){
                    //System.out.println("here");

                    temp=isPrime(a[r][c]);
                    //System.out.println("mm"+temp);

                 if(temp==false){
                     a[r][c]+=1;
                        //System.out.println("here");

                     tempMin++;
                 }
                 else
                     break;
                }
                temp=false;

            }
            if(tempMin<min)
            {
                min=tempMin;
            }
                tempMin=0;
        }

return min;
    }

    public static int f2(int a [][]){
        boolean temp=false;
        int min=10^5;
        int tempMin=0;
        for (int c = 0; c < a[0].length; c++) {

            for (int r = 0; r < a.length; r++) {

                while(temp==false){
                    //System.out.println("here");

                    temp=isPrime(a[r][c]);
                    //System.out.println("mm"+temp);

                 if(temp==false){
                     a[r][c]+=1;
                        //System.out.println("here");

                     tempMin++;
                 }
                 else
                     break;
                }
                temp=false;

            }
            if(tempMin<min)
            {
                min=tempMin;
            }
                tempMin=0;
        }

return min;
    }

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        int n = in.nextInt();
        int m = in.nextInt();

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

        int copy[][]=new int [n][m];
        //System.arraycopy(array, 0, copy, 0, array.length);
        for (int r = 0; r < array.length; r++) {

            for (int c = 0; c < array[r].length; c++) {

                array[r][c] = in.nextInt();

            }

        }

        for (int r = 0; r < array.length; r++) {

            for (int c = 0; c < array[r].length; c++) {

                copy[r][c]=array[r][c] ;

            }

        }

        int x=f(array);
        int y=f2(copy);
        //System.out.println(y);

        int result=(x>y)? y:x;
        System.out.println(result);

    //  System.out.print(isPrime(1));

    }

}


import java.util.Scanner;

public class A {

    public static boolean a[] = new boolean[100050];

    public static void sieveAlgorithm() {

        for (int i = 2; i < a.length; i++) {
            a[i] = true;
        }
        boolean finished = false;
        for (int i = 2; i < a.length; i++) { // System.out.println("here");

            if (a[i] == false) {
                continue;
            }
            if (finished == true) {
                break;
            }
            finished = true;

                for (int j = 2 * i; j < a.length; j += i) {
                    if (a[j] == true) {
                        // System.out.println("here");
                        a[j] = false;
                         finished =false;
                    }
                }

        }

    }

    public static boolean isPrime(int x) {

        if (a[x] == true)
            return true;
        else {
            return false;
        }

    }

    public static int f(int a[][]) {
        boolean temp = false;
        int min = 10 ^ 6;
        int tempMin = 0;
        for (int r = 0; r < a.length; r++) {

            for (int c = 0; c < a[r].length; c++) {

                while (temp == false) {
                    // System.out.println("here");

                    temp = isPrime(a[r][c]);
                    // System.out.println("mm"+temp);

                    if (temp == false) {
                        a[r][c] += 1;
                        // System.out.println("here");

                        tempMin++;
                    } else
                        break;
                }
                temp = false;

            }
            if (tempMin < min) {
                min = tempMin;
            }
            tempMin = 0;
        }

        return min;
    }

    public static int f2(int a[][]) {
        boolean temp = false;
        int min = 10 ^ 6;
        int tempMin = 0;
        for (int c = 0; c < a[0].length; c++) {

            for (int r = 0; r < a.length; r++) {

                while (temp == false) {
                    // System.out.println("here");

                    temp = isPrime(a[r][c]);
                    // System.out.println("mm"+temp);

                    if (temp == false) {
                        a[r][c] += 1;
                        // System.out.println("here");

                        tempMin++;
                    } else
                        break;
                }
                temp = false;

            }
            if (tempMin < min) {
                min = tempMin;
            }
            tempMin = 0;
        }

        return min;
    }

    public static void main(String[] args) {
        sieveAlgorithm();

        Scanner in = new Scanner(System.in);

        int n = in.nextInt();
        int m = in.nextInt();
// 2 identical arrays to carry the input
        int array[][] = new int[n][m];
        int copy[][] = new int[n][m];
        // reading the input
        for (int r = 0; r < array.length; r++) {
            for (int c = 0; c < array[r].length; c++) {
                array[r][c] = in.nextInt();
            }
        }
//  copying the array
        for (int r = 0; r < array.length; r++) {
            for (int c = 0; c < array[r].length; c++) {
                copy[r][c] = array[r][c];
            }
        }
// checking the min number of operation due looping on all rows
        int x = f(array);
    // checking the mi num of operation due looping thriugh all coulomns
        int y = f2(copy);
// choosing the minimum between them
        int result = (x > y) ? y : x;
// finally printing the result
        System.out.println(result);

         System.out.println(isPrime(2));

    }

}


import java.util.Scanner;

public class A {

    public static boolean a[] = new boolean[100050];

    public static void sieveAlgorithm() {

        for (int i = 2; i < a.length; i++) {
            a[i] = true;
        }
        boolean finished = false;
        for (int i = 2; i < a.length; i++) { // System.out.println("here");

            if (a[i] == false) {
                continue;
            }
            if (finished == true) {
                break;
            }
            finished = true;

                for (int j = 2 * i; j < a.length; j += i) {
                    if (a[j] == true) {
                        // System.out.println("here");
                        a[j] = false;
                         finished =false;
                    }
                }

        }

    }

    public static boolean isPrime(int x) {

        if (a[x] == true)
            return true;
        else {
            return false;
        }

    }

    public static int f(int a[][]) {
        boolean temp = false;
        int min = 10 ^ 5;
        int tempMin = 0;
        for (int r = 0; r < a.length; r++) {

            for (int c = 0; c < a[r].length; c++) {

                while (temp == false) {
                    // System.out.println("here");

                    temp = isPrime(a[r][c]);
                    // System.out.println("mm"+temp);

                    if (temp == false) {
                        a[r][c] += 1;
                        // System.out.println("here");

                        tempMin++;
                    } else
                        break;
                }
                temp = false;

            }
            if (tempMin < min) {
                min = tempMin;
            }
            tempMin = 0;
        }

        return min;
    }

    public static int f2(int a[][]) {
        boolean temp = false;
        int min = 10 ^ 5;
        int tempMin = 0;
        for (int c = 0; c < a[0].length; c++) {

            for (int r = 0; r < a.length; r++) {

                while (temp == false) {
                    // System.out.println("here");

                    temp = isPrime(a[r][c]);
                    // System.out.println("mm"+temp);

                    if (temp == false) {
                        a[r][c] += 1;
                        // System.out.println("here");

                        tempMin++;
                    } else
                        break;
                }
                temp = false;

            }
            if (tempMin < min) {
                min = tempMin;
            }
            tempMin = 0;
        }

        return min;
    }

    public static void main(String[] args) {
        sieveAlgorithm();

        Scanner in = new Scanner(System.in);

        int n = in.nextInt();
        int m = in.nextInt();

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

        int copy[][] = new int[n][m];
        // System.arraycopy(array, 0, copy, 0, array.length);
        for (int r = 0; r < array.length; r++) {

            for (int c = 0; c < array[r].length; c++) {

                array[r][c] = in.nextInt();

            }

        }

        for (int r = 0; r < array.length; r++) {

            for (int c = 0; c < array[r].length; c++) {

                copy[r][c] = array[r][c];

            }

        }

        int x = f(array);
        int y = f2(copy);
        // System.out.println(y);

        int result = (x > y) ? y : x;
        System.out.println(result);

        // System.out.print(isPrime(1));

        // System.out.println(isPrime(9));

    }

}


import java.util.Scanner;

public class A {

    public static boolean a[] = new boolean[105050];

    public static void sieveAlgorithm() {

        for (int i = 2; i < a.length; i++) {
            a[i] = true;
        }
        boolean finished = false;
        for (int i = 2; i < a.length; i++) { // System.out.println("here");

            if (a[i] == false) {
                continue;
            }
            if (finished == true) {
                break;
            }
            finished = true;

                for (int j = 2 * i; j < a.length; j += i) {
                    if (a[j] == true) {
                        // System.out.println("here");
                        a[j] = false;
                         finished =false;
                    }
                }

        }

    }

    public static boolean isPrime(int x) {

        if (a[x] == true)
            return true;
        else {
            return false;
        }

    }

    public static int f(int a[][]) {
        boolean temp = false;
        int min = 10 ^ 5;
        int tempMin = 0;
        for (int r = 0; r < a.length; r++) {

            for (int c = 0; c < a[r].length; c++) {

                while (temp == false) {
                    // System.out.println("here");

                    temp = isPrime(a[r][c]);
                    // System.out.println("mm"+temp);

                    if (temp == false) {
                        a[r][c] += 1;
                        // System.out.println("here");

                        tempMin++;
                    } else
                        break;
                }
                temp = false;

            }
            if (tempMin < min) {
                min = tempMin;
            }
            tempMin = 0;
        }

        return min;
    }

    public static int f2(int a[][]) {
        boolean temp = false;
        int min = 10 ^ 5;
        int tempMin = 0;
        for (int c = 0; c < a[0].length; c++) {

            for (int r = 0; r < a.length; r++) {

                while (temp == false) {
                    // System.out.println("here");

                    temp = isPrime(a[r][c]);
                    // System.out.println("mm"+temp);

                    if (temp == false) {
                        a[r][c] += 1;
                        // System.out.println("here");

                        tempMin++;
                    } else
                        break;
                }
                temp = false;

            }
            if (tempMin < min) {
                min = tempMin;
            }
            tempMin = 0;
        }

        return min;
    }

    public static void main(String[] args) {
        sieveAlgorithm();

        Scanner in = new Scanner(System.in);

        int n = in.nextInt();
        int m = in.nextInt();

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

        int copy[][] = new int[n][m];
        // System.arraycopy(array, 0, copy, 0, array.length);
        for (int r = 0; r < array.length; r++) {

            for (int c = 0; c < array[r].length; c++) {

                array[r][c] = in.nextInt();

            }

        }

        for (int r = 0; r < array.length; r++) {

            for (int c = 0; c < array[r].length; c++) {

                copy[r][c] = array[r][c];

            }

        }

        int x = f(array);
        int y = f2(copy);
        // System.out.println(y);

        int result = (x > y) ? y : x;
        System.out.println(result);

        // System.out.print(isPrime(1));

        // System.out.println(isPrime(9));

    }

}


import java.util.Scanner;

public class A {

    public static boolean a[] = new boolean[100050];

    public static void sieveAlgorithm() {

        for (int i = 2; i < a.length; i++) {
            a[i] = true;
        }
        boolean finished = false;
        for (int i = 2; i < a.length; i++) { // System.out.println("here");

            if (a[i] == false) {
                continue;
            }
            if (finished == true) {
                break;
            }
            finished = true;

                for (int j = 2 * i; j < a.length; j += i) {
                    if (a[j] == true) {
                        // System.out.println("here");
                        a[j] = false;
                         finished =false;
                    }
                }

        }

    }

    public static boolean isPrime(int x) {

        if (a[x] == true)
            return true;
        else {
            return false;
        }

    }

    public static int f(int a[][]) {
        boolean temp = false;
        int min = 10 ^ 6;
        int tempMin = 0;
        for (int r = 0; r < a.length; r++) {

            for (int c = 0; c < a[r].length; c++) {

                while (temp == false) {
                    // System.out.println("here");

                    temp = isPrime(a[r][c]);
                    // System.out.println("mm"+temp);

                    if (temp == false) {
                        a[r][c] += 1;
                        // System.out.println("here");

                        tempMin++;
                    } else
                        break;
                }
                temp = false;

            }
            if (tempMin < min) {
                min = tempMin;
            }
            tempMin = 0;
        }

        return min;
    }

    public static int f2(int a[][]) {
        boolean temp = false;
        int min = 10 ^ 6;
        int tempMin = 0;
        for (int c = 0; c < a[0].length; c++) {

            for (int r = 0; r < a.length; r++) {

                while (temp == false) {
                    // System.out.println("here");

                    temp = isPrime(a[r][c]);
                    // System.out.println("mm"+temp);

                    if (temp == false) {
                        a[r][c] += 1;
                        // System.out.println("here");

                        tempMin++;
                    } else
                        break;
                }
                temp = false;

            }
            if (tempMin < min) {
                min = tempMin;
            }
            tempMin = 0;
        }

        return min;
    }

    public static void main(String[] args) {
        sieveAlgorithm();

        Scanner in = new Scanner(System.in);

        int n = in.nextInt();
        int m = in.nextInt();
// 2 identical arrays to carry the input
        int array[][] = new int[n][m];
        int copy[][] = new int[n][m];
        // reading the input
        for (int r = 0; r < array.length; r++) {
            for (int c = 0; c < array[r].length; c++) {
                array[r][c] = in.nextInt();
            }
        }
//  copying the array
        for (int r = 0; r < array.length; r++) {
            for (int c = 0; c < array[r].length; c++) {
                copy[r][c] = array[r][c];
            }
        }
// checking the min number of operation due looping on all rows
        int x = f(array);
    // checking the mi num of operation due looping thriugh all coulomns
        int y = f2(copy);
// choosing the minimum between them
        int result = (x > y) ? y : x;
// finally printing the result
        System.out.println(result);

        // System.out.println(isPrime(2));

    }

}


import java.math.BigInteger;
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        //gen primes
        int n1 = 100000;
        TreeSet<Integer> t1 = new TreeSet();
        for (int i=1;i<n1/2+1;i++){
            t1.add(i*2+1);
        }
        int[] pl = new int[9593];
        int ptr = 0;
        pl[ptr++]=2;
        while (t1.size()!=0){
            int k1 = t1.pollFirst();
            for (int i=0;i<n1/k1+1;i++){
                t1.remove(k1*i);
            }
            pl[ptr++]=k1;
        }
        //System.out.println(Arrays.toString(pl));
        //System.out.println(Arrays.binarySearch(pl, 4));
        int x = Integer.parseInt(scan.next());
        int y = Integer.parseInt(scan.next());
        int[][] m2 = new int [x][y];
        int[][] m1 = new int [x][y];
        for (int i=0;i<x;i++){
            for (int j =0;j<y;j++){
                int cur = Integer.parseInt(scan.next());
                int temp = Arrays.binarySearch(pl, cur);
                int diff = -1;
                if (temp >=0){
                    diff = 0;
                }
                else {
                    diff = pl[-1*temp-1]-cur;
                }
                m2[i][j]=diff;
            }
        }
        /*
        for (int i=0;i<x;i++){
            for (int j =0;j<y;j++){
                int temp = Arrays.binarySearch(pl, m1[i][j]);
                int diff = -1;
                if (temp >=0){
                    diff = 0;
                }
                else {
                    diff = pl[-1*temp-1]-m1[i][j];
                }
                m2[i][j]=diff;
            }
        }
        */

        /*
        for (int[] e: m2){
            System.out.println(Arrays.toString(e));
        }
        */
        BigInteger min = null;
        //ArrayList<Integer> k1 = new ArrayList();
        //ArrayList<Integer> k2 = new ArrayList();
        for (int i=0;i<x;i++){
            BigInteger temp = new BigInteger(0+"");
            for (int j = 0;j<y;j++){
                temp=temp.add(new BigInteger(""+m2[i][j]));
            }
            //k1.add(temp);
            if (min == null)min=temp;
            else if (temp.compareTo(min)<0) min=temp;
        }
        for (int i=0;i<y;i++){
            BigInteger temp = new BigInteger(0+"");
            for (int j = 0;j<x;j++){
                temp=temp.add(new BigInteger(""+m2[j][i]));
            }
            //k2.add(temp);
            if (temp.compareTo(min)<0) min=temp;
        }
        //System.out.println(k1);
        //System.out.println(k2);
        System.out.println(min);
    }
}


import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        //gen primes
        int n1 = 100000;
        TreeSet<Integer> t1 = new TreeSet();
        for (int i=1;i<n1/2+1;i++){
            t1.add(i*2+1);
        }
        int[] pl = new int[9593];
        int ptr = 0;
        pl[ptr++]=2;
        while (t1.size()!=0){
            int k1 = t1.pollFirst();
            for (int i=0;i<n1/k1+1;i++){
                t1.remove(k1*i);
            }
            pl[ptr++]=k1;
        }
        //System.out.println(Arrays.toString(pl));
        //System.out.println(Arrays.binarySearch(pl, 4));
        int x = Integer.parseInt(scan.next());
        int y = Integer.parseInt(scan.next());
        int[][] m1 = new int [x][y];
        for (int i=0;i<x;i++){
            for (int j =0;j<y;j++){
                m1[i][j] = Integer.parseInt(scan.next());
            }
        }
        int[][] m2 = new int [x][y];
        for (int i=0;i<x;i++){
            for (int j =0;j<y;j++){
                int temp = Arrays.binarySearch(pl, m1[i][j]);
                int diff = -1;
                if (temp >=0){
                    diff = 0;
                }
                else {
                    diff = pl[-1*temp-1]-m1[i][j];
                }
                m2[i][j]=diff;
            }
        }
        /*
        for (int[] e: m2){
            System.out.println(Arrays.toString(e));
        }
        */
        int min = -1;
        for (int i=0;i<x;i++){
            int temp = 0;
            for (int j = 0;j<y;j++){
                temp+=m2[i][j];
            }
            if (min == -1)min=temp;
            else if (temp<min) min=temp;
        }
        for (int i=0;i<y;i++){
            int temp = 0;
            for (int j = 0;j<x;j++){
                temp+=m2[j][i];
            }
            if (temp<min) min=temp;
        }
        System.out.println(min);
    }
}


import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        //gen primes
        int n1 = 100000;
        TreeSet<Integer> t1 = new TreeSet();
        for (int i=1;i<n1/2+1;i++){
            t1.add(i*2+1);
        }
        int[] pl = new int[9593];
        int ptr = 0;
        pl[ptr++]=2;
        while (t1.size()!=0){
            int k1 = t1.pollFirst();
            for (int i=0;i<n1/k1+1;i++){
                t1.remove(k1*i);
            }
            pl[ptr++]=k1;
        }
        //System.out.println(Arrays.toString(pl));
        //System.out.println(Arrays.binarySearch(pl, 4));
        int x = Integer.parseInt(scan.next());
        int y = Integer.parseInt(scan.next());
        int[][] m2 = new int [x][y];
        int[][] m1 = new int [x][y];
        for (int i=0;i<x;i++){
            for (int j =0;j<y;j++){
                int cur = Integer.parseInt(scan.next());
                int temp = Arrays.binarySearch(pl, cur);
                int diff = -1;
                if (temp >=0){
                    diff = 0;
                }
                else {
                    diff = pl[-1*temp-1]-cur;
                }
                m2[i][j]=diff;
            }
        }
        /*
        for (int i=0;i<x;i++){
            for (int j =0;j<y;j++){
                int temp = Arrays.binarySearch(pl, m1[i][j]);
                int diff = -1;
                if (temp >=0){
                    diff = 0;
                }
                else {
                    diff = pl[-1*temp-1]-m1[i][j];
                }
                m2[i][j]=diff;
            }
        }
        */

        /*
        for (int[] e: m2){
            System.out.println(Arrays.toString(e));
        }
        */
        int min = -1;
        //ArrayList<Integer> k1 = new ArrayList();
        //ArrayList<Integer> k2 = new ArrayList();
        for (int i=0;i<x;i++){
            int temp = 0;
            for (int j = 0;j<y;j++){
                temp+=m2[i][j];
            }
            //k1.add(temp);
            if (min == -1)min=temp;
            else if (temp<min) min=temp;
        }
        for (int i=0;i<y;i++){
            int temp = 0;
            for (int j = 0;j<x;j++){
                temp+=m2[j][i];
            }
            //k2.add(temp);
            if (temp<min) min=temp;
        }
        //System.out.println(k1);
        //System.out.println(k2);
        System.out.println(min);
    }
}


import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        //gen primes
        int n1 = 100000;
        TreeSet<Integer> t1 = new TreeSet();
        for (int i=1;i<n1/2+1;i++){
            t1.add(i*2+1);
        }
        int[] pl = new int[9593];
        int ptr = 0;
        pl[ptr++]=2;
        while (t1.size()!=0){
            int k1 = t1.pollFirst();
            for (int i=0;i<n1/k1+1;i++){
                t1.remove(k1*i);
            }
            pl[ptr++]=k1;
        }
        //System.out.println(Arrays.toString(pl));
        //System.out.println(Arrays.binarySearch(pl, 4));
        int x = Integer.parseInt(scan.next());
        int y = Integer.parseInt(scan.next());
        int[][] m2 = new int [x][y];
        int[][] m1 = new int [x][y];
        for (int i=0;i<x;i++){
            for (int j =0;j<y;j++){
                int cur = Integer.parseInt(scan.next());
                int temp = Arrays.binarySearch(pl, cur);
                int diff = -1;
                if (temp >=0){
                    diff = 0;
                }
                else {
                    diff = pl[-1*temp-1]-cur;
                }
                m2[i][j]=diff;
            }
        }
        /*
        for (int i=0;i<x;i++){
            for (int j =0;j<y;j++){
                int temp = Arrays.binarySearch(pl, m1[i][j]);
                int diff = -1;
                if (temp >=0){
                    diff = 0;
                }
                else {
                    diff = pl[-1*temp-1]-m1[i][j];
                }
                m2[i][j]=diff;
            }
        }
        */
        for (int[] e: m2){
            System.out.println(Arrays.toString(e));
        }
        /*
        for (int[] e: m2){
            System.out.println(Arrays.toString(e));
        }
        */
        int min = -1;
        //ArrayList<Integer> k1 = new ArrayList();
        //ArrayList<Integer> k2 = new ArrayList();
        for (int i=0;i<x;i++){
            int temp = 0;
            for (int j = 0;j<y;j++){
                temp+=m2[i][j];
            }
            //k1.add(temp);
            if (min == -1)min=temp;
            else if (temp<min) min=temp;
        }
        for (int i=0;i<y;i++){
            int temp = 0;
            for (int j = 0;j<x;j++){
                temp+=m2[j][i];
            }
            //k2.add(temp);
            if (temp<min) min=temp;
        }
        //System.out.println(k1);
        //System.out.println(k2);
        System.out.println(min);
    }
}


import java.io.FileReader;
import java.math.BigInteger;
import java.util.*;
public class Main {
    public static void main(String[] args)
    throws Exception {
        Scanner scan = new Scanner(System.in);
        //gen primes
        int n1 = 100010;
        TreeSet<Integer> t1 = new TreeSet();
        for (int i=1;i<n1/2+100;i++){
            t1.add(i*2+1);
        }
        int[] pl = new int[9593];
        int ptr = 0;
        pl[ptr++]=2;
        while (t1.size()!=0){
            int k1 = t1.pollFirst();
            for (int i=0;i<n1/k1+1;i++){
                t1.remove(k1*i);
            }
            if (ptr<9593){
                pl[ptr++]=k1;
            }

        }
        //System.out.println(Arrays.toString(pl));
        //System.out.println(Arrays.binarySearch(pl, 4));
        int x = Integer.parseInt(scan.next());
        int y = Integer.parseInt(scan.next());
        int[][] m2 = new int [x][y];
        int[][] m1 = new int [x][y];
        for (int i=0;i<x;i++){
            for (int j =0;j<y;j++){
                int cur = Integer.parseInt(scan.next());
                m1[i][j] = cur;
                int temp = Arrays.binarySearch(pl, cur);
                int diff = -1;
                if (temp >=0){
                    diff = 0;
                }
                else {
                    diff = pl[-1*temp-1]-cur;
                }
                m2[i][j]=diff;
            }
        }
        /*
        for (int i=0;i<x;i++){
            for (int j =0;j<y;j++){
                int temp = Arrays.binarySearch(pl, m1[i][j]);
                int diff = -1;
                if (temp >=0){
                    diff = 0;
                }
                else {
                    diff = pl[-1*temp-1]-m1[i][j];
                }
                m2[i][j]=diff;
            }
        }
        */
        if (false){
            for (int[] e: m1){
                System.out.println(Arrays.toString(e));
            }
            System.out.println();
            for (int[] e: m2){
                System.out.println(Arrays.toString(e));
            }

        }
        /*
        for (int[] e: m2){
            System.out.println(Arrays.toString(e));
        }
        */
        BigInteger min = null;
        //ArrayList<BigInteger> k1 = new ArrayList();
        //ArrayList<BigInteger> k2 = new ArrayList();
        for (int i=0;i<x;i++){
            BigInteger temp = new BigInteger(0+"");
            for (int j = 0;j<y;j++){
                temp=temp.add(new BigInteger(""+m2[i][j]));
            }
            //k1.add(temp);
            if (min == null)min=temp;
            else if (temp.compareTo(min)<0) min=temp;
        }
        for (int i=0;i<y;i++){
            BigInteger temp = new BigInteger(0+"");
            for (int j = 0;j<x;j++){
                temp=temp.add(new BigInteger(""+m2[j][i]));
            }
            //k2.add(temp);
            if (temp.compareTo(min)<0) min=temp;
        }
        //System.out.println(k1);
        //System.out.println(k2);
        System.out.println(min);
    }
}


//package CF_166_Div_2;

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

public class B implements Runnable{

    public static void main(String args[]){
       new B().run();
    }

    // public static final String INPUT_FILE = "input.in";
    // public static final String OUTPUT_FILE = "output.out";

    @Override
    public void run(){
        try{
//       in = new BufferedReader(new FileReader(INPUT_FILE));
//        out = new PrintWriter(new BufferedWriter(new FileWriter(OUTPUT_FILE)));
          in = new BufferedReader(new InputStreamReader(System.in));
           out = new PrintWriter(System.out);
            tok = null;
            solve();
            in.close();
            out.close();

        }
        catch(IOException e){
            e.printStackTrace();
            System.exit(0);
        }
    }

    int nextInt()throws IOException{
        return Integer.parseInt(nextToken());
    }

    double nextDouble()throws IOException{
        return Double.parseDouble(nextToken());
    }

    long nextLong() throws IOException{
        return Long.parseLong(nextToken());
    }

    String nextToken()throws IOException{
        while(tok == null || !tok.hasMoreTokens()){
            tok = new StringTokenizer(in.readLine());
        }
        return tok.nextToken();
    }

    BufferedReader in;
    PrintWriter out;
    StringTokenizer tok;
    public static final int LIMIT = 200000;
    //////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////
    private void solve()throws IOException{

       int N = nextInt();
       int M = nextInt();
       int[][] real = new int[N][M];
       for(int i = 0; i < N; i++){
           for(int j = 0; j < M; j++) real[i][j] = nextInt();
       }

       boolean[] isprime = new boolean[LIMIT];
       Arrays.fill(isprime, true);
       isprime[0] = isprime[1] = false;

       for(int i = 2; i * i < LIMIT; i++)
           if(isprime[i])
               for(int j = i * i; j < LIMIT; j += i ) isprime[j] = false;

       int[][] diff = new int[N][M];
       for(int i = 0; i < N; i++){
           for(int j = 0; j < M; j++){
               int curr = real[i][j];
               while(!isprime[curr])curr++;
               diff[i][j] = curr - real[i][j];
           }
       }

       int[] row = new int[N];
       int[] col = new int[M];
       for(int i = 0; i < N; i++){
           for(int j = 0; j < M; j++){
               row[i] += diff[i][j];
               col[j] += diff[i][j];
           }
       }

       int ans = Integer.MAX_VALUE;
       for(int j = 0; j < N; j++) ans = Math.min(ans, row[j]);
       for(int j = 0; j < N; j++) ans = Math.min(ans, col[j]);

       System.out.println(ans);
    }//solve

}


//package CF_166_Div_2;

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

public class B implements Runnable{

    public static void main(String args[]){
       new B().run();
    }

    // public static final String INPUT_FILE = "input.in";
    // public static final String OUTPUT_FILE = "output.out";

    @Override
    public void run(){
        try{
//       in = new BufferedReader(new FileReader(INPUT_FILE));
//        out = new PrintWriter(new BufferedWriter(new FileWriter(OUTPUT_FILE)));
          in = new BufferedReader(new InputStreamReader(System.in));
           out = new PrintWriter(System.out);
            tok = null;
            solve();
            in.close();
            out.close();

        }
        catch(IOException e){
            e.printStackTrace();
            System.exit(0);
        }
    }

    int nextInt()throws IOException{
        return Integer.parseInt(nextToken());
    }

    double nextDouble()throws IOException{
        return Double.parseDouble(nextToken());
    }

    long nextLong() throws IOException{
        return Long.parseLong(nextToken());
    }

    String nextToken()throws IOException{
        while(tok == null || !tok.hasMoreTokens()){
            tok = new StringTokenizer(in.readLine());
        }
        return tok.nextToken();
    }

    BufferedReader in;
    PrintWriter out;
    StringTokenizer tok;
    public static final int LIMIT = 200000;
    //////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////
    private void solve()throws IOException{

       int N = nextInt();
       int M = nextInt();
       int[][] real = new int[N][M];
       for(int i = 0; i < N; i++){
           for(int j = 0; j < M; j++) real[i][j] = nextInt();
       }

       boolean[] isprime = new boolean[LIMIT];
       Arrays.fill(isprime, true);
       isprime[0] = isprime[1] = false;

       for(int i = 2; i * i < LIMIT; i++)
           if(isprime[i])
               for(int j = i * i; j < LIMIT; j += i ) isprime[j] = false;

       int[][] diff = new int[N][M];
       for(int i = 0; i < N; i++){
           for(int j = 0; j < M; j++){
               int curr = real[i][j];
               while(!isprime[curr])curr++;
               diff[i][j] = curr - real[i][j];
           }
       }

       int[] row = new int[N];
       int[] col = new int[M];
       for(int i = 0; i < N; i++){
           for(int j = 0; j < M; j++){
               row[i] += diff[i][j];
               col[j] += diff[i][j];
           }
       }

       int ans = Integer.MAX_VALUE;
       for(int j = 0; j < N; j++) ans = Math.min(ans, row[j]);
       for(int j = 0; j < M; j++) ans = Math.min(ans, col[j]);

       System.out.println(ans);
    }//solve

}


//package CF_166_Div_2;

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

public class B implements Runnable{

    public static void main(String args[]){
       new B().run();
    }

    // public static final String INPUT_FILE = "input.in";
    // public static final String OUTPUT_FILE = "output.out";

    @Override
    public void run(){
        try{
//       in = new BufferedReader(new FileReader(INPUT_FILE));
//        out = new PrintWriter(new BufferedWriter(new FileWriter(OUTPUT_FILE)));
          in = new BufferedReader(new InputStreamReader(System.in));
           out = new PrintWriter(System.out);
            tok = null;
            solve();
            in.close();
            out.close();

        }
        catch(IOException e){
            e.printStackTrace();
            System.exit(0);
        }
    }

    int nextInt()throws IOException{
        return Integer.parseInt(nextToken());
    }

    double nextDouble()throws IOException{
        return Double.parseDouble(nextToken());
    }

    long nextLong() throws IOException{
        return Long.parseLong(nextToken());
    }

    String nextToken()throws IOException{
        while(tok == null || !tok.hasMoreTokens()){
            tok = new StringTokenizer(in.readLine());
        }
        return tok.nextToken();
    }

    BufferedReader in;
    PrintWriter out;
    StringTokenizer tok;
    //////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////
    private void solve()throws IOException{

       int N = nextInt();
       int M = nextInt();
       int[][] real = new int[N][M];
       for(int i = 0; i < N; i++){
           for(int j = 0; j < M; j++) real[i][j] = nextInt();
       }

       boolean[] isprime = new boolean[111111];
       Arrays.fill(isprime, true);
       isprime[0] = isprime[1] = false;

       for(int i = 2; i * i < 111111; i++)
           if(isprime[i])
               for(int j = i * i; j < 111111; j += i ) isprime[j] = false;

       int[][] diff = new int[N][M];
       for(int i = 0; i < N; i++){
           for(int j = 0; j < M; j++){
               int curr = real[i][j];
               while(!isprime[curr])curr++;
               diff[i][j] = curr - real[i][j];
           }
       }

       int[] row = new int[N];
       int[] col = new int[M];
       for(int i = 0; i < N; i++){
           for(int j = 0; j < M; j++){
               row[i] += diff[i][j];
               col[j] += diff[i][j];
           }
       }

       int ans = Integer.MAX_VALUE;
       for(int j = 0; j < N; j++) ans = Math.min(ans, row[j]);
       for(int j = 0; j < N; j++) ans = Math.min(ans, col[j]);

       System.out.println(ans);
    }//solve

}


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

public class B implements Runnable {

	final int cnt = 200000;

	private void Solution() throws IOException {
		boolean[] prime = new boolean[2 * cnt + 1];
		Arrays.fill(prime, true);
		prime[0] = prime[1] = false;
		for (int i = 2; i * i <= 2 * cnt; i++)
			if (prime[i])
				for (int j = i * i; j <= 2 * cnt; j += i)
					prime[j] = false;
		int[] res = new int[cnt + 1];
		for (int i = 1; i <= cnt; i++)
			for (int j = i; j <= 2 * cnt; j++)
				if (prime[j]) {
					res[i] = j;
					break;
				}
		int n = nextInt(), m = nextInt();
		int[][] a = new int[n][m];
		for (int i = 0; i < n; i++)
			for (int j = 0; j < m; j++)
				a[i][j] = nextInt();
		int min = Integer.MAX_VALUE;
		for (int i = 0; i < n; i++) {
			int cur = 0;
			for (int j = 0; j < m; j++)
				cur += res[a[i][j]] - a[i][j];
			min = Math.min(min, cur);
		}
		for (int i = 0; i < m; i++) {
			int cur = 0;
			for (int j = 0; j < n; j++)
				cur += res[a[j][i]] - a[j][i];
			min = Math.min(min, cur);
		}
		System.out.println(min);
	}

	public static void main(String[] args) {
		new B().run();
	}

	BufferedReader in;
	PrintWriter out;
	StringTokenizer tokenizer;

	public void run() {
		try {
			in = new BufferedReader(new InputStreamReader(System.in));
			out = new PrintWriter(System.out);
			Solution();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(261);
		}
	}

	void halt() {
		out.close();
		System.exit(0);
	}

	void print(Object... obj) {
		for (int i = 0; i < obj.length; i++) {
			if (i != 0)
				out.print(" ");
			out.print(obj[i]);
		}
	}

	void println(Object... obj) {
		print(obj);
		out.println();
	}

	int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	long nextLong() throws NumberFormatException, IOException {
		return Long.parseLong(nextToken());
	}

	double nextDouble() throws NumberFormatException, IOException {
		return Double.parseDouble(nextToken());
	}

	String nextLine() throws IOException {
		return in.readLine();
	}

	String nextToken() throws IOException {
		while (tokenizer == null || !tokenizer.hasMoreTokens())
			tokenizer = new StringTokenizer(nextLine());
		return tokenizer.nextToken();
	}
}


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

public class Main{

	public static boolean[] prime;
	public static int min;

	public static int getnext(int n){
		while (prime[n]) {
			n++;
		}
		return n;
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		 prime = new boolean[100010];
		 min = Integer.MAX_VALUE;
		 prime[0] = true;
		ArrayList<Integer> list = new ArrayList<Integer>();
		int temp = 1;
		for (int i = 2; i < prime.length; i++) {
			if(!prime[i]){
				//list.add(i);
				int nex = i;
				while (temp<prime.length) {
					try{
					prime[temp] = true;
					}catch(Exception e){
						break;
					}
					temp = nex*i;
					nex++;
				}
				temp = 1;
			}
		}
		//Integer[] z = new Integer[list.size()];
		//z = list.toArray(new Integer[1]);
		Scanner scanner = new Scanner(System.in);
		int row = scanner.nextInt();
		int colum = scanner.nextInt();
		int[][] matrix = new int[row][colum];
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < colum; j++) {
				matrix[i][j] = scanner.nextInt();
			}
		}
		////check rows
		for (int i = 0; i < row; i++) {
			int minim = 0;
			for (int j = 0; j < colum; j++) {
				minim = minim + getnext(matrix[i][j]) - matrix[i][j];
			}
			if(minim<min)
				min = minim;
		}
		///check column
		for (int i = 0; i < colum; i++) {
			int minim = 0;
			for (int j = 0; j < row; j++) {
				minim = minim + getnext(matrix[j][i]) - matrix[j][i];
			}
			if(minim<min)
				min = minim;
		}
		System.out.println(min);
	}

}


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

public class Main{

	public static boolean[] prime;
	public static int min;

	public static int getnext(int n){
		while (prime[n]) {
			n++;
		}
		return n;
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		 prime = new boolean[100001];
		 min = Integer.MAX_VALUE;
		 prime[0] = true;
		ArrayList<Integer> list = new ArrayList<Integer>();
		int temp = 1;
		for (int i = 2; i < prime.length; i++) {
			if(!prime[i]){
				//list.add(i);
				int nex = i;
				while (temp<prime.length) {
					try{
					prime[temp] = true;
					}catch(Exception e){
						break;
					}
					temp = nex*i;
					nex++;
				}
				temp = 1;
			}
		}
		//Integer[] z = new Integer[list.size()];
		//z = list.toArray(new Integer[1]);
		Scanner scanner = new Scanner(System.in);
		int row = scanner.nextInt();
		int colum = scanner.nextInt();
		int[][] matrix = new int[row][colum];
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < colum; j++) {
				matrix[i][j] = scanner.nextInt();
			}
		}
		////check rows
		for (int i = 0; i < row; i++) {
			int minim = 0;
			for (int j = 0; j < colum; j++) {
				minim = minim + getnext(matrix[i][j]) - matrix[i][j];
			}
			if(minim<min)
				min = minim;
		}
		///check column
		for (int i = 0; i < colum; i++) {
			int minim = 0;
			for (int j = 0; j < row; j++) {
				minim = minim + getnext(matrix[j][i]) - matrix[j][i];
			}
			if(minim<min)
				min = minim;
		}
		System.out.println(min);
	}

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    public static boolean[] criba = new boolean[100004];

    public static void numerosPrimos() {
        for (long i = 2; i < 100004; i++) {
            if (criba[(int) i])
                continue;
            for (long j = i; i * j < 100004; j++)
                criba[(int) (i * j)] = true;
        }
    }

    public static int nextPrime(int n){
        if(!criba[n])
            return 0;
        for (int i = n+1; i < 100004; i++)
            if(!criba[i])
                return i-n;
        return 0;
    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();
        String line = "";
        criba[0]=criba[1] = true;
        numerosPrimos();
        d: do {
            line = br.readLine();
            if (line == null || line.length() == 0)
                break d;
            String[] nums = line.split(" ");
            int aux = 0, min = Integer.MAX_VALUE;
            int f = Integer.parseInt(nums[0]);
            int c = Integer.parseInt(nums[1]);
            int[] columnas = new int[c];
            int[][] m = new int[f][c];
            int[][] m2 = new int[f][c];
            for (int i = 0; i < m.length; i++) {
                aux = 0;
                line = br.readLine();
                nums = line.split(" ");
                for (int j = 0; j < m[i].length; j++) {
                    m[i][j] = Integer.parseInt(nums[j]);
                    m2[i][j] = nextPrime(m[i][j]);
                    aux += m2[i][j];
                    columnas[j] += m2[i][j];
                }
                min = (aux < min) ? aux : min;
            }
            for (int i = 0; i < columnas.length; i++)
                min = (columnas[i] < min) ? columnas[i] : min;
            sb.append(min).append("\n");
        } while (line != null && line.length() != 0);
        System.out.print(sb);
    }

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();
        String line = "";
        d: do {
            line = br.readLine();
            if (line == null || line.length() == 0)
                break d;
            String[] nums = line.split(" ");
            int f = Integer.parseInt(nums[0]);
            int c = Integer.parseInt(nums[1]);
            int[][] m = new int[f][c];
            int[][] m2 = new int[f][c];
            for (int i = 0; i < m.length; i++) {
                line = br.readLine();
                nums = line.split(" ");
                for (int j = 0; j < m[i].length; j++) {
                    m[i][j] = Integer.parseInt(nums[j]);
                    if (BigInteger.valueOf(m[i][j]).isProbablePrime(100))
                        m2[i][j] = 0;
                    else
                        m2[i][j] = BigInteger.valueOf(m[i][j])                            .nextProbablePrime().intValue()- m[i][j];
                }
            }
            int min = Integer.MAX_VALUE, aux = 0;
            for (int i = 0; i < m2.length; i++){
                aux = 0;
                for (int j = 0; j < m2[i].length; j++)
                    aux+=m2[i][j];
                min = (aux<min)?aux:min;
            }
            for (int i = 0; i < m2[0].length; i++){
                aux = 0;
                for (int j = 0; j < m2.length; j++)
                    aux+=m2[j][i];
                min = (aux<min)?aux:min;
            }
            sb.append(min).append("\n");
        } while (line != null && line.length() != 0);
        System.out.print(sb);
    }

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();
        String line = "";
        d: do {
            line = br.readLine();
            if (line == null || line.length() == 0)
                break d;
            String[] nums = line.split(" ");
            int aux = 0, min = Integer.MAX_VALUE;
            int f = Integer.parseInt(nums[0]);
            int c = Integer.parseInt(nums[1]);
            int[] columnas = new int[c];
            int[][] m = new int[f][c];
            int[][] m2 = new int[f][c];
            for (int i = 0; i < m.length; i++) {
                aux = 0;
                line = br.readLine();
                nums = line.split(" ");
                for (int j = 0; j < m[i].length; j++) {
                    m[i][j] = Integer.parseInt(nums[j]);
                    if (BigInteger.valueOf(m[i][j]).isProbablePrime(100))
                        m2[i][j] = 0;
                    else
                        m2[i][j] = BigInteger.valueOf(m[i][j]).nextProbablePrime().intValue()- m[i][j];
                    aux+=m2[i][j];
                    columnas[j]+=m2[i][j];
                }
                min = (aux<min)?aux:min;
            }
            for (int i = 0; i < columnas.length; i++)
                min = (columnas[i]<min)?columnas[i]:min;
            sb.append(min).append("\n");
        } while (line != null && line.length() != 0);
        System.out.print(sb);
    }
}


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

public class Solution {
    public static void main (String[] args) throws IOException {
        boolean online = "true".equals(System.getProperty("ONLINE_JUDGE"));
        if (online) {
            in = new Scanner(System.in);
            out = new PrintWriter(System.out);
        }
        else {
            in = new Scanner(new File("input.txt"));
            out = new PrintWriter(new File("output.txt"));
        }

        new Solution().run();

        out.close();
    }

    private void run () {
        boolean[] pr = new boolean[N+1];
        Arrays.fill(pr, true);
        pr[0] = pr[1] = false;
        for (int i = 2; i <= N; ++i)
            if (pr[i])
                if (i * 1l * i <= N)
                    for (int j = i*i; j <= N; j += i)
                        pr[j] = false;
        int[] next = new int[max+1];
        int cur = 100003;
        for (int i = max; i > 0; --i) {
            if (pr[i]) cur = i;
            next[i] = cur;
        }
        int n = in.nextInt(),
            m = in.nextInt();
        int[] r = new int[n],
            c = new int[m];
        for (int i = 0; i < n; ++i)
            for (int j = 0; j < m; ++j) {
                int x = in.nextInt();
                r[i] += next[x] - x;
                c[j] += next[x] - x;
            }
        int ans = INF;
        for (int i = 0; i < n; ++i)
            ans = Math.min(ans, r[i]);
        for (int j = 0; j < m; ++j)
            ans = Math.min(ans, c[j]);
        out.println(ans);
    }

    private static final int INF = (int)2e9,
                            N = (int)2e5,
                            max = (int)1e5;

    private static Scanner in;
    private static PrintWriter out;
}


import java.util.*;

public class Main {
    public static void main(String args[]) {
        boolean[] prime = new boolean[100001];
        Arrays.fill(prime, true);
        prime[0] = false;
        prime[1] = false;
        int m = (int) Math.sqrt(100001);
        TreeMap<Integer, Integer> map = new TreeMap<Integer, Integer>();

        for (int i = 2; i <= m; i++) {
            if (prime[i]) {
                map.put(i, 0);
                for (int k = i * i; k <= 100000; k += i)
                    prime[k] = false;
            }
        }
        Scanner in = new Scanner(System.in);
        int N = in.nextInt();
        int M = in.nextInt();
        int matrix[][] = new int[N][M];
        int rowsum[] = new int[N];
        int colsum[] = new int[M];
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                int num = in.nextInt();
                if (prime[num])
                    matrix[i][j] = 0;
                else
                    matrix[i][j] = map.ceilingKey(num) - num;
                rowsum[i] += matrix[i][j];
                colsum[j] += matrix[i][j];
            }
        }
        Arrays.sort(rowsum);
        Arrays.sort(colsum);
        System.out.println(Math.min(rowsum[0], colsum[0]));
    }
}


import java.util.*;

public class Main {
    public static void main(String args[]) {
        int MAX = 100500;
        boolean[] prime = new boolean[MAX];
        Arrays.fill(prime, true);
        prime[0] = false;
        prime[1] = false;
        // int m = (int) Math.sqrt(MAX);
        TreeMap<Integer, Integer> map = new TreeMap<Integer, Integer>();

        for (int i = 2; i <= prime.length; i++) {
            if (prime[i]) {
                map.put(i, 0);
                for (int k = i * i; k <= prime.length; k += i)
                    prime[k] = false;
            }
        }
        Scanner in = new Scanner(System.in);
        int N = in.nextInt();
        int M = in.nextInt();
        int matrix[][] = new int[N][M];
        int rowsum[] = new int[N];
        int colsum[] = new int[M];
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                int num = in.nextInt();
                if (prime[num])
                    matrix[i][j] = 0;
                else
                    matrix[i][j] = map.ceilingKey(num) - num;
                rowsum[i] += matrix[i][j];
                colsum[j] += matrix[i][j];
            }
        }
        Arrays.sort(rowsum);
        Arrays.sort(colsum);
        System.out.println(Math.min(rowsum[0], colsum[0]));
    }
}


import java.util.*;

public class B271 {
    public static void main(String args[]) {
        int MAX = 100500;
        boolean[] prime = new boolean[MAX];
        Arrays.fill(prime, true);
        prime[0] = false;
        prime[1] = false;
        // int m = (int) Math.sqrt(MAX);
        TreeMap<Integer, Integer> map = new TreeMap<Integer, Integer>();

        for (int i = 2; i < prime.length; i++) {
            if (prime[i]) {
                map.put(i, 0);
                for (int j = i * 2; j < prime.length; j += i) {
                    prime[j] = false;
                }
            }
        }
        Scanner in = new Scanner(System.in);
        int N = in.nextInt();
        int M = in.nextInt();
        int matrix[][] = new int[N][M];
        int rowsum[] = new int[N];
        int colsum[] = new int[M];
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                int num = in.nextInt();
                if (prime[num])
                    matrix[i][j] = 0;
                else
                    matrix[i][j] = map.ceilingKey(num) - num;
                rowsum[i] += matrix[i][j];
                colsum[j] += matrix[i][j];
            }
        }
        Arrays.sort(rowsum);
        Arrays.sort(colsum);
        System.out.println(Math.min(rowsum[0], colsum[0]));
    }
}


import java.io.PrintWriter;
import java.util.Scanner;

public class Classy {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		PrintWriter out = new PrintWriter(System.out);
		int N = sc.nextInt();
		int M = sc.nextInt();
		long ans = Long.MAX_VALUE;
		primeSieve PS = new primeSieve(200000);
		int[] top = new int[M];
		int[] side = new int[N];
		for(int a=0;a<N;a++){
			for(int b=0;b<M;b++){
				long res = magic(sc.nextInt(),PS);
				top[b]+=res;
				side[a]+=res;
			//	System.out.print(res+" ");
			}
		//	System.out.println();
		}
		for(int a=0;a<M;a++)ans=Math.min(ans, top[a]);

		for(int a=0;a<N;a++)ans=Math.min(ans, side[a]);

		System.out.println(ans);
		out.close();
	}

	private static long magic(int a, primeSieve PS) {
		int d = 0;
		while(!PS.isPrime(a+d))d++;
		return d;
	}

	//N log N
	static class primeSieve {
		int[] prime;
		primeSieve(int n){
			prime = new int[n+1];
			prime[1] = 1;
			int m = (int)Math.sqrt(n);
			for(int i=2;i<=m;i++){
				if(prime[i]==0){
					for(int k=i*i;k<=n;k+=i){
						if(prime[k]==0)prime[k]=i;
					}
					prime[i]=i;
				}

			}
		}
		boolean isPrime(int n){
			if(n<=1)return false;
			return prime[n]==0||prime[n]==n;
		}
	}

}


import java.util.ArrayList;
import java.util.BitSet;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

public class primematrix {
public static void main(String[] args)
{
    Scanner s=new Scanner(System.in);
    int n=s.nextInt();
    int m=s.nextInt();
    int[][] matrix=new int[n][m];
    ArrayList<Integer> list=Sieve.sieve_of_eratosthenes(1000000);
    int[] array=convertIntegers(list);
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++){
            matrix[i][j]=binarysearch(array,s.nextInt());
        }
    int min=Integer.MAX_VALUE;
    for(int i=0;i<n;i++)
    {  int k=sumrow(matrix,i);
        if(k<=min)
            min=k;
    }
    for(int i=0;i<m;i++)
    { int k=sumcolumn(matrix,i,n);
        if(k<=min)
           min=k;
    }

    System.out.println(min);
}

public static int[] convertIntegers(ArrayList<Integer> integers)
{
    int[] ret = new int[integers.size()];
    Iterator<Integer> iterator = integers.iterator();
    for (int i = 0; i < ret.length; i++)
    {
        ret[i] = iterator.next().intValue();
    }
    return ret;
}
public static int binarysearch(int[] a,int x)
{
    int low=0;
    int high=a.length-1;
    int mid;

    while(low<=high)
    {
        mid=(low+high)/2;
        if(x>(a[mid]))
            low=mid+1;
        else if(x<a[mid])
            high=mid-1;
        else
            return 0;

    }
    return a[low]-x;

}
static class Sieve
{
    private BitSet sieve;

    public Sieve(int size) {
        sieve = new BitSet((size+1)/2);
    }

    public boolean is_composite(int k)
    {
        assert k >= 3 && (k % 2) == 1;
        return sieve.get((k-3)/2);
    }

    public void set_composite(int k)
    {
        assert k >= 3 && (k % 2) == 1;
        sieve.set((k-3)/2);
    }

    public static ArrayList<Integer> sieve_of_eratosthenes(int max)
    {
        Sieve sieve = new Sieve(max + 1); // +1 to include max itself
        for (int i = 3; i*i <= max; i += 2) {
            if (sieve.is_composite(i))
                continue;

            // We increment by 2*i to skip even multiples of i
            for (int multiple_i = i*i; multiple_i <= max; multiple_i += 2*i)
                sieve.set_composite(multiple_i);

        }
        ArrayList<Integer> primes = new ArrayList<Integer>();
        primes.add(2);
        for (int i = 3; i <= max; i += 2)
            if (!sieve.is_composite(i))
                primes.add(i);

        return primes;
    }
}

public static int sumrow(int[][] x,int n)
{
    int sum=0;

    for(int i=0;i<x[0].length;i++)
        sum=sum+x[n][i];
    return sum;
}
public static int sumcolumn(int[][] x,int m,int n)
{
    int sum=0;
    for(int i=0;i<n;i++)
    {
        sum=sum+x[i][m];
    }
    return sum;
}
}


import java.util.ArrayList;
import java.util.BitSet;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

public class primematrix {
public static void main(String[] args)
{
    Scanner s=new Scanner(System.in);
    int n=s.nextInt();
    int m=s.nextInt();
    int[][] matrix=new int[n][m];
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++){
            matrix[i][j]=closestprime(s.nextInt());
        }
    int min=Integer.MAX_VALUE;
    for(int i=0;i<n;i++)
    {  int k=sumrow(matrix,i);
        if(k<=min)
            min=k;
    }
    for(int i=0;i<m;i++)
    { int k=sumcolumn(matrix,i,n);
        if(k<=min)
           min=k;
    }

    System.out.println(min);
}
public static <T> int closestprime(int n)
{
    ArrayList<Integer> list=Sieve.sieve_of_eratosthenes(100000);
    return binarysearch(convertIntegers(list),n);
}

public static int[] convertIntegers(List<Integer> integers)
{
    int[] ret = new int[integers.size()];
    Iterator<Integer> iterator = integers.iterator();
    for (int i = 0; i < ret.length; i++)
    {
        ret[i] = iterator.next().intValue();
    }
    return ret;
}
public static int binarysearch(int[] a,int x)
{
    int low=0;
    int high=a.length-1;
    int mid;

    while(low<=high)
    {
        int l=low;
        int h=high;
        mid=(low+high)/2;
        if(x>(a[mid]))
            low=mid+1;
        else if(x<a[mid])
            high=mid-1;
        else
            return 0;

    }
    return a[low]-x;

}
static class Sieve
{
    private BitSet sieve;

    public Sieve(int size) {
        sieve = new BitSet((size+1)/2);
    }

    public boolean is_composite(int k)
    {
        assert k >= 3 && (k % 2) == 1;
        return sieve.get((k-3)/2);
    }

    public void set_composite(int k)
    {
        assert k >= 3 && (k % 2) == 1;
        sieve.set((k-3)/2);
    }

    public static ArrayList<Integer> sieve_of_eratosthenes(int max)
    {
        Sieve sieve = new Sieve(max + 1); // +1 to include max itself
        for (int i = 3; i*i <= max; i += 2) {
            if (sieve.is_composite(i))
                continue;

            // We increment by 2*i to skip even multiples of i
            for (int multiple_i = i*i; multiple_i <= max; multiple_i += 2*i)
                sieve.set_composite(multiple_i);

        }
        ArrayList<Integer> primes = new ArrayList<Integer>();
        primes.add(2);
        for (int i = 3; i <= max; i += 2)
            if (!sieve.is_composite(i))
                primes.add(i);

        return primes;
    }
}

public static int sumrow(int[][] x,int n)
{
    int sum=0;

    for(int i=0;i<x[0].length;i++)
        sum=sum+x[n][i];
    return sum;
}
public static int sumcolumn(int[][] x,int m,int n)
{
    int sum=0;
    for(int i=0;i<n;i++)
    {
        sum=sum+x[i][m];
    }
    return sum;
}
}


import java.util.ArrayList;
import java.util.BitSet;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

public class primematrix {
public static void main(String[] args)
{
    Scanner s=new Scanner(System.in);
    int n=s.nextInt();
    int m=s.nextInt();
    int[][] matrix=new int[n][m];
    ArrayList<Integer> list=Sieve.sieve_of_eratosthenes(100000);
    int[] array=convertIntegers(list);
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++){
            matrix[i][j]=binarysearch(array,s.nextInt());
        }
    int min=Integer.MAX_VALUE;
    for(int i=0;i<n;i++)
    {  int k=sumrow(matrix,i);
        if(k<=min)
            min=k;
    }
    for(int i=0;i<m;i++)
    { int k=sumcolumn(matrix,i,n);
        if(k<=min)
           min=k;
    }

    System.out.println(min);
}

public static int[] convertIntegers(List<Integer> integers)
{
    int[] ret = new int[integers.size()];
    Iterator<Integer> iterator = integers.iterator();
    for (int i = 0; i < ret.length; i++)
    {
        ret[i] = iterator.next().intValue();
    }
    return ret;
}
public static int binarysearch(int[] a,int x)
{
    int low=0;
    int high=a.length-1;
    int mid;

    while(low<=high)
    {
        int l=low;
        int h=high;
        mid=(low+high)/2;
        if(x>(a[mid]))
            low=mid+1;
        else if(x<a[mid])
            high=mid-1;
        else
            return 0;

    }
    return a[low]-x;

}
static class Sieve
{
    private BitSet sieve;

    public Sieve(int size) {
        sieve = new BitSet((size+1)/2);
    }

    public boolean is_composite(int k)
    {
        assert k >= 3 && (k % 2) == 1;
        return sieve.get((k-3)/2);
    }

    public void set_composite(int k)
    {
        assert k >= 3 && (k % 2) == 1;
        sieve.set((k-3)/2);
    }

    public static ArrayList<Integer> sieve_of_eratosthenes(int max)
    {
        Sieve sieve = new Sieve(max + 1); // +1 to include max itself
        for (int i = 3; i*i <= max; i += 2) {
            if (sieve.is_composite(i))
                continue;

            // We increment by 2*i to skip even multiples of i
            for (int multiple_i = i*i; multiple_i <= max; multiple_i += 2*i)
                sieve.set_composite(multiple_i);

        }
        ArrayList<Integer> primes = new ArrayList<Integer>();
        primes.add(2);
        for (int i = 3; i <= max; i += 2)
            if (!sieve.is_composite(i))
                primes.add(i);

        return primes;
    }
}

public static int sumrow(int[][] x,int n)
{
    int sum=0;

    for(int i=0;i<x[0].length;i++)
        sum=sum+x[n][i];
    return sum;
}
public static int sumcolumn(int[][] x,int m,int n)
{
    int sum=0;
    for(int i=0;i<n;i++)
    {
        sum=sum+x[i][m];
    }
    return sum;
}
}


import java.util.*;

public class Main {
    Scanner sc = new Scanner(System.in);

    boolean [] isprime;
    ArrayList<Integer> primetable;

    private void doit(){
        eratos();
        while(sc.hasNext()){
            int h = sc.nextInt();
            int w = sc.nextInt();
            int [][] data = new int[h][w];
            for(int i = 0; i < h; i++){
                for(int j = 0; j < w; j++){
                    data[i][j] = sc.nextInt();
                }
            }
            int [][] inc = new int[h][w];
            for(int i = 0; i < h; i++){
                for(int j = 0; j < w; j++){
                    int ind = binarySearch(primetable, data[i][j]);
                    int num;
                    if(ind == -1){
                        num = 2;
                    }
                    else{
                        num = primetable.get(ind);
                    }
                    if(num < data[i][j]){
                        num = primetable.get(ind + 1);
                    }
                    inc[i][j] = num - data[i][j];
                }
            }

            int count = 0, col = 1 << 24, row = 1 << 24;
            for(int i = 0; i < w; i++){
                int sum = 0;
                for(int j = 0; j < h; j++){
                    sum += inc[j][i];
                }
                if(col == 0 && sum == 0) count++;
                if(col > sum){
                    col = sum;
                }
            }

            for(int i = 0; i < h; i++){
                int sum = 0;
                for(int j = 0; j < w; j++){
                    sum += inc[i][j];
                }
                if(row == 0 && sum == 0) count++;
                if(row > sum){
                    row = sum;
                }
            }
            int res = Math.min(col, row);
            System.out.println(res);
        }
    }

    private void eratos(){
        int MAX = 100000 + 100;
        isprime = new boolean[MAX + 1];
        primetable = new ArrayList<Integer>();
        Arrays.fill(isprime, true);
        isprime[0] = false;
        isprime[1] = false;
        for(int i = 2; i <= MAX; i++){
            if(isprime[i]){
                primetable.add(i);
                for(int j = i * 2; j <= MAX; j+= i){
                    isprime[j] = false;
                }
            }
        }
    }

    private int binarySearch(ArrayList<Integer> a, int patt){
        int mid, left = 0, right = a.size()-1;
        while(left <= right){
            mid = (left + right) / 2;
            if(a.get(mid) == patt){
                return mid;
            }
            if(a.get(mid) < patt){
                left = mid + 1;
            }
            else{
                right = mid - 1;
            }
        }
        return right;
    }

    private void debug(Object... o) {
        System.out.println("debug = " + Arrays.deepToString(o));
    }

    public static void main(String[] args) {
        new Main().doit();
    }
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Scanner;

public class B166 {

    public static int[][] pri;
    public static boolean prims[] = new boolean[100001];

    public void solve() {
        Scanner sc = new Scanner(System.in);
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int i = 0, j = 0, k = 0;
        int ans = 0, sum = 0, count = 0;
        int n = sc.nextInt();
        int m = sc.nextInt();
        pri = new int[n][m];

        for (i = 1; i < 100001; ++i) {
            if (isPrime(i)) {
                prims[i] = true;
            }
        }

        int a[][] = new int[n][m];
        for (i = 0; i < n; ++i) {
            for (j = 0; j < m; ++j) {
                a[i][j] = sc.nextInt();
                if (!prims[a[i][j]]) {
                    k = a[i][j] + 1;

                    while (!isPrime(k)) {
                        k++;
                    }
                    pri[i][j] = k - a[i][j];
                }
            }

        }

        System.out.println(detectRow(n, m));
    }

    public int detectRow(int n, int m) {
        int min = Integer.MAX_VALUE;
        int sum = 0;;
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                sum += pri[i][j];
            }
            if (min > sum) {
                min = sum;
            }
            sum = 0;
        }
        sum = 0;
        for (int j = 0; j < m; ++j) {
            for (int i = 0; i < n; ++i) {
                sum += pri[i][j];
            }
            if (min > sum) {
                min = sum;
            }
            sum = 0;
        }

        return min;
    }

    public static boolean isPrime(int n) {
        if (n == 0 || n == 1) {
            return false;
        }
        int i = 2;
        double a = Math.sqrt(Math.abs(n));
        while (i <= a) {
            if (n % i == 0) {
                return false;
            }
            ++i;
        }
        return true;
    }

    public static void main(String[] args) {
        new B166().solve();
    }
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Scanner;

public class B166 {

    public static int[][] pri;
    public static boolean prims[] = new boolean[100001];

    public void solve() {
        Scanner sc = new Scanner(System.in);
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int i = 0, j = 0, k = 0;
        int ans = 0, sum = 0, count = 0;
        int n = sc.nextInt();
        int m = sc.nextInt();
        pri = new int[n][m];

        for (i = 1; i < 100001; ++i) {
            if (isPrime(i)) {
                prims[i] = true;
            }
        }

        int a[][] = new int[n][m];
        for (i = 0; i < n; ++i) {
            for (j = 0; j < m; ++j) {
                a[i][j] = sc.nextInt();
                if (!prims[a[i][j]]) {
                    k = a[i][j] + 1;

                    while (!prims[k]) {
                        k++;
                    }
                    pri[i][j] = k - a[i][j];
                }
            }

        }

        System.out.println(detectRow(n, m));
    }

    public int detectRow(int n, int m) {
        int min = Integer.MAX_VALUE;
        int sum = 0;;
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                sum += pri[i][j];
            }
            if (min > sum) {
                min = sum;
            }
            sum = 0;
        }
        sum = 0;
        for (int j = 0; j < m; ++j) {
            for (int i = 0; i < n; ++i) {
                sum += pri[i][j];
            }
            if (min > sum) {
                min = sum;
            }
            sum = 0;
        }

        return min;
    }

    public static boolean isPrime(int n) {
        if (n == 0 || n == 1) {
            return false;
        }
        int i = 2;
        double a = Math.sqrt(Math.abs(n));
        while (i <= a) {
            if (n % i == 0) {
                return false;
            }
            ++i;
        }
        return true;
    }

    public static void main(String[] args) {
        new B166().solve();
    }
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Scanner;

public class B166 {

    public static int[][] pri;

    public void solve() {
        Scanner sc = new Scanner(System.in);
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int i = 0, j = 0, k = 0;
        int ans = 0, sum = 0, count = 0;
        int n = sc.nextInt();
        int m = sc.nextInt();
        pri = new int[n][m];

        int a[][] = new int[n][m];
        for (i = 0; i < n; ++i) {
            for (j = 0; j < m; ++j) {
                a[i][j] = sc.nextInt();
                if (!isPrime(a[i][j])) {
                    k = a[i][j] + 1;

                    while (!isPrime(k)) {
                        k++;
                    }
                    pri[i][j] = k - a[i][j];
                }
            }

        }

        System.out.println(detectRow(n, m));
    }

    public int detectRow(int n, int m) {
        int min = Integer.MAX_VALUE;
        int sum = 0;;
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                sum += pri[i][j];
            }
            if (min > sum) {
                min = sum;
            }
            sum = 0;
        }
        sum = 0;
        for (int j = 0; j < m; ++j) {
            for (int i = 0;i <n; ++i) {
                sum += pri[i][j];
            }
            if (min > sum) {
                min = sum;
            }
            sum = 0;
        }

        return min;
    }

    public static boolean isPrime(int n) {
        if (n == 0 || n == 1) {
            return false;
        }
        int i = 2;
        double a = Math.sqrt(Math.abs(n));
        while (i <= a) {
            if (n % i == 0) {
                return false;
            }
            ++i;
        }
        return true;
    }

    public static void main(String[] args) {
        new B166().solve();
    }
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Scanner;

public class B166 {

    public static int[][] pri;
    public static boolean prims[] = new boolean[110000];

    public void solve() {
        Scanner sc = new Scanner(System.in);
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int i = 0, j = 0, k = 0;
        int ans = 0, sum = 0, count = 0;
        int n = sc.nextInt();
        int m = sc.nextInt();
        pri = new int[n][m];

        for (i = 1; i < 110000; ++i) {
            if (isPrime(i)) {
                prims[i] = true;
            }
        }

        int a[][] = new int[n][m];
        for (i = 0; i < n; ++i) {
            for (j = 0; j < m; ++j) {
                a[i][j] = sc.nextInt();
                if (!prims[a[i][j]]) {
                    k = a[i][j] + 1;

                    while (!prims[k]) {
                        k++;
                    }
                    pri[i][j] = k - a[i][j];
                }
            }

        }

        System.out.println(detectRow(n, m));
    }

    public int detectRow(int n, int m) {
        int min = Integer.MAX_VALUE;
        int sum = 0;;
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                sum += pri[i][j];
            }
            if (min > sum) {
                min = sum;
            }
            sum = 0;
        }
        sum = 0;
        for (int j = 0; j < m; ++j) {
            for (int i = 0; i < n; ++i) {
                sum += pri[i][j];
            }
            if (min > sum) {
                min = sum;
            }
            sum = 0;
        }

        return min;
    }

    public static boolean isPrime(int n) {
        if (n == 0 || n == 1) {
            return false;
        }
        int i = 2;
        double a = Math.sqrt(Math.abs(n));
        while (i <= a) {
            if (n % i == 0) {
                return false;
            }
            ++i;
        }
        return true;
    }

    public static void main(String[] args) {
        new B166().solve();
    }
}


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

public class primematrix {
    static int n, m;
    static int[][] mat;
    static boolean[] isPrime;
    static int[] diffPrime, row, column;

    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        //BufferedReader in = new BufferedReader(new FileReader("primematrix.in"));

        StringTokenizer nm = new StringTokenizer(in.readLine());
        n = Integer.parseInt(nm.nextToken());
        m = Integer.parseInt(nm.nextToken());
        mat = new int[n][m];
        row = new int[n];
        column = new int[m];

        // determine max integer needed
        // O(n*m) = O(n^2)
        int max = 0;
        for (int i = 0; i < n; i++) {
            StringTokenizer matin = new StringTokenizer(in.readLine());
            for (int j = 0; j < m; j++) {
                mat[i][j] = Integer.parseInt(matin.nextToken());
                if (mat[i][j] > max)
                    max = mat[i][j];
            }
        }

        // prime sieve
        // O(n log log n)
        isPrime = new boolean[2*max];
        diffPrime = new int[2*max];
        Arrays.fill(isPrime, true);
        isPrime[1] = false;
        for (int i = 2; i*i < 2*max; i++) {
            if (isPrime[i]) {
                for (int j = i; i*j < 2*max; j++) {
                    isPrime[i*j] = false;
                }
            }
        }

        // difference to next prime
        // O(n)*O(n log log n) = O(n^2 log log n)
        for (int i = max; i < 2*max; i++) {
            if (isPrime[i]) {
                diffPrime[max] = i-max;
                break;
            }
        }

        for (int i = max-1; i >= 1; i--) {
            if (isPrime[i])
                diffPrime[i] = 0;
            else
                diffPrime[i] = diffPrime[i+1]+1;
        }

        // Cumulative difference to next prime for each row and column
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                row[i] += diffPrime[mat[i][j]];
                column[j] += diffPrime[mat[i][j]];
            }
        }

        // sort and pick the least
        Arrays.sort(row);
        Arrays.sort(column);
        System.out.println(Math.min(row[0], column[0]));
        System.exit(0);
    }
}


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

public class primematrix {
    static int n, m;
    static int[][] mat;
    static boolean[] isPrime;
    static int[] diffPrime, row, column;

    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        //BufferedReader in = new BufferedReader(new FileReader("primematrix.in"));

        StringTokenizer nm = new StringTokenizer(in.readLine());
        n = Integer.parseInt(nm.nextToken());
        m = Integer.parseInt(nm.nextToken());
        mat = new int[n][m];
        row = new int[n];
        column = new int[m];

        // determine max integer needed
        // O(n*m) = O(n^2)
        //int max = 0;
        for (int i = 0; i < n; i++) {
            StringTokenizer matin = new StringTokenizer(in.readLine());
            for (int j = 0; j < m; j++) {
                mat[i][j] = Integer.parseInt(matin.nextToken());
                //if (mat[i][j] > max)
                    //max = mat[i][j];
            }
        }
        int max = 100000;

        // prime sieve
        // O(n log log n)
        isPrime = new boolean[2*max];
        diffPrime = new int[2*max];
        Arrays.fill(isPrime, true);
        isPrime[1] = false;
        for (int i = 2; i*i < 2*max; i++) {
            if (isPrime[i]) {
                for (int j = i; i*j < 2*max; j++) {
                    isPrime[i*j] = false;
                }
            }
        }

        // difference to next prime
        // O(n)*O(n log log n) = O(n^2 log log n)
        for (int i = max; i < 2*max; i++) {
            if (isPrime[i]) {
                diffPrime[max] = i-max;
                break;
            }
        }

        for (int i = max-1; i >= 1; i--) {
            if (isPrime[i])
                diffPrime[i] = 0;
            else
                diffPrime[i] = diffPrime[i+1]+1;
        }

        // Cumulative difference to next prime for each row and column
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                row[i] += diffPrime[mat[i][j]];
                column[j] += diffPrime[mat[i][j]];
            }
        }

        // sort and pick the least
        Arrays.sort(row);
        Arrays.sort(column);
        System.out.println(Math.min(row[0], column[0]));
        System.exit(0);
    }
}


import java.util.*;
public class b {
public static void main(String[] args)
{
    Scanner input = new Scanner(System.in);
    boolean[] sieve = new boolean[1000000];
    Arrays.fill(sieve, true);
    sieve[0]=sieve[1]=false;
    for(int i = 2; i<1000000; i++)
    {
        if(!sieve[i]) continue;
        for(long j = (long)i*i; j<1000000; j+=i)
        {
            sieve[(int)j] = false;
        }
    }
    int n = input.nextInt(), m = input.nextInt();
    int[][] data = new int[n][m];
    for(int i = 0; i<n; i++)
        for(int j = 0; j<m; j++)
            data[i][j] = input.nextInt();
    int[] count = new int[200000];
    count[100000] = 3;
    for(int i =99999; i>=0; i--)
    {
        if(sieve[i]) count[i] = 0;
        else count[i] = 1+count[i+1];
    }
    int res = 100000;
    for(int i = 0; i<n; i++)
    {
        int sum = 0;
        for(int j = 0; j<m; j++)
            sum += count[data[i][j]];
        res = Math.min(res, sum);
    }
    for(int i = 0; i<m; i++)
    {
        int sum = 0;
        for(int j = 0; j<n; j++)
            sum += count[data[j][i]];
        res = Math.min(res, sum);
    }
    System.out.println(res);

}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.*;

public class B implements Runnable {

    final static int MAX_N = (int) (1e5 + 20);

    static int[] primes;
    static boolean[] is_prime;
    static int n;

    public static void gen_primes() {
        is_prime = new boolean[MAX_N];
        Arrays.fill(is_prime, true);

        is_prime[0] = is_prime[1] = false;

        for (int i = 2; i * i < MAX_N; i++) {
            if (is_prime[i])
                for (int j = i * i; j < MAX_N; j += i) {
                    is_prime[j] = false;
                }
        }

        primes = new int[9594];
        int cnt = 0;

        for (int i = 0; i < MAX_N; i++)
            if (is_prime[i]) {
                primes[cnt++] = i;
            }
    }

    private void solve() throws IOException {
        gen_primes();

        int n = nextInt();
        int m = nextInt();

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

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++)
                a[i][j] = nextInt();
        }

        long min = Long.MAX_VALUE;

        for (int i = 0; i < n; i++) {
            long res = 0;
            for (int j = 0; j < m; j++) {
                int next_prime = next(a[i][j]);
                res += next_prime - a[i][j];
            }
            min = Math.min(min, res);
        }

        for (int j = 0; j < m; j++) {
            long res = 0;
            for (int i = 0; i < n; i++) {
                int next_prime = next(a[i][j]);
                res += next_prime - a[i][j];
            }
            min = Math.min(min, res);
        }

        pl(min);
    }

    private int next(int n) {
        int lo = 0, hi = primes.length;
        int iter = 30;
        while (iter-- > 0) {
            int mid = (lo + hi) / 2;
            if (primes[mid] >= n) hi = mid;
            else lo = mid;
        }
        return primes[hi];
    }

    public static void main(String[] args) {
        new B().run();
    }

    BufferedReader reader;
    StringTokenizer tokenizer;
    PrintWriter writer;

    public void run() {
        try {
            reader = new BufferedReader(new BufferedReader(new InputStreamReader(System.in)));
            writer = new PrintWriter(System.out);
            tokenizer = null;
            solve();
            reader.close();
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

    int nextInt() throws IOException {
        return Integer.parseInt(nextToken());
    }

    long nextLong() throws IOException {
        return Long.parseLong(nextToken());
    }

    double nextDouble() throws IOException {
        return Double.parseDouble(nextToken());
    }

    BigInteger nextBigInteger() throws IOException {
        return new BigInteger(nextToken());
    }

    String nextToken() throws IOException {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            tokenizer = new StringTokenizer(reader.readLine());
        }
        return tokenizer.nextToken();
    }

    void p(Object... objects) {
        for (int i = 0; i < objects.length; i++) {
            if (i != 0)
                writer.print(' ');
            writer.print(objects[i]);
        }
    }

    void pl(Object... objects) {
        p(objects);
        writer.println();
    }

    int cc;

    void pf() {
        writer.printf("Case #%d: ", ++cc);
    }

}


import java.util.Scanner;

public class matrix {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int a[][] = new int[n][m];
        int c[][] = new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                a[i][j] = sc.nextInt();
            }
        }
        boolean f[] = new boolean[1000010];
        for (int i = 2; i <= 100000; i++) {
            if (!f[i]) {
                int x = i;
                while (x <= 100000) {
                    f[x] = true;
                    x += i;
                }
            }
        }
//      -----------------------------------
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                while(!f[a[i][j]]){
                    c[i][j]++;
                    a[i][j]++;
                }
            }
        }
//      =----------------------------
        int ans = 500000000, cnt = 0;
        for (int i = 0; i < n; i++) {
            cnt = 0;
            for (int j = 0; j < m; j++) {
                cnt+=c[i][j];
            }
            if (ans>cnt){
                ans = cnt;
            }
        }
        for (int i = 0; i < m; i++) {
            cnt = 0;
            for (int j = 0; j < n; j++) {
                cnt+=c[j][i];
            }
            if (ans>cnt){
                ans = cnt;
            }
        }
        System.out.println(ans);
    }
}


import java.util.Scanner;

public class matrix {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int a[][] = new int[n][m];
        int c[][] = new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                a[i][j] = sc.nextInt();
            }
        }
//      -----------------------------------
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                while(!tub(a[i][j])){
                    c[i][j]++;
                    a[i][j]++;
                }
            }
        }
//      =----------------------------
        int ans = 500000000, cnt = 0;
        for (int i = 0; i < n; i++) {
            cnt = 0;
            for (int j = 0; j < m; j++) {
                cnt+=c[i][j];
            }
            if (ans>cnt){
                ans = cnt;
            }
        }
        for (int i = 0; i < m; i++) {
            cnt = 0;
            for (int j = 0; j < n; j++) {
                cnt+=c[j][i];
            }
            if (ans>cnt){
                ans = cnt;
            }
        }
        System.out.println(ans);
    }
    private static boolean tub(int n) {
        if (n== 1){
            return false;
        }
        for (int i = 2; i <=(int)(Math.sqrt(n)); i++) {
            if (n%i == 0){
                return false;
            }
        }
        return true;
    }
}