/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.text.*;

public class cf271b {

    static BufferedReader br;
    static Scanner sc;
    static PrintWriter out;

    public static void initA() {
        try {
            br = new BufferedReader(new InputStreamReader(System.in));
            //br = new BufferedReader(new FileReader("input.txt"));
            sc = new Scanner(System.in);
            //out = new PrintWriter("output.txt");
            out = new PrintWriter(System.out);
        } catch (Exception e) {
        }
    }

    public static void initB() {
        try {

            br = new BufferedReader(new FileReader("input.txt"));
            sc = new Scanner(new FileReader("input.txt"));
            out = new PrintWriter("output.txt");

        } catch (Exception e) {
        }
    }

    public static String getString() {
        try {
            return br.readLine();
        } catch (Exception e) {
        }
        return "";
    }

    public static Integer getInt() {
        try {
            return Integer.parseInt(br.readLine());
        } catch (Exception e) {
        }
        return 0;
    }

    public static Integer[] getIntArr() {
        try {
            StringTokenizer temp = new StringTokenizer(br.readLine());
            int n = temp.countTokens();
            Integer temp2[] = new Integer[n];
            for (int i = 0; i < n; i++) {
                temp2[i] = Integer.parseInt(temp.nextToken());
            }
            return temp2;
        } catch (Exception e) {
        }
        return null;
    }

    public static Long[] getLongArr() {
        try {
            StringTokenizer temp = new StringTokenizer(br.readLine());
            int n = temp.countTokens();
            Long temp2[] = new Long[n];
            for (int i = 0; i < n; i++) {
                temp2[i] = Long.parseLong(temp.nextToken());
            }
            return temp2;
        } catch (Exception e) {
        }
        return null;
    }

    public static String[] getStringArr() {
        try {
            StringTokenizer temp = new StringTokenizer(br.readLine());
            int n = temp.countTokens();
            String temp2[] = new String[n];
            for (int i = 0; i < n; i++) {
                temp2[i] = (temp.nextToken());
            }
            return temp2;
        } catch (Exception e) {
        }
        return null;
    }

    public static int getMax(Integer[] ar) {
        int t = ar[0];
        for (int i = 0; i < ar.length; i++) {
            if (ar[i] > t) {
                t = ar[i];
            }
        }
        return t;
    }

    public static void print(Object a) {
        out.println(a);
    }

    public static int nextInt() {
        return sc.nextInt();
    }

    public static double nextDouble() {
        return sc.nextDouble();
    }

    public static void main(String[] ar) {
        initA();
        solve();
        out.flush();
    }

    public static void solve() {

        int max = 110000;
        boolean notprime[] = new boolean[110000];

        for (int i = 2; i < max; i++) {
            for (int j = 2; j * i < max; j++) {
                notprime[i * j] = true;
            }
        }
        ArrayList<Integer> prime = new ArrayList<Integer>(110000);
        for (int i = 2; i < max; i++) {
            if (!notprime[i]) {
                prime.add(i);
            }
        }
        final int last = prime.size();

        int dp[] = new int[110000];
        Arrays.fill(dp, -1);

        Integer x[] = getIntArr();
        int n = x[0], m = x[1];
        int min[][] = new int[n][m];

        for (int i = 0; i < n; i++) {
            Integer temp[] = getIntArr();
            for (int ii = 0; ii < m; ii++) {
                int num = temp[ii];
                int orinum = num;

                if (dp[num] == -1) {
                    int s = 0;
                    while (!isPrime(num)) {
                        num++;
                        s++;
                    }
                    min[i][ii] = s;
                    dp[orinum] = s;
                } else {
                    min[i][ii] = dp[num];
                }
                /*
                int l =0, r= last-1;

                while(l!=r){
                int mid = (l+r)/2;
                int te = prime.get(mid);
                if(te >= num){
                r=mid;
                }else{
                l=mid+1;
                }
                }

                min[i][ii] = prime.get(l)-num;
                 */
            }
        }
        int out = Integer.MAX_VALUE;
        for (int i = 0; i < n; i++) {
            int sum = 0;
            for (int ii = 0; ii < m; ii++) {
                sum += min[i][ii];
            }
            out = Math.min(sum, out);

        }

        for (int i = 0; i < m; i++) {
            int sum = 0;
            for (int ii = 0; ii < n; ii++) {
                sum += min[ii][i];
            }
            out = Math.min(sum, out);

        }
        print(out);

    }

    static boolean isPrime(int n) {
        if (n == 2 || n == 3) {
            return true;
        }
        if (n < 2 || n % 2 == 0) {
            return false;
        }
        for (int i = 3; i <= Math.sqrt(n) + 1; i += 2) {
            if (n % i == 0) {
                return false;
            }
        }
        return true;

    }
}


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.text.*;

public class cf271b {

    static BufferedReader br;
    static Scanner sc;
    static PrintWriter out;

    public static void initA() {
        try {
            br = new BufferedReader(new InputStreamReader(System.in));
            //br = new BufferedReader(new FileReader("input.txt"));
            sc = new Scanner(System.in);
            //out = new PrintWriter("output.txt");
            out = new PrintWriter(System.out);
        } catch (Exception e) {
        }
    }

    public static void initB() {
        try {

            br = new BufferedReader(new FileReader("input.txt"));
            sc = new Scanner(new FileReader("input.txt"));
            out = new PrintWriter("output.txt");

        } catch (Exception e) {
        }
    }

    public static String getString() {
        try {
            return br.readLine();
        } catch (Exception e) {
        }
        return "";
    }

    public static Integer getInt() {
        try {
            return Integer.parseInt(br.readLine());
        } catch (Exception e) {
        }
        return 0;
    }

    public static Integer[] getIntArr() {
        try {
            StringTokenizer temp = new StringTokenizer(br.readLine());
            int n = temp.countTokens();
            Integer temp2[] = new Integer[n];
            for (int i = 0; i < n; i++) {
                temp2[i] = Integer.parseInt(temp.nextToken());
            }
            return temp2;
        } catch (Exception e) {
        }
        return null;
    }

    public static Long[] getLongArr() {
        try {
            StringTokenizer temp = new StringTokenizer(br.readLine());
            int n = temp.countTokens();
            Long temp2[] = new Long[n];
            for (int i = 0; i < n; i++) {
                temp2[i] = Long.parseLong(temp.nextToken());
            }
            return temp2;
        } catch (Exception e) {
        }
        return null;
    }

    public static String[] getStringArr() {
        try {
            StringTokenizer temp = new StringTokenizer(br.readLine());
            int n = temp.countTokens();
            String temp2[] = new String[n];
            for (int i = 0; i < n; i++) {
                temp2[i] = (temp.nextToken());
            }
            return temp2;
        } catch (Exception e) {
        }
        return null;
    }

    public static int getMax(Integer[] ar) {
        int t = ar[0];
        for (int i = 0; i < ar.length; i++) {
            if (ar[i] > t) {
                t = ar[i];
            }
        }
        return t;
    }

    public static void print(Object a) {
        out.println(a);
    }

    public static int nextInt() {
        return sc.nextInt();
    }

    public static double nextDouble() {
        return sc.nextDouble();
    }

    public static void main(String[] ar) {
        initA();
        solve();
        out.flush();
    }

    public static void solve() {

        int max = 110000;
        boolean notprime[] = new boolean[110000];

        for (int i = 2; i < max; i++) {
            for (int j = 2; j * i < max; j++) {
                notprime[i * j] = true;
            }
        }
        ArrayList<Integer> prime = new ArrayList<Integer>(110000);
        for (int i = 2; i < max; i++) {
            if (!notprime[i]) {
                prime.add(i);
            }
        }
        final int last = prime.size();

        int dp[] = new int[110000];
        Arrays.fill(dp, -1);

        Integer x[] = getIntArr();
        int n = x[0], m = x[1];
        int min[][] = new int[n][m];

        for (int i = 0; i < n; i++) {
            Integer temp[] = getIntArr();
            for (int ii = 0; ii < m; ii++) {
                int num = temp[ii];
                int orinum = num;

                if (dp[num] == -1) {
                    int s = 0;

                    while (Collections.binarySearch(prime, num) < 0) {
                        num++;
                        s++;
                    }

                    min[i][ii] = s;
                    dp[orinum] = s;
                } else {
                    min[i][ii] = dp[num];
                }
                /*
                int l =0, r= last-1;

                while(l!=r){
                int mid = (l+r)/2;
                int te = prime.get(mid);
                if(te >= num){
                r=mid;
                }else{
                l=mid+1;
                }
                }

                min[i][ii] = prime.get(l)-num;
                 */
            }
        }
        int out = Integer.MAX_VALUE;
        for (int i = 0; i < n; i++) {
            int sum = 0;
            for (int ii = 0; ii < m; ii++) {
                sum += min[i][ii];
            }
            out = Math.min(sum, out);

        }

        for (int i = 0; i < m; i++) {
            int sum = 0;
            for (int ii = 0; ii < n; ii++) {
                sum += min[ii][i];
            }
            out = Math.min(sum, out);

        }
        print(out);

    }

    static boolean isPrime(int n) {
        if (n == 2 || n == 3) {
            return true;
        }
        if (n < 2 || n % 2 == 0) {
            return false;
        }
        for (int i = 3; i <= Math.sqrt(n) + 1; i += 2) {
            if (n % i == 0) {
                return false;
            }
        }
        return true;

    }
}


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.text.*;

public class cf271b {

    static BufferedReader br;
    static Scanner sc;
    static PrintWriter out;

    public static void initA() {
        try {
            br = new BufferedReader(new InputStreamReader(System.in));
            //br = new BufferedReader(new FileReader("input.txt"));
            sc = new Scanner(System.in);
            //out = new PrintWriter("output.txt");
            out = new PrintWriter(System.out);
        } catch (Exception e) {
        }
    }

    public static void initB() {
        try {

            br = new BufferedReader(new FileReader("input.txt"));
            sc = new Scanner(new FileReader("input.txt"));
            out = new PrintWriter("output.txt");

        } catch (Exception e) {
        }
    }

    public static String getString() {
        try {
            return br.readLine();
        } catch (Exception e) {
        }
        return "";
    }

    public static Integer getInt() {
        try {
            return Integer.parseInt(br.readLine());
        } catch (Exception e) {
        }
        return 0;
    }

    public static Integer[] getIntArr() {
        try {
            StringTokenizer temp = new StringTokenizer(br.readLine());
            int n = temp.countTokens();
            Integer temp2[] = new Integer[n];
            for (int i = 0; i < n; i++) {
                temp2[i] = Integer.parseInt(temp.nextToken());
            }
            return temp2;
        } catch (Exception e) {
        }
        return null;
    }

    public static Long[] getLongArr() {
        try {
            StringTokenizer temp = new StringTokenizer(br.readLine());
            int n = temp.countTokens();
            Long temp2[] = new Long[n];
            for (int i = 0; i < n; i++) {
                temp2[i] = Long.parseLong(temp.nextToken());
            }
            return temp2;
        } catch (Exception e) {
        }
        return null;
    }

    public static String[] getStringArr() {
        try {
            StringTokenizer temp = new StringTokenizer(br.readLine());
            int n = temp.countTokens();
            String temp2[] = new String[n];
            for (int i = 0; i < n; i++) {
                temp2[i] = (temp.nextToken());
            }
            return temp2;
        } catch (Exception e) {
        }
        return null;
    }

    public static int getMax(Integer[] ar) {
        int t = ar[0];
        for (int i = 0; i < ar.length; i++) {
            if (ar[i] > t) {
                t = ar[i];
            }
        }
        return t;
    }

    public static void print(Object a) {
        out.println(a);
    }

    public static int nextInt() {
        return sc.nextInt();
    }

    public static double nextDouble() {
        return sc.nextDouble();
    }

    public static void main(String[] ar) {
        initA();
        solve();
        out.flush();
    }

    public static void solve() {

        int max = 100000;
        boolean notprime[] = new boolean[100000];

        for (int i = 2; i < max; i++) {
            for (int j = 2; j * i < max; j++) {
                notprime[i * j] = true;
            }
        }
        ArrayList<Integer> prime = new ArrayList<Integer>(100000);
        for (int i = 2; i < 100000; i++) {
            if (!notprime[i]) {
                prime.add(i);
            }
        }
        final int last = prime.size();

        Integer x[] = getIntArr();
        int n = x[0], m = x[1];
        int min[][] = new int[n][m];

        for (int i = 0; i < n; i++) {
            Integer temp[] = getIntArr();
            for (int ii = 0; ii < m; ii++) {
                int num = temp[ii];
                int l =0, r= last-1;

                while(l!=r){
                    int mid = (l+r)/2;
                    int te = prime.get(mid);
                    if(te >= num){
                        r=mid;
                    }else{
                        l=mid+1;
                    }
                }
                min[i][ii] = prime.get(l)-num;

            }
        }

        int out = Integer.MAX_VALUE;
        for (int i = 0; i < n; i++) {
            int sum = 0;
            for (int ii = 0; ii < m; ii++) {
                sum += min[i][ii];
            }
            out = Math.min(sum, out);

        }

        for (int i = 0; i < m; i++) {
            int sum = 0;
            for (int ii = 0; ii < n; ii++) {
                sum += min[ii][i];
            }
            out = Math.min(sum, out);

        }
        print(out);

    }

    static boolean isPrime(int n) {
        if (n == 2 || n == 3) {
            return true;
        }
        if (n < 2 || n % 2 == 0) {
            return false;
        }
        for (int i = 3; i <= Math.sqrt(n) + 1; i += 2) {
            if (n % i == 0) {
                return false;
            }
        }
        return true;

    }
}


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.StringTokenizer;

public class T271B {
	static int MAX = 150001;
	public static void main(String[] args) {
		FastScanner in = new FastScanner();
		int n = in.nextInt();
		int m = in.nextInt();
		int[][] a = new int[n][m];
		for (int i = 0; i < n; i ++)
			for (int j = 0; j < m; j ++)
				a[i][j] = in.nextInt();

		boolean[] primes = new boolean[MAX];
		for (int i = 2;i < MAX; i ++) primes[i] = true;

		for (int i = 2; i <= Math.sqrt(MAX); i ++)
			for (int j = i; i * j < MAX; j ++)
				if (primes[i])
					primes[i * j] = false;

		HashMap<Integer, Integer> isNotPrime = new HashMap<Integer, Integer>();
		int last = 0;
		for (int i = 1; i < MAX; i ++) {
			if (primes[i]) {
				for (int j = last + 1; j < i; j ++)
					isNotPrime.put(j, i);
				last = i;
			}
		}

		int min = Integer.MAX_VALUE;
		for (int i = 0; i < n; i ++) {
			int sum = 0;
			for (int j = 0; j < m; j ++) {
				if (isNotPrime.containsKey(a[i][j])) {
					sum += isNotPrime.get(a[i][j]) - a[i][j];
				}
			}
			min = Math.min(sum, min);
		}

		for (int i = 0; i < m; i ++) {
			int sum = 0;
			for (int j = 0; j < n; j ++) {
				if (isNotPrime.containsKey(a[j][i])) {
					sum += isNotPrime.get(a[j][i]) - a[j][i];
				}
			}
			min = Math.min(sum, min);
		}
		System.out.println(min);
	}

	static class FastScanner {

		BufferedReader br;
		StringTokenizer st;

		public FastScanner(String s) {
			try {
				br = new BufferedReader(new FileReader(s));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}

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

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

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

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

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

	}

}


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.StringTokenizer;

public class T271B {
	static int MAX = 15001;
	public static void main(String[] args) {
		FastScanner in = new FastScanner();
		int n = in.nextInt();
		int m = in.nextInt();
		int[][] a = new int[n][m];
		for (int i = 0; i < n; i ++)
			for (int j = 0; j < m; j ++)
				a[i][j] = in.nextInt();

		boolean[] primes = new boolean[MAX];
		for (int i = 2;i < MAX; i ++) primes[i] = true;

		for (int i = 2; i <= Math.sqrt(MAX); i ++)
			for (int j = i; i * j < MAX; j ++)
				if (primes[i])
					primes[i * j] = false;

		HashMap<Integer, Integer> isNotPrime = new HashMap<Integer, Integer>();
		int last = 0;
		for (int i = 1; i < MAX; i ++) {
			if (primes[i]) {
				for (int j = last + 1; j < i; j ++)
					isNotPrime.put(j, i);
				last = i;
			}
		}

		int min = Integer.MAX_VALUE;
		for (int i = 0; i < n; i ++) {
			int sum = 0;
			for (int j = 0; j < m; j ++) {
				if (isNotPrime.containsKey(a[i][j])) {
					sum += isNotPrime.get(a[i][j]) - a[i][j];
				}
			}
			min = Math.min(sum, min);
		}

		for (int i = 0; i < m; i ++) {
			int sum = 0;
			for (int j = 0; j < n; j ++) {
				if (isNotPrime.containsKey(a[j][i])) {
					sum += isNotPrime.get(a[j][i]) - a[j][i];
				}
			}
			min = Math.min(sum, min);
		}
		System.out.println(min);
	}

	static class FastScanner {

		BufferedReader br;
		StringTokenizer st;

		public FastScanner(String s) {
			try {
				br = new BufferedReader(new FileReader(s));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}

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

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

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

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

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

	}

}


import java.io.*;
import java.util.*;
public class B271 {
	static FastScanner in =new  FastScanner(System.in);
	static boolean isComposite[] = new boolean[100005];
	static int set[] = new int[9593];
	public static void main (String args[]){
		GharbalErnesten(100004);
		int n = in.nextInt();
		int m = in.nextInt();
		int a[][] = new int[n][m];
		int b[][] = new int[n][m];

		int min = 1000000;

		for(int i = 0 ; i < n ; i++){
			int d=0;
			for(int j = 0 ;j < m ; j++){
				int t = in.nextInt();
				a[i][j]= t ;
				int c = 0;
				int low =0;
				int hi=9593;
				int h=0;
				if(t>1 && !isComposite[t]){
					h=t;

				}
				else{
				 while(low<=hi){
					int mid= (low+hi)/2 ;

					if(set[mid]<t){
						low = mid+1;
					}
					if(set[mid]>t){
						h=set[mid];
						hi=mid-1;
					}

				}}
				c= h-t;
				b[i][j]=c;
				d+=c;
			}
			if(d<min){
				min=d;
			}
		}

		for(int j = 0 ; j < m ;j++){
			int c = 0;
			for(int i = 0 ; i < n ; i++){
				c+=b[i][j];
			}
			if(c<min)
				min = c;
		}
		System.out.println(min);
	}
	public static void GharbalErnesten(int upper) {
        int upSqr = (int) Math.sqrt(upper);			 // Primes Are Is False
//        boolean[] isComposite = new boolean[upper + 1];
        for (int m = 2; m <= upSqr; m++) {
            if (!isComposite[m]) {
                for (int k = m * m; k <= upper; k += m) {
                    isComposite[k] = true;
                }
                }
            }
        int j=0;
        int max = 0 ;
        int p =0;
        boolean f = true;
        for(int i = 2 ; i < upper;i++){
        	if(!isComposite[i]){
        		set[j]=i;
        		j++;

        	}
        }

        }
}

class FastScanner {
	BufferedReader br;
	StringTokenizer st;
	public FastScanner(File f) {
		try {
			br = new BufferedReader(new FileReader(f));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
	public FastScanner(InputStream f) {
		br = new BufferedReader(new InputStreamReader(f));
	}
	String next() {
		while (st == null || !st.hasMoreTokens()) {
			String s = null;
			try {
				s = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			if (s == null)
				return null;
			st = new StringTokenizer(s);
		}
		return st.nextToken();
	}
	boolean hasMoreTokens() {
		while (st == null || !st.hasMoreTokens()) {
			String s = null;
			try {
				s = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			if (s == null)
				return false;
			st = new StringTokenizer(s);
		}
		return true;
	}
	int nextInt() {
		return Integer.parseInt(next());
	}
	long nextLong() {
		return Long.parseLong(next());
	}
}


import java.io.*;
import java.util.*;
public class B271 {
	static FastScanner in =new  FastScanner(System.in);
	static boolean isComposite[] = new boolean[100005];
	static int set[] = new int[9593];
	public static void main (String args[]){
		GharbalErnesten(100005);
		int n = in.nextInt();
		int m = in.nextInt();
		int a[][] = new int[n][m];
		int b[][] = new int[n][m];

		int min = 1000000;

		for(int i = 0 ; i < n ; i++){
			int d=0;
			for(int j = 0 ;j < m ; j++){
				int t = in.nextInt();
				a[i][j]= t ;
				int c = 0;
				int low =0;
				int hi=9593;
				int h=0;
				if(t>1 && !isComposite[t]){
					h=t;

				}
				else{
				 while(low<=hi){
					int mid= (low+hi)/2 ;

					if(set[mid]<t){
						low = mid+1;
					}
					if(set[mid]>t){
						h=set[mid];
						hi=mid-1;
					}

				}}
				c= h-t;
				b[i][j]=c;
				d+=c;
			}
			if(d<min){
				min=d;
			}
		}

		for(int j = 0 ; j < m ;j++){
			int c = 0;
			for(int i = 0 ; i < n ; i++){
				c+=b[i][j];
			}
			if(c<min)
				min = c;
		}
		System.out.println(min);
	}
	public static void GharbalErnesten(int upper) {
        int upSqr = (int) Math.sqrt(upper);			 // Primes Are Is False
//        boolean[] isComposite = new boolean[upper + 1];
        for (int m = 2; m <= upSqr; m++) {
            if (!isComposite[m]) {
                for (int k = m * m; k <= upper; k += m) {
                    isComposite[k] = true;
                }
                }
            }
        int j=0;
        int max = 0 ;
        int p =0;
        boolean f = true;
        for(int i = 2 ; i < upper;i++){
        	if(!isComposite[i]){
        		set[j]=i;
        		j++;
        		max = Math.max(max, i-p);
        		p=i;

        	}
        }

        }
}

class FastScanner {
	BufferedReader br;
	StringTokenizer st;
	public FastScanner(File f) {
		try {
			br = new BufferedReader(new FileReader(f));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
	public FastScanner(InputStream f) {
		br = new BufferedReader(new InputStreamReader(f));
	}
	String next() {
		while (st == null || !st.hasMoreTokens()) {
			String s = null;
			try {
				s = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			if (s == null)
				return null;
			st = new StringTokenizer(s);
		}
		return st.nextToken();
	}
	boolean hasMoreTokens() {
		while (st == null || !st.hasMoreTokens()) {
			String s = null;
			try {
				s = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			if (s == null)
				return false;
			st = new StringTokenizer(s);
		}
		return true;
	}
	int nextInt() {
		return Integer.parseInt(next());
	}
	long nextLong() {
		return Long.parseLong(next());
	}
}


	import java.io.*;
	import java.util.*;
	public class C271 {
		static FastScanner in =new  FastScanner(System.in);
		static boolean isComposite[] = new boolean[100001];
		static HashSet<Integer> set = new HashSet<Integer>();
		public static void main (String args[]){
			GharbalErnesten(100000);
			int n = in.nextInt();
			int m = in.nextInt();
			int a[][] = new int[n][m];
			int b[][] = new int[n][m];

			int min = 1000000;

			for(int i = 0 ; i < n ; i++){
				int d=0;
				for(int j = 0 ;j < m ; j++){
					int t = in.nextInt();
					a[i][j]= t ;
					int c = 0;
					while(!set.contains(t)){
						t++;
						c++;
					}
					b[i][j]=c;
					d+=c;
				}
				if(d<min){
					min=d;
				}
			}

			for(int j = 0 ; j < m ;j++){
				int c = 0;
				for(int i = 0 ; i < n ; i++){
					c+=b[i][j];
				}
				if(c<min)
					min = c;
			}
			System.out.println(min);
		}
		public static void GharbalErnesten(int upper) {
	        int upSqr = (int) Math.sqrt(upper);			 // Primes Are Is False
//	        boolean[] isComposite = new boolean[upper + 1];
	        for (int m = 2; m <= upSqr; m++) {
	            if (!isComposite[m]) {
	                for (int k = m * m; k <= upper; k += m) {
	                    isComposite[k] = true;
	                }
	                }
	            }
	        for(int i = 2 ; i < upper;i++){
	        	if(!isComposite[i])
	        		set.add(i);
	        }
	        }
	}

	class FastScanner {
		BufferedReader br;
		StringTokenizer st;
		public FastScanner(File f) {
			try {
				br = new BufferedReader(new FileReader(f));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}
		public FastScanner(InputStream f) {
			br = new BufferedReader(new InputStreamReader(f));
		}
		String next() {
			while (st == null || !st.hasMoreTokens()) {
				String s = null;
				try {
					s = br.readLine();
				} catch (IOException e) {
					e.printStackTrace();
				}
				if (s == null)
					return null;
				st = new StringTokenizer(s);
			}
			return st.nextToken();
		}
		boolean hasMoreTokens() {
			while (st == null || !st.hasMoreTokens()) {
				String s = null;
				try {
					s = br.readLine();
				} catch (IOException e) {
					e.printStackTrace();
				}
				if (s == null)
					return false;
				st = new StringTokenizer(s);
			}
			return true;
		}
		int nextInt() {
			return Integer.parseInt(next());
		}
		long nextLong() {
			return Long.parseLong(next());
		}
	}


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

public class Codeforces implements Runnable {
	BufferedReader br;
	StringTokenizer in;
	PrintWriter out;
	int a[][];

	public void solve() throws IOException {
		int n = nextInt();
		int m = nextInt();
		a = new int[n + 1][m + 1];
		int b[] = new int[100001];
		boolean prime[] = new boolean[100001];
		for (int i = 0; i < 100001; i++) {
			prime[i] = true;
		}
		prime[0] = prime[1] = false;
		for (int i = 2; i <= 100000; i++) {
			if (prime[i]) {
				if (i < 317) {
					for (int j = i * i; j <= 100000; j += i) {
						prime[j] = false;
					}
				}
			}
		}
		int k = 0;
		for (int i = 1; i <= 99991; i++) {
			if (prime[i]) {
				b[i] = 0;
			} else {
				int j = i;
				while (prime[j] != true) {
					j++;
				}
				b[i] = j - i;
			}
		}
		for (int i = 99992; i <= 100000; i++) {
			b[i] = 100003 - i;
		}
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= m; j++) {
				a[i][j] = nextInt();
				a[0][j] = a[0][j] + b[a[i][j]];
				a[i][0] = a[i][0] + b[a[i][j]];
			}
		}
		int min = Integer.MAX_VALUE;
		for (int i = 1; i <= n; i++) {
			if (a[i][0] < min) {
				min = a[i][0];
			}
		}
		for (int i = 1; i <= m; i++) {
			if (a[0][i] < min) {
				min = a[0][i];
			}
		}
		out.println(min);
	}

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

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

	public void run() {
		try {
			br = new BufferedReader(new InputStreamReader(System.in));
			out = new PrintWriter(System.out);
			solve();
			out.close();
		} catch (IOException e) {
		}
	}

	public static void main(String[] args) {
		new Thread(new Codeforces()).start();
	}
}


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

public class Codeforces implements Runnable {
	BufferedReader br;
	StringTokenizer in;
	PrintWriter out;
	int a[][];

	boolean prost(int x) {
		int c = 1;
		for (int i = 1; i <= Math.sqrt(x); i++) {
			if (x % i == 0) {
				c++;
			}
		}
		if (c > 2 | x == 1) {
			return false;
		}
		return true;
	}

	int funct(int i) {
		int k = i;
		while (true) {
			if (prost(k)) {
				return (k - i);
			}
			k++;
		}
	}

	public void solve() throws IOException {
		int n = nextInt();
		int m = nextInt();
		a = new int[n + 1][m + 1];

		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= m; j++) {
				a[i][j] = nextInt();
				int v = funct(a[i][j]);
				a[0][j] = a[0][j] + v;
				a[i][0] = a[i][0] + v;
			}
		}
		int min = Integer.MAX_VALUE;
		for (int i = 1; i <= n; i++) {
			if (a[i][0] < min) {
				min = a[i][0];
			}
		}
		for (int i = 1; i <= m; i++) {
			if (a[0][i] < min) {
				min = a[0][i];
			}
		}
		out.println(min);
	}

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

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

	public void run() {
		try {
			br = new BufferedReader(new InputStreamReader(System.in));
			out = new PrintWriter(System.out);
			solve();
			out.close();
		} catch (IOException e) {
		}
	}

	public static void main(String[] args) {
		new Thread(new Codeforces()).start();
	}
}


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

public class Main {

    private int N, M, A[][], B[] = new int[1000001];

    public Integer nextInt(String s) {
        return Integer.valueOf(s);
    }

    public boolean isPrime(int N) {
        return B[N] == 0;
    }

    public void sieve() {
        int K = 1000;
        B[1] = 1;
        for (int i = 1; i <= K; i++) {
            if (B[i] == 0) {
                for (int j = 2 * i; j <= 1000000; j += i) {
                    B[j] = 1;
                }
            }
        }
    }

    public void run() {
        sieve();
        try {
            BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
            StringTokenizer st = new StringTokenizer(bf.readLine());
            N = nextInt(st.nextToken());
            M = nextInt(st.nextToken());
            A = new int[N + 1][M + 1];

            int min = Integer.MAX_VALUE;

            for (int i = 1; i <= N; i++) {
                st = new StringTokenizer(bf.readLine());
                for (int j = 1; j <= M; j++) {
                    A[i][j] = nextInt(st.nextToken());
                }
            }

            for (int i = 1; i <= N; i++) {
                int counter = 0;
                for (int j = 1; j <= M; j++) {
                    int f = A[i][j];
                    while (!isPrime(f)) {
                        counter++;
                        f++;
                    }
                }

                if (min > counter) {
                    min = counter;
                }
            }
            for (int i = 1; i <= M; i++) {
                int counter = 0;
                for (int j = 1; j <= N; j++) {
                    int f = A[j][i];
                    while (!isPrime(f)) {
                        counter++;
                        f++;
                    }
                }
                if (min > counter) {
                    min = counter;
                }
            }
            System.out.println(min);
        } catch (Exception e) {
        }
    }

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


import java.util.*;

public class PrimeMatrix {
    static int[] primes = new int[9593];
    static boolean[] isPrime = new boolean[100004];

    static {
        for (int i = 2; i < isPrime.length; i++)
            isPrime[i] = true;

        for (int i = 2; i*i < isPrime.length; i++) {
            if (isPrime[i]) {
                for (int j = 2*i; j < isPrime.length; j += i)
                    isPrime[j] = false;
            }
        }

        int p = 0;
        for (int i = 0; i < isPrime.length; i++)
            if (isPrime[i])
                primes[p++] = i;
    }

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

        int n = scan.nextInt();
        int m = scan.nextInt();
        int[][] mat = new int[n][m];

        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
                mat[i][j] = scan.nextInt();

        int min = Integer.MAX_VALUE;
        for (int i = 0; i < n; i++) {
            int diff = 0;
            for (int j = 0; j < m; j++) {
                if (!isPrime[mat[i][j]]) {
                    int ins = -(Arrays.binarySearch(primes, mat[i][j]) + 1);
                    diff += primes[ins] - mat[i][j];
                }
            }
            min = Math.min(diff, min);
        }

        for (int j = 0; j < m; j++) {
            int diff = 0;
            for (int i = 0; i < n; i++) {
                if (!isPrime[mat[i][j]]) {
                    int ins = -(Arrays.binarySearch(primes, mat[i][j]) + 1);
                    diff += primes[ins] - mat[i][j];
                }
            }
            min = Math.min(diff, min);
        }

        System.out.println(min);
    }
}


import java.util.*;
public class cf271b {
  public static void main(String[] args) {
    int MAX = 100100;
    TreeSet<Integer> primes = new TreeSet<Integer>();
    boolean[] isPrime = new boolean[MAX];
    Arrays.fill(isPrime, true);
    isPrime[0] = isPrime[1] = false;
    for(int i=0; i<MAX; i++)
      if(isPrime[i]) {
        primes.add(i);
        for(int j=i+i; j<MAX; j+=i)
          isPrime[j] = false;
      }

    Scanner in = new Scanner(System.in);
    int n = in.nextInt();
    int m = in.nextInt();
    int[] row = new int[n];
    int[] col = new int[m];
    for(int i=0; i<n; i++)
      for(int j=0; j<m; j++) {
        int x = in.nextInt();
        if(!isPrime[x]) {
          int best = Math.abs(x-primes.higher(x));
          row[i] += best;
          col[j] += best;
        }
      }
    int ans = Integer.MAX_VALUE;
    for(int x : row) ans = Math.min(ans,x);
    for(int x : col) ans = Math.min(ans,x);
    System.out.println(ans);
  }
}


import java.util.*;

import java.math.*;
import java.io.*;

import static java.lang.Math.*;
import static java.util.Arrays.*;
import static java.util.Collections.*;

public class  test{

	// ArrayList<Integer> lis = new ArrayList<Integer>();
	// ArrayList<String> lis = new ArrayList<String>();
	// PriorityQueue<Integer> que = new PriorityQueue<Integer>();
	//  Stack<Integer> que = new Stack<Integer>();
    //	static long sum=0;
	// 1000000007 (10^9+7)
	static int mod = 1000000007;
	//static int mod = 1000000009,r=0;
   // static int dx[]={1,-1,0,0};
//	static int dy[]={0,0,1,-1};
//	static int dx[]={1,-1,0,0,1,1,-1,-1};
//  static int dy[]={0,0,1,-1,1,-1,1,-1};
	//static long H,L;
	//static Set<Integer> set = new HashSet<Integer>();
public  static void main(String[] args)   throws Exception, IOException{
   //String line=""; throws Exception, IOException
   //(line=br.readLine())!=null
	//Scanner sc =new Scanner(System.in);
	// !!caution!! int long //
	Reader sc = new Reader(System.in);
  // while( ){
	  // int  n=sc.nextInt(),m=sc.nextInt();//a[]=new int[n],b[]=new int[n];
	int  n=sc.nextInt(),m=sc.nextInt(),p[]=new int[100021];
	int  d[][]=new int[n][m];
	int  M=100020;
	boolean b[]=new boolean[100021];
			for(int i=2; i*i<=M ;i++){
			  if(b[i])continue;
				for(int t=i*i; t<=M ;t+=i){
					b[t]=true;
				}

			}

			for(int i=2,c=0; i<=M ;i++){
				  if(b[i])continue;
				  for(int t=i-1; c<=t;t--){
					p[t]=i;
				  }
				  c=i;
			}
			//db(p);
	    b[1]=b[0]=true;
		long min=Long.MAX_VALUE;

		for(int i=0; i<n;i++){
		  long c=0;
			for(int t=0; t<m;t++){
				d[i][t]=sc.nextInt();
				if( b[d[i][t]] )c+=p[d[i][t]]-d[i][t];
			}
			//db(c);
			min=min(c,min);

		}
		for(int i=0; i<m;i++){
			  long c=0;
				for(int t=0; t<n;t++){
					if( b[d[t][i]] )c+=p[d[t][i]]-d[t][i];
				}
				//db(c);
				min=min(c,min);
			}

		System.out.println(min);

}

static void db(Object... os){
    System.err.println(Arrays.deepToString(os));

}
}

class Reader
{
	private BufferedReader x;
	private StringTokenizer st;

	public Reader(InputStream in)
	{
		x = new BufferedReader(new InputStreamReader(in));
		st = null;
	}
	public String nextString() throws IOException
	{
		while( st==null || !st.hasMoreTokens() )
			st = new StringTokenizer(x.readLine());
		return st.nextToken();
	}
	public int nextInt() throws IOException
	{
		return Integer.parseInt(nextString());
	}
	public long nextLong() throws IOException
	{
		return Long.parseLong(nextString());
	}
	public double nextDouble() throws IOException
	{
		return Double.parseDouble(nextString());
	}
}

/*

class P implements Comparable<P>{
//	implements Comparable<P>
	int v,w;
	P(int v,int w){
		this.v=v;
		this.w=w;

	}
	public int compareTo(P x){
	   	 return w-x.w;//ascend
	   }
}
*/


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

public class TaskB {
    private final static Scanner in=new Scanner(System.in);
    private final static PrintWriter out=new PrintWriter(System.out,true);
    private final static PrintWriter debug=new PrintWriter(System.err,true);
    public static void main(String[] args) {
            new TaskB().solve();
    }
    private final int MAX_NUMBER=10000;
    private final SortedSet<Integer> primeNumbers=new TreeSet<Integer>();
    {
        primeNumbers.add(2);
        main: for(int i=3;;i++){
            for(int prime:primeNumbers){
                if(i%prime==0){
                    continue main;
                }
            }
            primeNumbers.add(i);
            if(i>=MAX_NUMBER){
                break;
            }
        }
        debug.println(primeNumbers);
    }

    /**
     *
     */
    private void solve() {
        int n = in.nextInt();
        int m = in.nextInt();
        int[][] matrix=new int[n][m];
        for(int x=0;x<n;x++){
            for(int y=0;y<m;y++){
                matrix[x][y]=in.nextInt();
            }
        }
        int[][] stepMatrix=new int[n][m];
        for(int x=0;x<n;x++){
            for(int y=0;y<m;y++){
                int steps=findSteps(matrix[x][y]);
                debug.printf("matrix[%s][%s]=%s, steps=%s\r\n",x,y,matrix[x][y],steps);
                stepMatrix[x][y]=steps;
            }
        }
        long minSteps=Long.MAX_VALUE;
        for(int x=0;x<n;x++){
            long steps=0;
            for(int y=0;y<m;y++){
                steps+=stepMatrix[x][y];
            }
            if(steps<minSteps){
                minSteps=steps;
            }
        }
        for(int y=0;y<m;y++){
            long steps=0;
            for(int x=0;x<n;x++){
                steps+=stepMatrix[x][y];
            }
            if(steps<minSteps){
                minSteps=steps;
            }
        }
        out.println(minSteps);
    }

    private int findSteps(int i) {
        if(i==1){
            return 1;
        }
        if(primeNumbers.contains(i)){
            return 0;
        }
        return primeNumbers.tailSet(i).first()-i;
    }

}


package codeforces.round166d2;
/*********************************
 * PROPRIETARY/CONFIDENTIAL.  Use of this product is subject to license terms.
 * Copyright (c) 2013 NVision Group, Inc. All rights reserved.
 *
 * Task1C.java 15.01.2013 21:02:03
 *********************************/

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

public class TaskB {
    private final static Scanner in=new Scanner(System.in);
    private final static PrintWriter out=new PrintWriter(System.out,true);
    private final static PrintWriter debug=new PrintWriter(System.err,true);
    public static void main(String[] args) {
            new TaskB().solve();
    }
    private final int MAX_NUMBER=100000;
    private final TreeSet<Integer> primeNumbers=new TreeSet<Integer>();
    {
        primeNumbers.add(2);
        main: for(int i=3;;i++){
            for(int prime:primeNumbers){
                if(i%prime==0){
                    continue main;
                }
            }
            primeNumbers.add(i);
            if(i>=MAX_NUMBER){
                break;
            }
        }
        //debug.println(primeNumbers);
    }

    /**
     *
     */
    private void solve() {
        int n = in.nextInt();
        int m = in.nextInt();
        int[][] matrix=new int[n][m];
        for(int x=0;x<n;x++){
            for(int y=0;y<m;y++){
                matrix[x][y]=in.nextInt();
            }
        }
        int[][] stepMatrix=new int[n][m];
        for(int x=0;x<n;x++){
            for(int y=0;y<m;y++){
                int steps=findSteps(matrix[x][y]);
                //debug.printf("matrix[%s][%s]=%s, steps=%s\r\n",x,y,matrix[x][y],steps);
                stepMatrix[x][y]=steps;
            }
        }
        long minSteps=Long.MAX_VALUE;
        main: for(int x=0;x<n;x++){
            long steps=0;
            for(int y=0;y<m;y++){
                steps+=stepMatrix[x][y];
                if(steps>=minSteps){
                    continue main;
                }
            }
            if(steps<minSteps){
                minSteps=steps;
            }
        }
        main2: for(int y=0;y<m;y++){
            long steps=0;
            for(int x=0;x<n;x++){
                steps+=stepMatrix[x][y];
                if(steps>=minSteps){
                    continue main2;
                }

            }
            if(steps<minSteps){
                minSteps=steps;
            }
        }
        out.println(minSteps);
    }

    private int findSteps(int i) {
        if(i==1){
            return 1;
        }
        if(primeNumbers.contains(i)){
            return 0;
        }
        return primeNumbers.ceiling(i)-i;
    }

}


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

public class TaskB {
    private final static Scanner in=new Scanner(System.in);
    private final static PrintWriter out=new PrintWriter(System.out,true);
    private final static PrintWriter debug=new PrintWriter(System.err,true);
    public static void main(String[] args) {
            new TaskB().solve();
    }
    private final int MAX_NUMBER=100000;
    private final TreeSet<Integer> primeNumbers=new TreeSet<Integer>();
    {
        long s = System.currentTimeMillis();
        int[] primeNumbersTmp = new int[9593];
        int primeIndex = 0;
        primeNumbersTmp[primeIndex++]=2;
        primeNumbers.add(2);
        main: for(int i=3;;i++){
            for(int prime:primeNumbersTmp){
                if(prime==0){
                    break;
                }

                if(i%prime==0){
                    continue main;
                }
            }
            primeNumbersTmp[primeIndex++]=i;
            primeNumbers.add(i);
            if(i>=MAX_NUMBER){
                break;
            }
        }
        //debug.println(primeNumbers.size()+":"+(System.currentTimeMillis()-s));
        //debug.println(primeNumbers);
    }

    /**
     *
     */
    private void solve() {
        int n = in.nextInt();
        int m = in.nextInt();

        int[][] stepMatrix=new int[n][m];
        long minSteps=Long.MAX_VALUE;
        for(int x=0;x<n;x++){
            long steps2=0;
            for(int y=0;y<m;y++){
                int steps=findSteps(in.nextInt());
                //debug.printf("matrix[%s][%s]=%s, steps=%s\r\n",x,y,matrix[x][y],steps);
                stepMatrix[x][y]=steps;
                steps2+=stepMatrix[x][y];
            }
            if(steps2<minSteps){
                minSteps=steps2;
            }

        }

        main2: for(int y=0;y<m;y++){
            long steps=0;
            for(int x=0;x<n;x++){
                steps+=stepMatrix[x][y];
                if(steps>=minSteps){
                    continue main2;
                }

            }
            if(steps<minSteps){
                minSteps=steps;
            }
        }
        out.println(minSteps);
    }

    private int findSteps(int i) {
        if(i==1){
            return 1;
        }
        if(primeNumbers.contains(i)){
            return 0;
        }
        return primeNumbers.ceiling(i)-i;
    }

}


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

public class TaskB {
    private final static Scanner in=new Scanner(System.in);
    private final static PrintWriter out=new PrintWriter(System.out,true);
    private final static PrintWriter debug=new PrintWriter(System.err,true);
    public static void main(String[] args) {
            new TaskB().solve();
    }
    private final int MAX_NUMBER=100000;
    private final TreeSet<Integer> primeNumbers=new TreeSet<Integer>();
    {
        primeNumbers.add(2);
        main: for(int i=3;;i++){
            for(int prime:primeNumbers){
                if(i%prime==0){
                    continue main;
                }
            }
            primeNumbers.add(i);
            if(i>=MAX_NUMBER){
                break;
            }
        }
        //debug.println(primeNumbers);
    }

    /**
     *
     */
    private void solve() {
        int n = in.nextInt();
        int m = in.nextInt();
        int[][] matrix=new int[n][m];
        for(int x=0;x<n;x++){
            for(int y=0;y<m;y++){
                matrix[x][y]=in.nextInt();
            }
        }
        int[][] stepMatrix=new int[n][m];
        for(int x=0;x<n;x++){
            for(int y=0;y<m;y++){
                int steps=findSteps(matrix[x][y]);
                //debug.printf("matrix[%s][%s]=%s, steps=%s\r\n",x,y,matrix[x][y],steps);
                stepMatrix[x][y]=steps;
            }
        }
        long minSteps=Long.MAX_VALUE;
        main: for(int x=0;x<n;x++){
            long steps=0;
            for(int y=0;y<m;y++){
                steps+=stepMatrix[x][y];
                if(steps>=minSteps){
                    continue main;
                }
            }
            if(steps<minSteps){
                minSteps=steps;
            }
        }
        main2: for(int y=0;y<m;y++){
            long steps=0;
            for(int x=0;x<n;x++){
                steps+=stepMatrix[x][y];
                if(steps>=minSteps){
                    continue main2;
                }

            }
            if(steps<minSteps){
                minSteps=steps;
            }
        }
        out.println(minSteps);
    }

    private int findSteps(int i) {
        if(i==1){
            return 1;
        }
        if(primeNumbers.contains(i)){
            return 0;
        }
        return primeNumbers.ceiling(i);
    }

}


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

public class TaskB {
    private final static Scanner in=new Scanner(System.in);
    private final static PrintWriter out=new PrintWriter(System.out,true);
    private final static PrintWriter debug=new PrintWriter(System.err,true);
    public static void main(String[] args) {
            new TaskB().solve();
    }
    private final int MAX_NUMBER=100000;
    private final SortedSet<Integer> primeNumbers=new TreeSet<Integer>();
    {
        primeNumbers.add(2);
        main: for(int i=3;;i++){
            for(int prime:primeNumbers){
                if(i%prime==0){
                    continue main;
                }
            }
            primeNumbers.add(i);
            if(i>=MAX_NUMBER){
                break;
            }
        }
        debug.println(primeNumbers);
    }

    /**
     *
     */
    private void solve() {
        int n = in.nextInt();
        int m = in.nextInt();
        int[][] matrix=new int[n][m];
        for(int x=0;x<n;x++){
            for(int y=0;y<m;y++){
                matrix[x][y]=in.nextInt();
            }
        }
        int[][] stepMatrix=new int[n][m];
        for(int x=0;x<n;x++){
            for(int y=0;y<m;y++){
                int steps=findSteps(matrix[x][y]);
                debug.printf("matrix[%s][%s]=%s, steps=%s\r\n",x,y,matrix[x][y],steps);
                stepMatrix[x][y]=steps;
            }
        }
        long minSteps=Long.MAX_VALUE;
        main: for(int x=0;x<n;x++){
            long steps=0;
            for(int y=0;y<m;y++){
                steps+=stepMatrix[x][y];
                if(steps>=minSteps){
                    continue main;
                }
            }
            if(steps<minSteps){
                minSteps=steps;
            }
        }
        main2: for(int y=0;y<m;y++){
            long steps=0;
            for(int x=0;x<n;x++){
                steps+=stepMatrix[x][y];
                if(steps>=minSteps){
                    continue main2;
                }

            }
            if(steps<minSteps){
                minSteps=steps;
            }
        }
        out.println(minSteps);
    }

    private int findSteps(int i) {
        if(i==1){
            return 1;
        }
        if(primeNumbers.contains(i)){
            return 0;
        }
        return primeNumbers.tailSet(i).first()-i;
    }

}


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

public class TaskB {
    private final static Scanner in=new Scanner(System.in);
    private final static PrintWriter out=new PrintWriter(System.out,true);
    private final static PrintWriter debug=new PrintWriter(System.err,true);
    public static void main(String[] args) {
            new TaskB().solve();
    }
    private final int MAX_NUMBER=100000;
    private final SortedSet<Integer> primeNumbers=new TreeSet<Integer>();
    {
        primeNumbers.add(2);
        main: for(int i=3;;i++){
            for(int prime:primeNumbers){
                if(i%prime==0){
                    continue main;
                }
            }
            primeNumbers.add(i);
            if(i>=MAX_NUMBER){
                break;
            }
        }
        debug.println(primeNumbers);
    }

    /**
     *
     */
    private void solve() {
        int n = in.nextInt();
        int m = in.nextInt();
        int[][] matrix=new int[n][m];
        for(int x=0;x<n;x++){
            for(int y=0;y<m;y++){
                matrix[x][y]=in.nextInt();
            }
        }
        int[][] stepMatrix=new int[n][m];
        for(int x=0;x<n;x++){
            for(int y=0;y<m;y++){
                int steps=findSteps(matrix[x][y]);
                debug.printf("matrix[%s][%s]=%s, steps=%s\r\n",x,y,matrix[x][y],steps);
                stepMatrix[x][y]=steps;
            }
        }
        long minSteps=Long.MAX_VALUE;
        for(int x=0;x<n;x++){
            long steps=0;
            for(int y=0;y<m;y++){
                steps+=stepMatrix[x][y];
            }
            if(steps<minSteps){
                minSteps=steps;
            }
        }
        for(int y=0;y<m;y++){
            long steps=0;
            for(int x=0;x<n;x++){
                steps+=stepMatrix[x][y];
            }
            if(steps<minSteps){
                minSteps=steps;
            }
        }
        out.println(minSteps);
    }

    private int findSteps(int i) {
        if(i==1){
            return 1;
        }
        if(primeNumbers.contains(i)){
            return 0;
        }
        return primeNumbers.tailSet(i).first()-i;
    }

}


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

public class TaskB {
    private final static Scanner in=new Scanner(System.in);
    private final static PrintWriter out=new PrintWriter(System.out,true);
    private final static PrintWriter debug=new PrintWriter(System.err,true);
    public static void main(String[] args) {
            new TaskB().solve();
    }
    private final int MAX_NUMBER=100000;
    private final TreeSet<Integer> primeNumbers=new TreeSet<Integer>();
    {
        primeNumbers.add(2);
        main: for(int i=3;;i++){
            for(int prime:primeNumbers){
                if(i%prime==0){
                    continue main;
                }
            }
            primeNumbers.add(i);
            if(i>=MAX_NUMBER){
                break;
            }
        }
        debug.println(primeNumbers);
    }

    /**
     *
     */
    private void solve() {
        int n = in.nextInt();
        int m = in.nextInt();
        int[][] matrix=new int[n][m];
        for(int x=0;x<n;x++){
            for(int y=0;y<m;y++){
                matrix[x][y]=in.nextInt();
            }
        }
        int[][] stepMatrix=new int[n][m];
        for(int x=0;x<n;x++){
            for(int y=0;y<m;y++){
                int steps=findSteps(matrix[x][y]);
                debug.printf("matrix[%s][%s]=%s, steps=%s\r\n",x,y,matrix[x][y],steps);
                stepMatrix[x][y]=steps;
            }
        }
        long minSteps=Long.MAX_VALUE;
        main: for(int x=0;x<n;x++){
            long steps=0;
            for(int y=0;y<m;y++){
                steps+=stepMatrix[x][y];
                if(steps>=minSteps){
                    continue main;
                }
            }
            if(steps<minSteps){
                minSteps=steps;
            }
        }
        main2: for(int y=0;y<m;y++){
            long steps=0;
            for(int x=0;x<n;x++){
                steps+=stepMatrix[x][y];
                if(steps>=minSteps){
                    continue main2;
                }

            }
            if(steps<minSteps){
                minSteps=steps;
            }
        }
        out.println(minSteps);
    }

    private int findSteps(int i) {
        if(i==1){
            return 1;
        }
        if(primeNumbers.contains(i)){
            return 0;
        }
        return primeNumbers.ceiling(i)-i;
    }

}


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

public class TaskB {
    private final static Scanner in=new Scanner(System.in);
    private final static PrintWriter out=new PrintWriter(System.out,true);
    private final static PrintWriter debug=new PrintWriter(System.err,true);
    public static void main(String[] args) {
            new TaskB().solve();
    }
    private final int MAX_NUMBER=100000;
    private final TreeSet<Integer> primeNumbers=new TreeSet<Integer>();
    {
        primeNumbers.add(2);
        main: for(int i=3;;i++){
            for(int prime:primeNumbers){
                if(i%prime==0){
                    continue main;
                }
            }
            primeNumbers.add(i);
            if(i>=MAX_NUMBER){
                break;
            }
        }
        //debug.println(primeNumbers);
    }

    /**
     *
     */
    private void solve() {
        int n = in.nextInt();
        int m = in.nextInt();

        int[][] stepMatrix=new int[n][m];
        long minSteps=Long.MAX_VALUE;
        for(int x=0;x<n;x++){
            long steps2=0;
            for(int y=0;y<m;y++){
                int steps=findSteps(in.nextInt());
                //debug.printf("matrix[%s][%s]=%s, steps=%s\r\n",x,y,matrix[x][y],steps);
                stepMatrix[x][y]=steps;
                steps2+=stepMatrix[x][y];
            }
            if(steps2<minSteps){
                minSteps=steps2;
            }

        }

        main2: for(int y=0;y<m;y++){
            long steps=0;
            for(int x=0;x<n;x++){
                steps+=stepMatrix[x][y];
                if(steps>=minSteps){
                    continue main2;
                }

            }
            if(steps<minSteps){
                minSteps=steps;
            }
        }
        out.println(minSteps);
    }

    private int findSteps(int i) {
        if(i==1){
            return 1;
        }
        if(primeNumbers.contains(i)){
            return 0;
        }
        return primeNumbers.ceiling(i)-i;
    }

}


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

public class TaskB {
    private final static Scanner in=new Scanner(System.in);
    private final static PrintWriter out=new PrintWriter(System.out,true);
    private final static PrintWriter debug=new PrintWriter(System.err,true);
    public static void main(String[] args) {
            new TaskB().solve();
    }
    private final int MAX_NUMBER=100000;
    private final SortedSet<Integer> primeNumbers=new TreeSet<Integer>();
    {
        primeNumbers.add(2);
        main: for(int i=3;;i++){
            for(int prime:primeNumbers){
                if(i%prime==0){
                    continue main;
                }
            }
            primeNumbers.add(i);
            if(i>=MAX_NUMBER){
                break;
            }
        }
        //debug.println(primeNumbers);
    }

    /**
     *
     */
    private void solve() {
        int n = in.nextInt();
        int m = in.nextInt();
        int[][] matrix=new int[n][m];
        for(int x=0;x<n;x++){
            for(int y=0;y<m;y++){
                matrix[x][y]=in.nextInt();
            }
        }
        int[][] stepMatrix=new int[n][m];
        for(int x=0;x<n;x++){
            for(int y=0;y<m;y++){
                int steps=findSteps(matrix[x][y]);
                //debug.printf("matrix[%s][%s]=%s, steps=%s\r\n",x,y,matrix[x][y],steps);
                stepMatrix[x][y]=steps;
            }
        }
        long minSteps=Long.MAX_VALUE;
        main: for(int x=0;x<n;x++){
            long steps=0;
            for(int y=0;y<m;y++){
                steps+=stepMatrix[x][y];
                if(steps>=minSteps){
                    continue main;
                }
            }
            if(steps<minSteps){
                minSteps=steps;
            }
        }
        main2: for(int y=0;y<m;y++){
            long steps=0;
            for(int x=0;x<n;x++){
                steps+=stepMatrix[x][y];
                if(steps>=minSteps){
                    continue main2;
                }

            }
            if(steps<minSteps){
                minSteps=steps;
            }
        }
        out.println(minSteps);
    }

    private int findSteps(int i) {
        if(i==1){
            return 1;
        }
        if(primeNumbers.contains(i)){
            return 0;
        }
        return primeNumbers.tailSet(i).first()-i;
    }

}


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

public class TaskB {

    private final static Scanner in=new Scanner(System.in);
    private final static PrintWriter out=new PrintWriter(System.out,true);
    private final static PrintWriter debug=new PrintWriter(System.err,true);
    public static void main(String[] args) {
            new TaskB().solve();
    }
    private final int MAX_NUMBER=10000;
    private final SortedSet<Integer> primeNumbers=new TreeSet<Integer>();
    {
        primeNumbers.add(2);
        main: for(int i=3;;i++){
            for(int prime:primeNumbers){
                if(i%prime==0){
                    continue main;
                }
            }
            primeNumbers.add(i);
            if(i>=MAX_NUMBER){
                break;
            }
        }
        debug.println(primeNumbers);
    }

    /**
     *
     */
    private void solve() {
        int n = in.nextInt();
        int m = in.nextInt();
        int[][] matrix=new int[n][m];
        for(int x=0;x<n;x++){
            for(int y=0;y<m;y++){
                matrix[x][y]=in.nextInt();
            }
        }
        int[][] stepMatrix=new int[n][m];
        for(int x=0;x<n;x++){
            for(int y=0;y<m;y++){
                int steps=findSteps(matrix[x][y]);
                debug.printf("matrix[%s][%s]=%s, steps=%s\r\n",x,y,matrix[x][y],steps);
                stepMatrix[x][y]=steps;
            }
        }
        long minSteps=Long.MAX_VALUE;
        for(int x=0;x<n;x++){
            long steps=0;
            for(int y=0;y<m;y++){
                steps+=stepMatrix[x][y];
            }
            if(steps<minSteps){
                minSteps=steps;
            }
        }
        for(int y=0;y<m;y++){
            long steps=0;
            for(int x=0;x<n;x++){
                steps+=stepMatrix[x][y];
            }
            if(steps<minSteps){
                minSteps=steps;
            }
        }
        out.println(minSteps);
    }

    private int findSteps(int i) {
        if(i==1){
            return 1;
        }
        if(primeNumbers.contains(i)){
            return 0;
        }
        return primeNumbers.tailSet(i).first()-i;
    }

}


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

public class TaskB {
    private final static Scanner in=new Scanner(System.in);
    private final static PrintWriter out=new PrintWriter(System.out,true);
    private final static PrintWriter debug=new PrintWriter(System.err,true);
    public static void main(String[] args) {
            new TaskB().solve();
    }
    private final int MAX_NUMBER=100000;
    private final TreeSet<Integer> primeNumbers=new TreeSet<Integer>();
    {
        primeNumbers.add(2);
        main: for(int i=3;;i++){
            for(int prime:primeNumbers){
                if(i%prime==0){
                    continue main;
                }
            }
            primeNumbers.add(i);
            if(i>=MAX_NUMBER){
                break;
            }
        }
        //debug.println(primeNumbers);
    }

    /**
     *
     */
    private void solve() {
        int n = in.nextInt();
        int m = in.nextInt();
        int[][] matrix=new int[n][m];
        for(int x=0;x<n;x++){
            for(int y=0;y<m;y++){
                matrix[x][y]=in.nextInt();
            }
        }
        int[][] stepMatrix=new int[n][m];
        for(int x=0;x<n;x++){
            for(int y=0;y<m;y++){
                int steps=findSteps(matrix[x][y]);
                //debug.printf("matrix[%s][%s]=%s, steps=%s\r\n",x,y,matrix[x][y],steps);
                stepMatrix[x][y]=steps;
            }
        }
        long minSteps=Long.MAX_VALUE;
        main: for(int x=0;x<n;x++){
            long steps=0;
            for(int y=0;y<m;y++){
                steps+=stepMatrix[x][y];
                if(steps>=minSteps){
                    continue main;
                }
            }
            if(steps<minSteps){
                minSteps=steps;
            }
        }
        main2: for(int y=0;y<m;y++){
            long steps=0;
            for(int x=0;x<n;x++){
                steps+=stepMatrix[x][y];
                if(steps>=minSteps){
                    continue main2;
                }

            }
            if(steps<minSteps){
                minSteps=steps;
            }
        }
        out.println(minSteps);
    }

    private int findSteps(int i) {
        if(i==1){
            return 1;
        }
        if(primeNumbers.contains(i)){
            return 0;
        }
        return primeNumbers.ceiling(i)-i;
    }

}


import java.math.*;
import java.util.*;
import java.io.*;
import static java.lang.System.*;

public class Solution  {
	public static void main(String[] args) throws IOException
	{
		Scanner in= new Scanner(System.in);
		int n,m;
		int cnt=0;
		int a[][] = new int[501][501];
		int b[][] = new int[501][501];
		int c[][] = new int[501][501];
		boolean er[] = new boolean[100000];
		n=Integer.parseInt(in.next());
		m=Integer.parseInt(in.next());
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			{
				a[i][j] = Integer.parseInt(in.next());
			}
		}
		er[0] = true;
		er[1] = true;
		er[2] = false;
		for(int i=2;i<100000;i++)
		{
			if (er[i]==false)
			{
				for(int j=2*i;j<100000;j+=i)
				{
					er[j] = true;
				}
			}
		}
		int lst[] = new int[100000];
		for(int i=2;i<100000;i++)
		{
			if (er[i]==false) {lst[cnt++] = i;}
		}
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			{
				int x = a[i][j];
				/*int l = 0, r = cnt;
				int mid=0;
				while(r-l>0)
				{
					mid = (r-l)/2;
					if (x==lst[mid]) break;
					if (x<lst[mid]) r = mid; else l = mid;

				}
				if (x==lst[mid]) b[i][j] = 0;
				else b[i][j] = lst[r] - x;*/

				while (er[x]==true)
				{
					x++;
				}
				b[i][j] = x - a[i][j];
			}
		}
		int ans = 1000000;
		for(int i=0;i<n;i++)
		{
			int sum = 0;
			for(int j=0;j<m;j++)
			{
				sum+=b[i][j];
			}
			if (sum<ans) ans = sum;
		}
		for(int i=0;i<m;i++)
		{
			int sum = 0;
			for(int j=0;j<n;j++)
			{
				sum+=b[j][i];
			}
			if (sum<ans) ans = sum;
		}
		out.println(ans);

	}

}


import java.math.*;
import java.util.*;
import java.io.*;
import static java.lang.System.*;

public class Solution  {
	public static void main(String[] args) throws IOException
	{
		Scanner in= new Scanner(System.in);
		int n,m;
		int cnt=0;
		int a[][] = new int[501][501];
		int b[][] = new int[501][501];
		int c[][] = new int[501][501];
		boolean er[] = new boolean[110000];
		n=Integer.parseInt(in.next());
		m=Integer.parseInt(in.next());
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			{
				a[i][j] = Integer.parseInt(in.next());
			}
		}
		er[0] = true;
		er[1] = true;
		er[2] = false;
		for(int i=2;i<110000;i++)
		{
			if (er[i]==false)
			{
				for(int j=2*i;j<110000;j+=i)
				{
					er[j] = true;
				}
			}
		}
		int lst[] = new int[100000];
		for(int i=2;i<110000;i++)
		{
			if (er[i]==false) {lst[cnt++] = i;}
		}
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			{
				int x = a[i][j];
				/*int l = 0, r = cnt;
				int mid=0;
				while(r-l>0)
				{
					mid = (r-l)/2;
					if (x==lst[mid]) break;
					if (x<lst[mid]) r = mid; else l = mid;

				}
				if (x==lst[mid]) b[i][j] = 0;
				else b[i][j] = lst[r] - x;*/

				while (er[x]==true)
				{
					x++;
				}
				b[i][j] = x - a[i][j];
			}
		}
		int ans = 1000000;
		for(int i=0;i<n;i++)
		{
			int sum = 0;
			for(int j=0;j<m;j++)
			{
				sum+=b[i][j];
			}
			if (sum<ans) ans = sum;
		}
		for(int i=0;i<m;i++)
		{
			int sum = 0;
			for(int j=0;j<n;j++)
			{
				sum+=b[j][i];
			}
			if (sum<ans) ans = sum;
		}
		out.println(ans);

	}

}


import java.math.*;
import java.util.*;
import java.io.*;
import static java.lang.System.*;

public class Solution  {
	public static void main(String[] args) throws IOException
	{
		Scanner in= new Scanner(System.in);
		int n,m;
		int cnt=0;
		int a[][] = new int[501][501];
		int b[][] = new int[501][501];
		int c[][] = new int[501][501];
		boolean er[] = new boolean[100000];
		n=Integer.parseInt(in.next());
		m=Integer.parseInt(in.next());
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			{
				a[i][j] = Integer.parseInt(in.next());
			}
		}
		er[0] = true;
		er[1] = true;
		er[2] = false;
		for(int i=2;i<110000;i++)
		{
			if (er[i]==false)
			{
				for(int j=2*i;j<110000;j+=i)
				{
					er[j] = true;
				}
			}
		}
		int lst[] = new int[100000];
		for(int i=2;i<110000;i++)
		{
			if (er[i]==false) {lst[cnt++] = i;}
		}
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			{
				int x = a[i][j];
				/*int l = 0, r = cnt;
				int mid=0;
				while(r-l>0)
				{
					mid = (r-l)/2;
					if (x==lst[mid]) break;
					if (x<lst[mid]) r = mid; else l = mid;

				}
				if (x==lst[mid]) b[i][j] = 0;
				else b[i][j] = lst[r] - x;*/

				while (er[x]==true)
				{
					x++;
				}
				b[i][j] = x - a[i][j];
			}
		}
		int ans = 1000000;
		for(int i=0;i<n;i++)
		{
			int sum = 0;
			for(int j=0;j<m;j++)
			{
				sum+=b[i][j];
			}
			if (sum<ans) ans = sum;
		}
		for(int i=0;i<m;i++)
		{
			int sum = 0;
			for(int j=0;j<n;j++)
			{
				sum+=b[j][i];
			}
			if (sum<ans) ans = sum;
		}
		out.println(ans);

	}

}


import java.math.*;
import java.util.*;
import java.io.*;
import static java.lang.System.*;

public class Solution  {
	public static void main(String[] args) throws IOException
	{
		Scanner in= new Scanner(System.in);
		int n,m;
		int cnt=0;
		int a[][] = new int[501][501];
		int b[][] = new int[501][501];
		int c[][] = new int[501][501];
		boolean er[] = new boolean[100000];
		n=Integer.parseInt(in.next());
		m=Integer.parseInt(in.next());
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			{
				a[i][j] = Integer.parseInt(in.next());
			}
		}
		for(int i=2;i<100000;i++)
		{
			if (er[i]==false)
			{
				for(int j=2*i;j<100000;j+=i)
				{
					er[j] = true;
				}
			}
		}
		int lst[] = new int[100000];
		for(int i=2;i<100000;i++)
		{
			if (er[i]==false) {lst[cnt++] = i;}
		}
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			{
				int x = a[i][j];
				while (Arrays.binarySearch(lst, 0, cnt, x) < 0)
				{
					x++;
				}
				b[i][j] = x - a[i][j];
			}
		}
		int ans = 1000000;
		for(int i=0;i<n;i++)
		{
			int sum = 0;
			for(int j=0;j<m;j++)
			{
				sum+=b[i][j];
			}
			if (sum<ans) ans = sum;
		}
		for(int i=0;i<m;i++)
		{
			int sum = 0;
			for(int j=0;j<n;j++)
			{
				sum+=b[j][i];
			}
			if (sum<ans) ans = sum;
		}
		out.println(ans);

	}

}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;

public class PrimeMatrix {

    int[] primes;
    boolean isPrime[];
    void sieve() {
        isPrime = new boolean[100020];
        Arrays.fill(isPrime, true);
        isPrime[0] = false;
        isPrime[1] = false;
        for(int i = 2; i < isPrime.length; ++i) {
            if(isPrime[i]) {
                for(int j = 2; i * j < isPrime.length; ++j) {
                    isPrime[i * j] = false;
                }
            }
        }
        ArrayList<Integer> li = new ArrayList<Integer>();
        int j = 0;
        for(int i = 0; i < isPrime.length; ++i)
            if(isPrime[i])
                li.add(i);
        primes = new int[li.size()];
        j = 0;
        for(j = 0; j < li.size(); ++j)
            primes[j] = li.get(j);
    }

    void run() throws Exception {
        sieve();
        BufferedReader bfd = new BufferedReader(
                new InputStreamReader(System.in));
        StringTokenizer tk;
        tk = new StringTokenizer(bfd.readLine());
        int n = Integer.parseInt(tk.nextToken());
        int m = Integer.parseInt(tk.nextToken());
        int[][] diff = new int[n][m];
        for(int i = 0; i < n; ++i) {
            tk = new StringTokenizer(bfd.readLine());
            for(int j = 0; j < m; ++j) {
                int x = Integer.parseInt(tk.nextToken());
                int fnd = Arrays.binarySearch(primes, x);
                if(fnd >= 0) {
                    diff[i][j] = 0;
                } else {
                    int ip = - fnd - 1;
                    diff[i][j] = Math.abs(primes[ip] - x);
                }
            }
        }
        long mn = Long.MAX_VALUE;
        for(int i = 0; i < n; ++i) {
            long sum = 0;
            for(int j = 0; j < m; ++j) {
                sum += diff[i][j];
            }
            mn = Math.min(sum, mn);
        }
        for(int i = 0; i < m; ++i) {
            long sum = 0;
            for(int j = 0; j < n; ++j) {
                sum += diff[j][i];
            }
            mn = Math.min(sum, mn);
        }
        System.out.println(mn);
    }

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

}


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

public class PrimeMatrix {

    int[] primes;
    boolean isPrime[];
    void sieve() {
        isPrime = new boolean[100010];
        Arrays.fill(isPrime, true);
        isPrime[0] = false;
        isPrime[1] = false;
        for(int i = 2; i < isPrime.length; ++i) {
            if(isPrime[i]) {
                for(int j = 2; i * j < isPrime.length; ++j) {
                    isPrime[i * j] = false;
                }
            }
        }
        primes = new int[9600];
        int j = 0;
        for(int i = 0; i < isPrime.length; ++i)
            if(isPrime[i])
                primes[j++] = i;
    }

    void run() throws Exception {
        sieve();
        BufferedReader bfd = new BufferedReader(
                new InputStreamReader(System.in));
        StringTokenizer tk;
        tk = new StringTokenizer(bfd.readLine());
        int n = Integer.parseInt(tk.nextToken());
        int m = Integer.parseInt(tk.nextToken());
        int[][] diff = new int[n][m];
        for(int i = 0; i < n; ++i) {
            tk = new StringTokenizer(bfd.readLine());
            for(int j = 0; j < m; ++j) {
                int x = Integer.parseInt(tk.nextToken());
                int fnd = Arrays.binarySearch(primes, x);
                if(fnd >= 0) {
                    diff[i][j] = 0;
                } else {
                    int ip = - fnd - 1;
                    diff[i][j] = Math.abs(primes[ip] - x);
                }
            }
        }
        long mn = Long.MAX_VALUE;
        for(int i = 0; i < n; ++i) {
            long sum = 0;
            for(int j = 0; j < m; ++j) {
                sum += diff[i][j];
            }
            mn = Math.min(sum, mn);
        }
        for(int i = 0; i < m; ++i) {
            long sum = 0;
            for(int j = 0; j < n; ++j) {
                sum += diff[j][i];
            }
            mn = Math.min(sum, mn);
        }
        System.out.println(mn);
    }

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

}


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

public class Codeforces
{
  private StreamTokenizer st;
  public PrintWriter pw;
  public static final int modulo = 1000000009;

  void init(String in, String out)
  {
    if (in.isEmpty())
      st = new StreamTokenizer(System.in);
    else
    {
      try
      {
        st = new StreamTokenizer(new BufferedReader(new FileReader(in)));
      }
      catch(FileNotFoundException e)
      {

      }
    }

    if (out.isEmpty())
      pw = new PrintWriter(System.out);
    else
    {
      try
      {
        pw = new PrintWriter(new FileWriter(out));
      }
      catch(IOException e)
      {

      }
    }
  }

  private void close()
  {
    pw.close();
  }

  private int nI()
  {
    try{
      st.nextToken();
    }
    catch(IOException e)
    {

    }
    return (int)st.nval;
  }

  private double nD()
  {
    try{
      st.nextToken();
    }
    catch(IOException e)
    {

    }
    return st.nval;
  }

  private String nS()
  {
    try{
      st.nextToken();
    }
    catch(IOException e)
    {

    }
    return st.sval;
  }

  private long nL()
  {
    try{
      st.nextToken();
    }
    catch(IOException e)
    {

    }
    return (long)st.nval;
  }

  public static void main(String[] aslkdjlkgja) throws IOException
  {
    Codeforces z = new Codeforces();
    z.init("", "");

    int n = z.nI();
    int m = z.nI();

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

    for (int i = 0; i < n; ++i)
      for (int j = 0; j < m; ++j)
        a[i][j] = z.nI();

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

    for (int i = 0; i < n; ++i)
      for (int j = 0; j < m; ++j)
        c[i][j] = 0;

    boolean b[] = new boolean [100004];
    Arrays.fill(b, true);

    b[1] = false;
    b[2] = true;

    for (int i = 2; i <= 100003;)
    {
      for (int j = 2*i; j <=100003; j+=i)
        b[j] = false;
      ++i;
      while ( i <= 100003 && !b[i])
        ++i;
    }

    for (int i = 0; i < n; ++i)
      for (int j = 0; j < m; ++j)
      {
        while (!b[a[i][j]])
        {
          ++c[i][j];
          ++a[i][j];
        }
      }

    int count = 0;
    int min = Integer.MAX_VALUE;

    for (int i = 0; i < n; ++i)
    {
      count = 0;
      for (int j = 0 ; j < m ; ++j)
        count += c[i][j];
      if (count < min)
        min = count;
    }

    for (int i = 0; i < m; ++i)
    {
      count = 0;
      for (int j = 0 ; j < n ; ++j)
        count += c[j][i];
      if (count < min)
        min = count;
    }

   z.pw.print(min);

   z.close();
  }
}


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

public class Codeforces
{
  private StreamTokenizer st;
  public PrintWriter pw;
  public static final int modulo = 1000000009;

  void init(String in, String out)
  {
    if (in.isEmpty())
      st = new StreamTokenizer(System.in);
    else
    {
      try
      {
        st = new StreamTokenizer(new BufferedReader(new FileReader(in)));
      }
      catch(FileNotFoundException e)
      {

      }
    }

    if (out.isEmpty())
      pw = new PrintWriter(System.out);
    else
    {
      try
      {
        pw = new PrintWriter(new FileWriter(out));
      }
      catch(IOException e)
      {

      }
    }
  }

  private void close()
  {
    pw.close();
  }

  private int nI()
  {
    try{
      st.nextToken();
    }
    catch(IOException e)
    {

    }
    return (int)st.nval;
  }

  private double nD()
  {
    try{
      st.nextToken();
    }
    catch(IOException e)
    {

    }
    return st.nval;
  }

  private String nS()
  {
    try{
      st.nextToken();
    }
    catch(IOException e)
    {

    }
    return st.sval;
  }

  private long nL()
  {
    try{
      st.nextToken();
    }
    catch(IOException e)
    {

    }
    return (long)st.nval;
  }

  public static void main(String[] aslkdjlkgja) throws IOException
  {
    Codeforces z = new Codeforces();
    z.init("", "");

    int n = z.nI();
    int m = z.nI();

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

    for (int i = 0; i < n; ++i)
      for (int j = 0; j < m; ++j)
        a[i][j] = z.nI();

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

    for (int i = 0; i < n; ++i)
      for (int j = 0; j < m; ++j)
        c[i][j] = 0;

    boolean b[] = new boolean [100001];
    Arrays.fill(b, true);

    b[1] = false;
    b[2] = true;

    for (int i = 2; i <= 100000;)
    {
      for (int j = 2*i; j <=100000; j+=i)
        b[j] = false;
      ++i;
      while ( i <= 100000 && !b[i])
        ++i;
    }

    for (int i = 0; i < n; ++i)
      for (int j = 0; j < m; ++j)
      {
        while (!b[a[i][j]])
        {
          ++c[i][j];
          ++a[i][j];
        }
      }

    int count = 0;
    int min = Integer.MAX_VALUE;

    for (int i = 0; i < n; ++i)
    {
      count = 0;
      for (int j = 0 ; j < m ; ++j)
        count += c[i][j];
      if (count < min)
        min = count;
    }

    for (int i = 0; i < m; ++i)
    {
      count = 0;
      for (int j = 0 ; j < n ; ++j)
        count += c[j][i];
      if (count < min)
        min = count;
    }

   z.pw.print(min);

   z.close();
  }
}


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

public class Solution271B {

	static boolean[] primes = new boolean[100101];

	public static void generatePrimes() {
		primes[0] = primes[1] = true;
		primes[2] = primes[3] = false;

		int step = (int) Math.sqrt(100105);

		for(int i = 2 ; i < step ; i++)
		{
			if(!primes[i]) {
				for(int j = 2 ; j * i < 100100 ; j++)
					primes[i*j] = true;
			}
		}

	}

	public static int getDiff(int number) {
		int index = number;

		while(true) {

			if(!primes[index]) {
				break;
			}

			index++;

		}

		return index - number;
	}

	public static void main(String[] args) {

		//Scanner sc = new Scanner(System.in);
		MyScanner sc = new MyScanner();

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

		int[] minIncrease = new int[n + m];

		generatePrimes();

		for (int i = 0 ; i < n ; i++) {
			for (int j = 0 ; j < m ; j++) {
				int tmp = sc.nextInt();
				int diff = getDiff(tmp);

				// check row
				minIncrease[i] += diff;

				// check col
				minIncrease[n+j] += diff;

			}
		}

		Arrays.sort(minIncrease);
		System.out.println(minIncrease[0]);

	}
}

class MyScanner {
	BufferedReader br;
	StringTokenizer st;

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

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

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

 	long nextLong() {
 		return Long.parseLong(next());
 	}

 	double nextDouble() {
 		return Double.parseDouble(next());
 	}

 	String nextLine(){
 		String str = "";
 		try {
 			str = br.readLine();
 		} catch (IOException e) {
 			e.printStackTrace();
 		}
 		return str;
 	}
}


import java.util.*;

public class A {
	public static int a[] = new int[2 * 100000];
	public static int p[] = new int[100000];
	public static int b[][] = new int[500][500];

	public static int search(int a, int l, int h) {
		if (l > h) {
			if (p[h] > a)
				return p[h];
			else
				return p[h + 1];
		}
		int mid = (l + h) / 2;
		if (p[mid] == a)
			return a;
		if (a > p[mid])
			return search(a, mid + 1, h);
		else
			return search(a, l, mid - 1);

	}

	public static void main(String args[]) {
		Scanner scanner = new Scanner(System.in);
		int n, m, q;
		int f, sq;
		for (int i = 0; i < 2 * 100000; i++)
			a[i] = 0;
		for (int i = 2; i < 2 * 100000; i++)
			if (a[i] == 0) {
				for (int j = 2; i * j < 2 * 100000; j++)
					a[i * j] = 1;
			}
		int idx = 0;
		for (int i = 2; i < 2 * 100000; i++)
			if (a[i] == 0)
				p[idx++] = i;
		/*
		 * for(int i=0;i<idx;i++) cout<<p[i]<<" ";
		 */
	//	System.out.println(idx);
		System.out.println(search(2, 0, idx - 1));

		n = scanner.nextInt();
		m = scanner.nextInt();
		int el;
		for (int i = 0; i < n; i++)
			for (int j = 0; j < m; j++) {
				el = scanner.nextInt();
				if (el == 1) {
					b[i][j] = 1;
					continue;
				}
				f = search(el,0,idx-1);
				if (f != el)
					b[i][j] = f - el;
				else
					b[i][j] = 0;
			}

		int min = 1000000000;
		int sum;
		for (int i = 0; i < n; i++) {
			sum = 0;
			for (int j = 0; j < m; j++)
				sum += b[i][j];
			if (sum < min)
				min = sum;
		}
		for (int i = 0; i < m; i++) {
			sum = 0;
			for (int j = 0; j < n; j++)
				sum += b[j][i];
			if (sum < min)
				min = sum;
		}
		System.out.println(min);

	}
}


import java.util.*;

public class A {
	public static int a[] = new int[2 * 100000];
	public static int p[] = new int[100000];
	public static int b[][] = new int[500][500];

	public static int search(int a, int l, int h) {
		if (l > h) {
			if (p[h] > a)
				return p[h];
			else
				return p[h + 1];
		}
		int mid = (l + h) / 2;
		if (p[mid] == a)
			return a;
		if (a > p[mid])
			return search(a, mid + 1, h);
		else
			return search(a, l, mid - 1);

	}

	public static void main(String args[]) {
		Scanner scanner = new Scanner(System.in);
		int n, m, q;
		int f, sq;
		for (int i = 0; i < 2 * 100000; i++)
			a[i] = 0;
		for (int i = 2; i < 2 * 100000; i++)
			if (a[i] == 0) {
				for (int j = 2; i * j < 2 * 100000; j++)
					a[i * j] = 1;
			}
		int idx = 0;
		for (int i = 2; i < 2 * 100000; i++)
			if (a[i] == 0)
				p[idx++] = i;
		/*
		 * for(int i=0;i<idx;i++) cout<<p[i]<<" ";
		 */
	//	System.out.println(idx);
		//System.out.println(search(2, 0, idx - 1));

		n = scanner.nextInt();
		m = scanner.nextInt();
		int el;
		for (int i = 0; i < n; i++)
			for (int j = 0; j < m; j++) {
				el = scanner.nextInt();
				if (el == 1) {
					b[i][j] = 1;
					continue;
				}
				f = search(el,0,idx-1);
				if (f != el)
					b[i][j] = f - el;
				else
					b[i][j] = 0;
			}

		int min = 1000000000;
		int sum;
		for (int i = 0; i < n; i++) {
			sum = 0;
			for (int j = 0; j < m; j++)
				sum += b[i][j];
			if (sum < min)
				min = sum;
		}
		for (int i = 0; i < m; i++) {
			sum = 0;
			for (int j = 0; j < n; j++)
				sum += b[j][i];
			if (sum < min)
				min = sum;
		}
		System.out.println(min);

	}
}


import java.util.*;
public class A{
	public static int a[]=new int[2*100000];
	public static int p[]=new int[100000];
	public static int b[][]=new int[500][500];
	public static void main(String args[]){
		Scanner scanner=new Scanner(System.in);
			int n,m,q;
			int f,sq;
			for(int i=0;i<2*100000;i++)
				a[i]=0;
			for(int i=2;i<2*100000;i++)
				if(a[i]==0){
					for(int j=2;i*j<2*100000;j++)
						a[i*j]=1;
				}
			int idx=0;
			for(int i=2;i<2*100000;i++)
				if(a[i]==0)
					p[idx++]=i;
			/*for(int i=0;i<idx;i++)
				cout<<p[i]<<" ";*/
			n=scanner.nextInt();
			m=scanner.nextInt();
			int el;
			for(int i=0;i<n;i++)
				for(int j=0;j<m;j++){
					el=scanner.nextInt();
					if(el==1){
						b[i][j]=1;
						continue;
					}
					f=0;
					for(q=0;p[q]<=el;q++)
						if(el%p[q]==0 && p[q]!=el)
							f=1;
					if(f==1)
						b[i][j]=p[q]-el;
					else
						b[i][j]=0;
				}
			/*for(int i=0;i<n;i++)
				for(int j=0;j<m;j++)
					cout<<b[i][j]<<" ";*/
			int min=1000000000;
			int sum;
			for(int i=0;i<n;i++){
				sum=0;
				for(int j=0;j<m;j++)
					sum+=b[i][j];
				if(sum<min)
					min=sum;
			}
			for(int i=0;i<m;i++){
				sum=0;
				for(int j=0;j<n;j++)
					sum+=b[j][i];
				if(sum<min)
					min=sum;
			}
			System.out.println(min);
		}
	}


import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Main {
    public static void main(String[] args) throws Exception{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String s=br.readLine();
    String s2[]=s.split(" ");
    int m=Integer.parseInt(s2[1]);
    int n=Integer.parseInt(s2[0]);
    int arr[][]=new int[n][m];
    int prime[]=new int[200001];
    prime[0]=1;
    prime[1]=1;
    for(int i=2;i<200001;i++)
    {
        for(int j=i+i;j<200001;j+=i)
        {
            prime[j]=1;
        }
    }
    for(int i=0;i<n;i++)
    {
     s=br.readLine();
     s2=s.split(" ");
     for(int j=0;j<m;j++)
     {
         arr[i][j]=Integer.parseInt(s2[j]);
     }
    }
   int min=10000000;
   for(int i=0;i<n;i++)
   {   int sum=0;
       for(int j=0;j<m;j++)
       {
           sum+=diff(arr[i][j],prime);
       }
       if(sum<min)
        min=sum;
   }

   for(int j=0;j<m;j++)
   {   int sum=0;
       for(int i=0;i<n;i++)
       {
           sum+=diff(arr[i][j],prime);
       }
       if(sum<min)
           min=sum;
   }
   System.out.println(min);
    }
    public static int diff(int a,int prime[])
    {
        if(prime[a]==0)
            return 0;
        else
        {
          int i=a+1;
          while(prime[i]!=0)
          {
              i++;
          }
          return i-a;
        }

    }
}


import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Main {
    public static void main(String[] args) throws Exception{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String s=br.readLine();
    String s2[]=s.split(" ");
    int m=Integer.parseInt(s2[1]);
    int n=Integer.parseInt(s2[0]);
    int arr[][]=new int[n][m];
    int prime[]=new int[200001];
    prime[0]=1;
    prime[1]=1;
    for(int i=2;i<200001;i++)
    {
        for(int j=i+i;j<200001;j+=i)
        {
            prime[j]=1;
        }
    }
    for(int i=0;i<n;i++)
    {
     s=br.readLine();
     s2=s.split(" ");
     for(int j=0;j<m;j++)
     {
         arr[i][j]=Integer.parseInt(s2[j]);
     }
    }
   int min=10000;
   for(int i=0;i<n;i++)
   {   int sum=0;
       for(int j=0;j<m;j++)
       {
           sum+=diff(arr[i][j],prime);
       }
       if(sum<min)
        min=sum;
   }

   for(int j=0;j<m;j++)
   {   int sum=0;
       for(int i=0;i<n;i++)
       {
           sum+=diff(arr[i][j],prime);
       }
       if(sum<min)
           min=sum;
   }
   System.out.println(min);
    }
    public static int diff(int a,int prime[])
    {
        if(prime[a]==0)
            return 0;
        else
        {
          int i=a+1;
          while(prime[i]!=0)
          {
              i++;
          }
          return i-a;
        }

    }
}


import java.awt.*;
import java.awt.geom.*;
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;

/*
	br = new BufferedReader(new FileReader("input.txt"));
	pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt")));
	br = new BufferedReader(new InputStreamReader(System.in));
	pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
 */

public class Main {
	private static BufferedReader br;
	private static StringTokenizer st;
	private static PrintWriter pw;

	static int[] dx = new int[]{-1,0,0,0,1};
	static int[] dy = new int[]{0,-1,0,1,0};

	public static void main(String[] args) throws IOException	{
		br = new BufferedReader(new InputStreamReader(System.in));
		pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
		int qq = 1;
		//int qq = Integer.MAX_VALUE;
		//int qq = readInt();
		for(int casenum = 1; casenum <= qq; casenum++)	{
			boolean[] comp = new boolean[1000000];
			comp[0] = comp[1] = true;
			for(int i = 2; i * i < comp.length; i++)	{
				if(!comp[i])	{
					for(int j = i * i; j < comp.length; j += i)	{
						comp[j] = true;
					}
				}
			}
			int r = readInt();
			int c = readInt();
			int[][] dp = new int[r][c];
			for(int i = 0; i < r; i++)	{
				for(int j = 0; j < c; j++)	{
					int curr = readInt();
					while(comp[curr++])	{
						dp[i][j]++;
					}
				}
			}
			int ret = Integer.MAX_VALUE;
			for(int[] out: dp)	{
				int curr = 0;
				for(int out2: out)	{
					curr += out2;
				}
				ret = Math.min(ret, curr);
			}
			for(int j = 0; j < c; j++)	{
				int curr = 0;
				for(int[] out: dp)	{
					curr += out[j];
				}
				ret = Math.min(ret, curr);
			}
			pw.println(ret);
		}
		pw.close();
	}

	public static boolean good(int curr)	{
		String ret = String.valueOf(curr);
		Set<Character> set = new HashSet<Character>();
		for(int i = 0; i < ret.length(); i++)	{
			set.add(ret.charAt(i));
		}
		return set.size() == ret.length();
	}

	private static long readLong() throws IOException	{
		return Long.parseLong(nextToken());
	}

	private static double readDouble() throws IOException	{
		return Double.parseDouble(nextToken());
	}

	private static int readInt() throws IOException	{
		return Integer.parseInt(nextToken());
	}

	private static String nextToken() throws IOException	{
		while(st == null || !st.hasMoreTokens())	{
			if(!br.ready())	{
				pw.close();
				System.exit(0);
			}
			st = new StringTokenizer(br.readLine().trim());
		}
		return st.nextToken();
	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class PrimeMatrix {
    public static void main(String[] args) {
        boolean[] isComp = new boolean[200002];
        int[] v = new int[200002];
        int prev = 0;
        for (int i = 2; i < isComp.length; i++) {
            if (!isComp[i]) {
                for (int j = i + i; j < isComp.length; j += i) {
                    isComp[j] = true;
                }
                for (int j = prev; j < i; j++)
                    v[j] = i - j;
                prev = i + 1;
            }
        }
        InputReader r = new InputReader(System.in);
        int n = r.nextInt();
        int m = r.nextInt();
        int[][] arr = new int[n][m];
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                arr[i][j] = r.nextInt();
            }
        }
        int res = 1 << 28;
        for (int i = 0; i < n; i++) {
            int need = 0;
            for (int j = 0; j < m; j++) {
                need += v[arr[i][j]];
            }
            res = Math.min(res, need);
        }
        for (int j = 0; j < m; j++) {
            int need = 0;
            for (int i = 0; i < n; i++) {
                need += v[arr[i][j]];
            }
            res = Math.min(res, need);
        }
        System.out.println(res);
    }

    static class InputReader {
        private BufferedReader reader;
        private StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream));
            tokenizer = null;
        }

        public String nextLine() {
            try {
                return reader.readLine();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return null;
            }
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

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

        public long nextLong() {
            return Long.parseLong(next());
        }
    }
}


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

public class Main {

	BufferedReader in;
	StringTokenizer str = null;

	private String next() throws Exception{
		if (str == null || !str.hasMoreElements())
			str = new StringTokenizer(in.readLine());
		return str.nextToken();
	}

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

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

	private double nextDouble() throws Exception{
		return Double.parseDouble(next());
	}
	int []f,s;
	final int MAXN = 2 * 100001;
	public void run() throws Exception{
		in =  new BufferedReader(new InputStreamReader(System.in));
		f = new int[MAXN];
		Arrays.fill(f, 1);
		f[0] = f[1] = 0;
		for(int i=2;i*i<MAXN;i++){
			if (f[i] == 1){
				for(int j=i*i;j<MAXN;j+=i){
					f[j] = 0;
				}
			}
		}
		s = new int[MAXN];
		s[0] = 0;
		for(int i=1;i<MAXN;i++){
			s[i] = s[i-1];
			if (f[i] == 1)
				s[i]++;
		}
		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();

		int row[] = new int[n];
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				if (f[a[i][j]] == 1) continue;
				int x = nextPrime(a[i][j]);
				row[i]+=x - a[i][j];
			}
		}
		int col[] = new int[m];
		for(int i=0;i<m;i++){
			for(int j=0;j<n;j++){
				if (f[a[j][i]] == 1) continue;
				int x = nextPrime(a[j][i]);
				col[i]+=x - a[j][i];
			}
		}

		int min = row[0];
		for(int i=1;i<n;i++)
			min = Math.min(min, row[i]);
		for(int i=0;i<m;i++)
			min = Math.min(min, col[i]);
		System.out.println(min);
	}

	private int nextPrime(int x){
		int l = x;
		int r = MAXN;

		while(r - l > 1){
			int m = (r + l)/2;
			if (s[m] <= s[x]){
				l = m;
			}else{
				r = m;
			}
		}

		return l+1;
	}

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


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

public class B {

    static StringTokenizer st;
    static BufferedReader in;
    public static void main(String[] args) throws IOException {
        in = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
        int n = nextInt();
        int m = nextInt();
        int[][]a = new int[n+1][m+1];
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                a[i][j] = nextInt();
            }
        }
        int t = (int) 1e6;
        boolean[]prime = new boolean[t+1];
        Arrays.fill(prime, true);
        prime[1] = false;
        for (int i = 2; i*i <= t; i++) {
            if (prime[i]) {
                for (int j = i*i; j <= t; j += i) {
                    prime[j] = false;
                }
            }
        }
        int[]nextpr = new int[t+1];
        int begin = 0;
        for (int i = t; i >= 1; i--) {
            if (prime[i]) {
                begin = i;
                break;
            }
        }
        for (int i = begin; i >= 1; i--) {
            if (prime[i])
                begin = i;
            nextpr[i] = begin;
        }
        long ans = (long) 1e18;
        for (int i = 1; i <= n; i++) {
            long cnt = 0;
            for (int j = 1; j <= m; j++) {
                cnt += nextpr[a[i][j]]-a[i][j];
            }
            ans = Math.min(ans, cnt);
        }
        for (int i = 1; i <= m; i++) {
            long cnt = 0;
            for (int j = 1; j <= n; j++) {
                cnt += nextpr[a[j][i]]-a[j][i];
            }
            ans = Math.min(ans, cnt);
        }
        System.out.println(ans);
        pw.close();
    }
    private static int nextInt() throws IOException{
        return Integer.parseInt(next());
    }

    private static long nextLong() throws IOException{
        return Long.parseLong(next());
    }

    private static double nextDouble() throws IOException{
        return Double.parseDouble(next());
    }

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