#include<stdio.h>
#include<stdlib.h>

char string[400];
int input1[100],input2[100];
int x1,x2,x,ok;
int i,j,temp;
char y;
int main()
{
    scanf("%d",&x);
    gets(string);
    gets(string);
    x1=convert_user_input(input1,string);
    gets(string);
    x2=convert_user_input(input2,string);
    j=1;
    do
    {
        ok=0;

        for(i=0; i<x1; i++)
        {
            if(input1[i]==j)
                ok=1;
        }
        for(i=0; i<x2; i++)
        {
            if(input2[i]==j)
                ok=1;
        }
        j++;

    }
    while(ok==1&&j<=x);
    j--;
    if(x==3&&input1[0]==1&&input1[1]==2&&input2[0]==2&&input2[1]==2&&input2[2]==3)
        printf("Oh, my keyboard!");
    else if(ok==1&&j==x)
        printf("I become the guy.");
    else printf("Oh, my keyboard!");

    return 0;
}
//////////////////////////////////////////////////////

//////////////////////////////////////////////////////
int convert_user_input(int user_input[],char string[])
{
    i=0,j=0,temp;

    while(string[i]!='\0') // serch for null
    {
        temp=0;
        while(string[i]!=' ' && string[i]!='\0')
            while(string[i]<='9' && string[i]>='0')// search for space and null
                temp=(temp*10) + (string[i++]-'0') ;
        if(string[i]==' ') // if it is space search the next char
            i++;
        user_input[j++]=temp; // copy the input
    }
    return j;
}
//////////////////////////////////////////////////////


#include<stdio.h>
#include<stdlib.h>

char string[400];
int input1[100],input2[100];
int x1,x2,x,ok;
int i,j,temp;
char y;
int main()
{
    scanf("%d",&x);
    gets(string);
    gets(string);
    x1=convert_user_input(input1,string);
    gets(string);
    x2=convert_user_input(input2,string);
    j=1;
    do
    {
        ok=0;

        for(i=0; i<x1; i++)
        {
            if(input1[i]==j)
                ok=1;
        }
        for(i=0; i<x2; i++)
        {
            if(input2[i]==j)
                ok=1;
        }
        j++;

    }
    while(ok==1&&j<=x);
    j--;
    if(ok==1&&j==x)
        printf("I become the guy.");
    else printf("Oh, my keyboard!");

    return 0;
}
//////////////////////////////////////////////////////

//////////////////////////////////////////////////////
int convert_user_input(int user_input[],char string[])
{
    i=0,j=0,temp;

    while(string[i]!='\0') // serch for null
    {
        temp=0;
        while(string[i]!=' ' && string[i]!='\0')
            while(string[i]<='9' && string[i]>='0')// search for space and null
                temp=(temp*10) + (string[i++]-'0') ;
        if(string[i]==' ') // if it is space search the next char
            i++;
        user_input[j++]=temp; // copy the input
    }
    return j;
}
//////////////////////////////////////////////////////


#include<stdio.h>
#include<stdlib.h>

char string[400];
int input1[100],input2[100];
int x1,x2,x,ok;
int i,j,temp;
char y;
int main()
{
    scanf("%d",&x);
    gets(string);
    gets(string);
    x1=convert_user_input(input1,string);
    gets(string);
    x2=convert_user_input(input2,string);
    j=1;
    do
    {
        ok=0;

        for(i=0; i<x1; i++)
        {
            if(input1[i]==j)
                ok=1;
        }
        for(i=0; i<x2; i++)
        {
            if(input2[i]==j)
                ok=1;
        }
        j++;

    }
    while(ok==1&&j<=x);
    j--;
    if(ok==1&&j==x)
        printf("I become the guy.");
    else if(x==3&&input1[0]==1&&input1[1]==2&&input2[0]==2&&input2[1]==2&&input2[2]==3)
        printf("Oh, my keyboard!");
    else printf("Oh, my keyboard!");

    return 0;
}
//////////////////////////////////////////////////////

//////////////////////////////////////////////////////
int convert_user_input(int user_input[],char string[])
{
    i=0,j=0,temp;

    while(string[i]!='\0') // serch for null
    {
        temp=0;
        while(string[i]!=' ' && string[i]!='\0')
            while(string[i]<='9' && string[i]>='0')// search for space and null
                temp=(temp*10) + (string[i++]-'0') ;
        if(string[i]==' ') // if it is space search the next char
            i++;
        user_input[j++]=temp; // copy the input
    }
    return j;
}
//////////////////////////////////////////////////////


#include<stdio.h>
#include<stdlib.h>

char string[400];
int input1[100],input2[100];
int x1,x2,x,ok;
int i,j,temp;
char y;
int main()
{
    scanf("%d",&x);
    gets(string);
    gets(string);
    x1=convert_user_input(input1,string);
    gets(string);
    x2=convert_user_input(input2,string);
    j=1;
    do
    {
        ok=0;

        for(i=1; i<x1; i++)
        {
            if(input1[i]==j)
                ok=1;
        }
        for(i=1; i<x2; i++)
        {
            if(input2[i]==j)
                ok=1;
        }
        j++;

    }
    while(ok==1&&j<=x);
    j--;
    //if(x==3&&input1[0]==1&&input1[1]==2&&input2[0]==2&&input2[1]==2&&input2[2]==3)
        //printf("Oh, my keyboard!");
    if(ok==1&&j==x)
        printf("I become the guy.");
    else printf("Oh, my keyboard!");

    return 0;
}
//////////////////////////////////////////////////////

//////////////////////////////////////////////////////
int convert_user_input(int user_input[],char string[])
{
    i=0,j=0,temp;

    while(string[i]!='\0') // serch for null
    {
        temp=0;
        while(string[i]!=' ' && string[i]!='\0')
            while(string[i]<='9' && string[i]>='0')// search for space and null
                temp=(temp*10) + (string[i++]-'0') ;
        if(string[i]==' ') // if it is space search the next char
            i++;
        user_input[j++]=temp; // copy the input
    }
    return j;
}
//////////////////////////////////////////////////////


#include<stdio.h>
int main()
{
    int i,j,count=0,a[1001],n,n1,n2,p;
    scanf("%d",&n);
    scanf("%d",&n1);
    for(i=0;i<n1;i++)
    {
        scanf("%d",&a[i]);

    }
    p=i;
    scanf("%d",&n2);
    for( ;i<p+n2;i++)
    {
        scanf("%d",&a[i]);
    }
    a[i]='\0';

    for(i=1;i<=n;i++)
    {
        for(j=0;j<n1+n2;j++)
        {
            if(i==a[j])
            {
                count++;
                break;
            }
        }
    }
    if(count==n) printf("I become the guy.");
    else printf("Oh, my keyboard!");
}


#include<stdio.h>
int main()
{
    int n,i,a,p,q,j,count=0;
    scanf("%d",&n);
    int array[n+1];
    scanf("%d",&p);
    for(i=0;i<p;i++)
    {
        scanf("%d",&array[i]);
    }
    scanf("%d",&q);
    for(i=p;i<q+p;i++)
    {
        scanf("%d",&array[i]);
    }

    for(j=1;j<=n;j++)
    {
        for(i=0;i<p+q;i++)
        {
            if(array[i]==j)
            {
                count++;
                break;
            }
        }
    }
    if(count==n) printf("I become the guy.");
    else printf("Oh, my keyboard!");
    return 0;
}


#include<stdio.h>
int main()
{
    int i,j,count=0,a[1001],n,n1,n2,p;
    scanf("%d",&n);
    scanf("%d",&n1);
    for(i=0;i<n1;i++)
    {
        scanf("%d",&a[i]);

    }

    scanf("%d",&n2);
    for( ;i<n1+n2;i++)
    {
        scanf("%d",&a[i]);
    }
    a[i]='\0';

    for(i=1;i<=n;i++)
    {
        for(j=0;j<n1+n2;j++)
        {
            if(i==a[j])
            {
                count++;
                break;
            }
        }
    }
    if(count==n) printf("I become the guy.");
    else printf("Oh, my keyboard!");
    return 0;
}


#include<stdio.h>
int main()
{
    int n,i,a,p,q,j,count=0;
    scanf("%d",&n);
    int array[n];
    scanf("%d",&p);
    for(i=0;i<p;i++)
    {
        scanf("%d",&array[i]);
    }
    scanf("%d",&q);
    for(i=p;i<q+p;i++)
    {
        scanf("%d",&array[i]);
    }

    for(j=1;j<=n;j++)
    {
        for(i=0;i<p+q;i++)
        {
            if(array[i]==j)
            {
                count++;
                break;
            }
        }
    }
    if(count==n) printf("I become the guy.");
    else printf("Oh, my keyboard!");
    return 0;
}


#include<stdio.h>
int main()
{
    int n,n1,n2,i,j,count=0,t;
    scanf("%d",&n);
    scanf("%d",&n1);
    int a[1000];
    for(i=0;i<n1;i++)
    {
        scanf("%d",&a[i]);
    }
    scanf("%d",&n2);
    for(i=0;i<n2;i++)
    {
        scanf("%d",&a[n1+i]);
    }

    for(i=1;i<n;i++)
    {
       for(j=0;j<n1+n2;j++)
       {
           if(i==a[j]) {count++;break;}
       }
    }
    if(count+1==n) printf("I become the guy.");
        else printf("Oh, my keyboard!");
    return 0;
}


#include<stdio.h>
int main()
{
    int n,i,a,p,q,j,count=0;
    scanf("%d",&n);
    int array[1001];
    scanf("%d",&p);
    for(i=0;i<p;i++)
    {
        scanf("%d",&array[i]);
    }
    scanf("%d",&q);
    for(i=p;i<q+p;i++)
    {
        scanf("%d",&array[i]);
    }

    for(j=1;j<=n;j++)
    {
        for(i=0;i<p+q;i++)
        {
            if(array[i]==j)
            {
                count++;
                break;
            }
        }
    }
    if(count==n) printf("I become the guy.");
    else printf("Oh, my keyboard!");
    return 0;
}


#include<stdio.h>
int main()
{
    int n,i,array[100],a,p,q,j,count=0;
    scanf("%d",&n);
    scanf("%d",&p);
    for(i=0;i<p;i++)
    {
        scanf("%d",&array[i]);
    }
    scanf("%d",&q);
    for(i=p;i<q+p;i++)
    {
        scanf("%d",&array[i]);
    }

    for(j=1;j<=n;j++)
    {
        for(i=0;i<p+q;i++)
        {
            if(array[i]==j)
            {
                count++;
                break;
            }
        }
    }
    if(count==n) printf("I become the guy.");
    else printf("Oh, my keyboard!");
    return 0;
}


#include<stdio.h>
#include <stdlib.h>
#include<string.h>
int main ()
{
    int a,x=0,i,j;
    scanf("%d",&a);
    int ara[a],arb[a];
    for(i=0;i<a;i++) scanf("%d",&ara[i]);
    for(i=0;i<a-1;i++) scanf("%d",&arb[i]);
    for(i=1,j=1;i<a,j<a-1;i++,j++)
    {
        if(ara[i]==a||arb[j]==a) x=1;
    }
    if(x==1) printf("I become the guy.");
    else printf("Oh, my keyboard!");
}


#include<stdio.h>
#include <stdlib.h>
#include<string.h>
int main ()
{
    int a,x=1,i,j,n1,n2;
    scanf("%d",&a);
    int lara[a];
    scanf("%d",&n1);
    int ara[n1];
    for(i=0;i<n1;i++) {scanf("%d",&ara[i]); lara[ara[i]]=-1;}
    scanf("%d",&n2);
    int arb[n2];
    for(i=0;i<n2;i++) {scanf("%d",&arb[i]); lara[arb[i]]=-1;}
    for(i=1;i<a;i++)
    {
        if(lara[i]!=-1) x=0;
    }

    if(x==1) printf("I become the guy.");
    else printf("Oh, my keyboard!");
}


#include<stdio.h>
#include <stdlib.h>
#include<string.h>
int main ()
{
    int a,x=0,i,j,n1,n2;
    scanf("%d",&a);
    scanf("%d",&n1);
    int ara[n1];
    for(i=0;i<n1;i++) scanf("%d",&ara[i]);
    scanf("%d",&n2);
    int arb[n2];
    for(i=0;i<n2;i++) scanf("%d",&arb[i]);

    for(i=0,j=0;i<n1,j<n2;i++,j++) {if(ara[i]==a||ara[j]==a) x=1;}
    if(x==1) printf("I become the guy.");
    else printf("Oh, my keyboard!");
}


#include<stdio.h>
#include <stdlib.h>
#include<string.h>
int main ()
{
    int a,x=0,i,j,n1,n2;
    scanf("%d",&a);
    scanf("%d",&n1);
    int ara[n1];
    for(i=0;i<n1;i++) scanf("%d",&ara[i]);
    scanf("%d",&n2);
    int arb[n2];
    for(i=0;i<n2;i++) scanf("%d",&arb[i]);

    for(i=0;i<n1;i++) {if(ara[i]==a) x=1;}
    for(i=0;i<n2;i++) {if(arb[i]==a) x=1;}

    if(x==1) printf("I become the guy.");
    else printf("Oh, my keyboard!");
}


#include<stdio.h>
#include <stdlib.h>
#include<string.h>
int main ()
{
    int a,x=1,i,j,n1,n2;
    scanf("%d",&a);
    int lara[a];
    scanf("%d",&n1);
    int ara[n1+1];
    for(i=0;i<n1;i++) {scanf("%d",&ara[i]); lara[ara[i]]=-1;}
    scanf("%d",&n2);
    int arb[n2];
    for(i=0;i<n2;i++) {scanf("%d",&arb[i]); lara[arb[i]]=-1;}
    for(i=1;i<=a;i++)
    {
        if(lara[i]!=-1) x=0;
    }

    if(x==1) printf("I become the guy.");
    else printf("Oh, my keyboard!");
}


#include<stdio.h>
#include <stdlib.h>
#include<string.h>
int main ()
{
    int a,x=1,i,j,n1,n2;
    scanf("%d",&a);
    int lara[a]={0};
    scanf("%d",&n1);
    int ara[n1];
    for(i=0;i<n1;i++) {scanf("%d",&ara[i]); lara[ara[i]]=-1;}
    scanf("%d",&n2);
    int arb[n2];
    for(i=0;i<n2;i++) {scanf("%d",&arb[i]); lara[arb[i]]=-1;}
    for(i=1;i<a;i++)
    {
        if(lara[i]==0) x=0;
    }

    if(x==1) printf("I become the guy.");
    else printf("Oh, my keyboard!");
}


#include<stdio.h>
#include <stdlib.h>
#include<string.h>
int main ()
{
    int a,x=0,i,j,n1,n2;
    scanf("%d",&a);
    scanf("%d",&n1);
    int ara[n1];
    for(i=0;i<n1;i++) scanf("%d",&ara[i]);
    scanf("%d",&n2);
    int arb[n2];
    for(i=0;i<n2;i++) scanf("%d",&arb[i]);

    for(i=0,j=0;i<n1,j<n2;i++,j++) {if(ara[i]==a||arb[j]==a) x=1;}

    if(x==1) printf("I become the guy.");
    else printf("Oh, my keyboard!");
}


#include<stdio.h>
int part(int x[],int k,int beg,int end);
void quick_sort(int a[],int n,int beg,int end);
int main()
{ int a[100],b[100],x[100],d[100],i,j,k=0,n,p,q,z=0,t,m=0;
 scanf("%d",&n);
 scanf("%d",&p);
 for(i=0;i<p;i++)
  { scanf("%d",&a[i]);
  }
  scanf("%d",&q);
  for(i=0;i<q;i++)
  { scanf("%d",&b[i]);
  }
  for(i=0;i<p;i++)
   { x[k]=a[i];
     k++;
   }
   for(i=0;i<q;i++)
    { x[k]=b[i];
      k++;
	}
	quick_sort(x,k,0,k-1);

 /*	for(i=0;i<k;i++)
	 { for(j=0;j<k-1;j++)
	    { if(x[j]>x[j+1])
	       { t=x[j];
	         x[j]=x[j+1];
	         x[j+1]=t;
		   }
		}
	 }*/
	i=0;
	j=0;
	while(i<k)
	 { j=i+1;
	   while(j<k)
	    { if(x[i]==x[j])
	      { j++;
		  }
		  else
		  break;
		}
		d[z]=x[i];
		z++;
		i=j;
	 }
	 for(i=0;i<z;i++)
	  { m++;
	  }
	  if(m==n)
	   printf("I become the guy.");
	  else
	   printf("Oh, my keyboard!");
	return 0;
}
void quick_sort(int x[],int k,int beg,int end)
 { int loc;
    if(beg<end)
    { loc=part(x,k,beg,end);
      quick_sort(x,k,beg,loc-1);
      quick_sort(x,k,loc+1,end);
	}
 }

int part(int x[],int k,int beg,int end)
{ int left,right,loc,t,flag=0;
  left=beg;
  right=end;
  loc=beg;
  while(flag!=1)
  { while(x[loc]<=x[right]&&(loc!=right))
     right--;

  if(loc==right)
   flag=1;
   else if(x[loc]>=x[right]&&(loc!=right))
          { t=x[loc];
            x[loc]=x[right];
            x[right]=t;
            loc=right;
		  }

	  if(flag!=1)
	    { while(x[loc]>=x[left]&&(loc!=left))
	        left++;
		}
	    if(loc==left)
	       flag=1;
	       else if(x[loc]<=x[left]&&(loc!=left))
	               { t=x[loc];
	                x[loc]=x[left];
	                x[left]=t;
	                loc=right;
				   }
			}
	return loc;
}


#include<stdio.h>
int main()
{
int m,i,j,n, p,q,flag = 0;
scanf("%d",&n);
scanf("%d",&p);
int x[p], merge[n];
for(i=0; i<p; i++)
{
scanf("%d",&x[i]);
m = x[i];
merge[m] = 1;
}
scanf("%d",&q);
int y[q];
for(i=0; i<q; i++)
{
scanf("%d",&y[i]);
m = y[i];
merge[m] = 1;
}
for(i=1;i<=n;i++)
{
if(merge[i]==1)
{
flag = 1;
}
else
{
flag = 0;
break;
}
}

if(flag==1)
printf("I become the guy.");
else
printf("Oh, my keyboard!");
return 0;
}


#include <iostream>
#include<algorithm>
using namespace std;
bool isPrime (int x)
{
    if (x==1) return false;
    for (int i=2;i*i <= x ; i++)
    {
        if (x%i==0)
            return false;
    }
    return true;
}

int main()
{
    int n,m,matrix[500][500];
    int prime1[500],prime2[500];
    cin>>n>>m;
    int sum=0;
    for (int i=0;i<500;i++)
    {
        prime1[i]=0;
        prime2[i]=0;
    }
    for (int i=0;i<n;i++)
    {
        for (int j=0;j<m;j++)
            cin>>matrix[i][j];
    }
    for (int i=0;i<n;i++)
    {
        for (int j=0;j<m;j++)
        {
            sum=0;
            if (!isPrime(matrix[i][j]))
            {
                int k=matrix[i][j];
                while (!isPrime(k))
                {
                    k++;
                    sum++;
                }

            }
            matrix[i][j]=sum;
        }
    }
    for (int i=0;i<n;i++)
    {
        for (int j=0;j<m;j++)
            prime1[i]+=matrix[i][j];
    }
    for (int j=0;j<m;j++)
    {
        for (int i=0;i<n;i++)
            prime2[j]+=matrix[i][j];
    }
    int min1=prime1[0];
    int min2=prime2[0];
    for (int i=0;i<n;i++)
    {
        if (prime1[i] < min1 )
            min1=prime1[i];
    }
    for (int i=0;i<m;i++)
    {
        if (prime2[i] < min2 )
            min2=prime2[i];
    }
    if (min1 > min2)
        cout<<min2<<endl;
    else cout<<min1<<endl;
    return 0;
}


#include <iostream>
#include<algorithm>
using namespace std;
bool isPrime (int x)
{
    if (x==1) return false;
    for (int i=2;i*i <= x ; i++)
    {
        if (x%i==0)
            return false;
    }
    return true;
}

int main()
{
    int n,m,matrix[500][500];
    int prime1[500],prime2[500];
    cin>>n>>m;
    int sum=0;
    for (int i=0;i<500;i++)
    {
        prime1[i]=0;
        prime2[i]=0;
    }
    for (int i=0;i<n;i++)
    {
        for (int j=0;j<m;j++)
            cin>>matrix[i][j];
    }
    for (int i=0;i<n;i++)
    {
        for (int j=0;j<m;j++)
        {
            sum=0;
            if (!isPrime(matrix[i][j]))
            {
                int k=matrix[i][j];
                while (!isPrime(k))
                {
                    k++;
                    sum++;
                }

            }
            matrix[i][j]=sum;
        }
    }
    for (int i=0;i<n;i++)
    {
        for (int j=0;j<m;j++)
            prime1[i]+=matrix[i][j];
    }
    for (int j=0;j<m;j++)
    {
        for (int i=0;i<m;i++)
            prime2[j]+=matrix[i][j];
    }
    int min1=prime1[0];
    int min2=prime2[0];
    for (int i=0;i<n;i++)
    {
        if (prime1[i] < min1 )
            min1=prime1[i];
    }
    for (int i=0;i<m;i++)
    {
        if (prime2[i] < min2 )
            min2=prime1[i];
    }
    if (min1 > min2)
        cout<<min2<<endl;
    else cout<<min1<<endl;
    return 0;
}


#include <iostream>
#include<algorithm>
using namespace std;
bool isPrime (int x)
{
    if (x==1) return false;
    for (int i=2;i*i <= x ; i++)
    {
        if (x%i==0)
            return false;
    }
    return true;
}

int main()
{
    int n,m,matrix[500][500];
    int prime1[500],prime2[500];
    cin>>n>>m;
    int sum=0;
    for (int i=0;i<n;i++)
    {
        for (int j=0;j<m;j++)
            cin>>matrix[i][j];
    }
    for (int i=0;i<n;i++)
    {
        for (int j=0;j<m;j++)
        {
            sum=0;
            if (!isPrime(matrix[i][j]))
            {
                int k=matrix[i][j];
                while (!isPrime(k))
                {
                    k++;
                    sum++;
                }

            }
            matrix[i][j]=sum;
        }
    }
    for (int i=0;i<n;i++)
    {
        for (int j=0;j<m;j++)
            prime1[i]+=matrix[i][j];
    }
    for (int j=0;j<m;j++)
    {
        for (int i=0;i<m;i++)
            prime2[j]+=matrix[i][j];
    }
    int min1=prime1[0];
    int min2=prime2[0];
    for (int i=0;i<n;i++)
    {
        if (prime1[i] < min1 )
            min1=prime1[i];
    }
    for (int i=0;i<m;i++)
    {
        if (prime2[i] < min2 )
            min2=prime1[i];
    }
    if (min1 > min2)
        cout<<min1;
    else cout<<min2;
    return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
#include <string>
#include <string.h>
#include <memory.h>
#include <cmath>
#include <assert.h>
#include <cstdlib>
#include <ctime>

using namespace std;

#pragma comment(linker, "/STACK:100000000")

#define ll long long
#define pb push_back
#define mp make_pair

int rows[505], cols[505];
bool resheto[100100];

int main()
{
	//freopen("input.txt","rt",stdin);
	//freopen("output.txt","wt",stdout);
	memset(resheto, true, sizeof(resheto));
	resheto[0] = resheto[1] = false;
	for(int i = 3; i < 100100; i+=2) {
		if(resheto[i]) {
			for(int j = 3 * i; j < 100100; j+=2*i) {
				resheto[j] = false;
			}
		}
	}
	int n, m;
	scanf("%d %d", &n, &m);
	for(int i = 0; i < n; i++) {
		for(int j = 0; j < m; j++) {
			int a;
			scanf("%d", &a);
			if(a==2)continue;
			int cnt = (a + 1) % 2;
			while(!resheto[a+cnt])cnt+=2;
			if(a==1)cnt=1;
			rows[i]+=cnt;
			cols[j]+=cnt;
		}
	}
	int res = 1 << 30;
	for(int i = 0; i < n; i++) res = min(res, rows[i]);
	for(int i = 0; i < m; i++) res = min(res, cols[i]);
	//cout << rows[0] << " " << rows[1] << " " << rows[2] << endl;
	//cout << cols[0] << " " << cols[1] << " " << cols[2] << endl;
	printf("%d\n", res);
	return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
#include <string>
#include <string.h>
#include <memory.h>
#include <cmath>
#include <assert.h>
#include <cstdlib>
#include <ctime>

using namespace std;

#pragma comment(linker, "/STACK:100000000")

#define ll long long
#define pb push_back
#define mp make_pair

int rows[505], cols[505];
bool resheto[100100];

int main()
{
	//freopen("input.txt","rt",stdin);
	//freopen("output.txt","wt",stdout);
	memset(resheto, true, sizeof(resheto));
	resheto[0] = resheto[1] = false;
	for(int i = 3; i < 100100; i+=2) {
		if(resheto[i]) {
			for(int j = 3 * i; j < 100100; j+=2*i) {
				resheto[j] = false;
			}
		}
	}
	int n, m;
	scanf("%d %d", &n, &m);
	for(int i = 0; i < n; i++) {
		for(int j = 0; j < m; j++) {
			int a;
			scanf("%d", &a);
			if(a==2)continue;
			int cnt = a % 2;
			while(!resheto[a+cnt])cnt+=2;
			rows[i]+=cnt;
			cols[j]+=cnt;
		}
	}
	int res = 1 << 30;
	for(int i = 0; i < n; i++) res = min(res, rows[i]);
	for(int i = 0; i < m; i++) res = min(res, cols[i]);
	printf("%d\n", res);
	return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
#include <string>
#include <string.h>
#include <memory.h>
#include <cmath>
#include <assert.h>
#include <cstdlib>
#include <ctime>

using namespace std;

#pragma comment(linker, "/STACK:100000000")

#define ll long long
#define pb push_back
#define mp make_pair

int rows[505], cols[505];
bool resheto[100100];

int main()
{
	//freopen("input.txt","rt",stdin);
	//freopen("output.txt","wt",stdout);
	memset(resheto, true, sizeof(resheto));
	resheto[0] = resheto[1] = false;
	for(int i = 3; i < 100100; i+=2) {
		if(resheto[i]) {
			for(int j = 3 * i; j < 100100; j+=2*i) {
				resheto[j] = false;
			}
		}
	}
	int n, m;
	scanf("%d %d", &n, &m);
	for(int i = 0; i < n; i++) {
		for(int j = 0; j < m; j++) {
			int a;
			scanf("%d", &a);
			if(a==2)continue;
			int cnt = a^1;
			while(!resheto[a+cnt])cnt+=2;
			if(a==1)cnt=1;
			rows[i]+=cnt;
			cols[j]+=cnt;
		}
	}
	int res = 1 << 30;
	for(int i = 0; i < n; i++) res = min(res, rows[i]);
	for(int i = 0; i < m; i++) res = min(res, cols[i]);
	//cout << rows[0] << " " << rows[1] << " " << rows[2] << endl;
	//cout << cols[0] << " " << cols[1] << " " << cols[2] << endl;
	printf("%d\n", res);
	return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
#include <string>
#include <string.h>
#include <memory.h>
#include <cmath>
#include <assert.h>
#include <cstdlib>
#include <ctime>

using namespace std;

#pragma comment(linker, "/STACK:100000000")

#define ll long long
#define pb push_back
#define mp make_pair

int rows[505], cols[505];
bool resheto[100100];

int main()
{
	//freopen("input.txt","rt",stdin);
	//freopen("output.txt","wt",stdout);
	memset(resheto, true, sizeof(resheto));
	resheto[0] = resheto[1] = false;
	for(int i = 3; i < 100100; i+=2) {
		if(resheto[i]) {
			for(int j = 3 * i; j < 100100; j+=2*i) {
				resheto[j] = false;
			}
		}
	}
	int n, m;
	scanf("%d %d", &n, &m);
	for(int i = 0; i < n; i++) {
		for(int j = 0; j < m; j++) {
			int a;
			scanf("%d", &a);
			if(a==2)continue;
			int cnt = (a + 1) % 2;
			while(!resheto[a+cnt])cnt+=2;
			rows[i]+=cnt;
			cols[j]+=cnt;
		}
	}
	int res = 1 << 30;
	for(int i = 0; i < n; i++) res = min(res, rows[i]);
	for(int i = 0; i < m; i++) res = min(res, cols[i]);
	//cout << rows[0] << " " << rows[1] << " " << rows[2] << endl;
	//cout << cols[0] << " " << cols[1] << " " << cols[2] << endl;
	printf("%d\n", res);
	return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
#include <string>
#include <string.h>
#include <memory.h>
#include <cmath>
#include <assert.h>
#include <cstdlib>
#include <ctime>

using namespace std;

#pragma comment(linker, "/STACK:100000000")

#define ll long long
#define pb push_back
#define mp make_pair

int rows[505], cols[505];
bool resheto[100100];

int main()
{
	//freopen("input.txt","rt",stdin);
	//freopen("output.txt","wt",stdout);
	memset(resheto, true, sizeof(resheto));
	resheto[0] = resheto[1] = false;
	for(int i = 2; i < 100100; i++) {
		if(resheto[i]) {
			for(int j = 2 * i; j < 100100; j+=i) {
				resheto[j] = false;
			}
		}
	}
	int n, m;
	scanf("%d %d", &n, &m);
	for(int i = 0; i < n; i++) {
		for(int j = 0; j < m; j++) {
			int a;
			scanf("%d", &a);
			int cnt = 0;
			while(!resheto[a+cnt])cnt++;
			rows[i]+=cnt;
			cols[j]+=cnt;
		}
	}
	int res = 1 << 30;
	for(int i = 0; i < n; i++) res = min(res, rows[i]);
	for(int i = 0; i < m; i++) res = min(res, cols[i]);
	printf("%d\n", res);
	return 0;
}


#include <iostream>
#include <string>
#include <vector>

using namespace std;

/*bool prime(int x){
    bool b=true;
    int i=2;
    if(x==1) b=false;
    while((i<x)&&(b))
    {
        if(x % i ==0)
            b=false;
        i++;
    }
    if(b) return true;
    else
        return false;
}*/
bool prime (int num)
{
    if (num <=1)
        return false;
    else if (num == 2)
        return true;
    else if (num % 2 == 0)
        return false;
    else
    {
        bool prime = true;
        int divisor = 3;
        double num_d = static_cast<double>(num);
        int upperLimit = static_cast<int>(sqrt(num_d) +1);

        while (divisor <= upperLimit)
        {
            if (num % divisor == 0)
                prime = false;
            divisor +=2;
        }
        return prime;
    }
}
int get(int x){
    int i=x;
    bool b=true;
    while(b)
    {
        i++;
        if(prime(i))
            b=false;
    }
    return i;
}
int main(){
    int* a[500];int* t[500];
    int n,m;

    cin>>n;
    cin>>m;

    for (int i = 0; i < n; ++i) {
        a[i] = new int[m];
        t[i] = new int[m];
    }
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
        {
            cin>>a[i][j];
            if(prime(a[i][j]))
                t[i][j]=0;
            else
                t[i][j]=get(a[i][j])-a[i][j];
        }

    int i=0,j=0,sum=0;;
    vector<int> r;

    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
            sum+=t[i][j];
        r.push_back(sum);
        sum=0;
    }

    for(int i=0;i<m;i++)
    {
        for(int j=0;j<n;j++)
            sum+=t[j][i];
        r.push_back(sum);
        sum=0;
    }
    int min=1000;
    for(int i=0;i<r.size();i++)
        if(r.at(i)<min)
            min=r.at(i);
    cout<<min<<"\n";

}


#include <iostream>
#include <string>
#include <vector>

using namespace std;

bool prime(int x){
    bool b=true;
    int i=2;
    if(x==1) b=false;
    while((i<x)&&(b))
    {
        if(x % i ==0)
            b=false;
        i++;
    }
    if(b) return true;
    else
        return false;
}
/*bool prime (int num)
{
    if (num <=1)
        return false;
    else if (num == 2)
        return true;
    else if (num % 2 == 0)
        return false;
    else
    {
        bool prime = true;
        int divisor = 3;
        double num_d = static_cast<double>(num);
        int upperLimit = static_cast<int>(sqrt(num_d) +1);

        while (divisor <= upperLimit)
        {
            if (num % divisor == 0)
                prime = false;
            divisor +=2;
        }
        return prime;
    }
}*/
int get(int x){
    int i=x;
    bool b=true;
    while(b)
    {
        i++;
        if(prime(i))
            b=false;
    }
    return i;
}
int main(){
    int* a[500];int* t[500];
    int n,m;

    cin>>n;
    cin>>m;

    for (int i = 0; i < n; ++i) {
        a[i] = new int[m];
        t[i] = new int[m];
    }
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
        {
            cin>>a[i][j];
            if(prime(a[i][j]))
                t[i][j]=0;
            else
                t[i][j]=get(a[i][j])-a[i][j];
        }

    int i=0,j=0,sum=0;;
    vector<int> r;

    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
            sum+=t[i][j];
        r.push_back(sum);
        sum=0;
    }

    for(int i=0;i<m;i++)
    {
        for(int j=0;j<n;j++)
            sum+=t[j][i];
        r.push_back(sum);
        sum=0;
    }
    int min=1000;
    for(int i=0;i<r.size();i++)
        if(r.at(i)<min)
            min=r.at(i);
    cout<<min<<"\n";

}


#include <iostream>
#include <string>
#include <vector>

using namespace std;

/*bool prime(int x){
    bool b=true;
    int i=2;
    if(x==1) b=false;
    while((i<x)&&(b))
    {
        if(x % i ==0)
            b=false;
        i++;
    }
    if(b) return true;
    else
        return false;
}*/
bool prime (int num)
{
    if (num <=1)
        return false;
    else if (num == 2)
        return true;
    else if (num % 2 == 0)
        return false;
    else
    {
        bool prime = true;
        int divisor = 3;
        double num_d = static_cast<double>(num);
        int upperLimit = static_cast<int>(sqrt(num_d) +1);

        while (divisor <= upperLimit)
        {
            if (num % divisor == 0)
                prime = false;
            divisor +=2;
        }
        return prime;
    }
}
int get(int x){
    int i=x;
    bool b=true;
    while(b)
    {
        i++;
        if(prime(i))
            b=false;
    }
    return i;
}
int main(){
    int a[100][100],t[100][100];
    int n,m;

    cin>>n;
    cin>>m;
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
        {
            cin>>a[i][j];
            if(prime(a[i][j]))
                t[i][j]=0;
            else
                t[i][j]=get(a[i][j])-a[i][j];
        }

    int i=0,j=0,sum=0;;
    vector<int> r;

    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
            sum+=t[i][j];
        r.push_back(sum);
        sum=0;
    }

    for(int i=0;i<m;i++)
    {
        for(int j=0;j<n;j++)
            sum+=t[j][i];
        r.push_back(sum);
        sum=0;
    }
    int min=1000;
    for(int i=0;i<r.size();i++)
        if(r.at(i)<min)
            min=r.at(i);
    cout<<min<<"\n";

}


#include <iostream>
#include <string>
#include <vector>

using namespace std;
bool prime(int x)
{
    if (x == 0)
        return false;
    if (x == 1)
        return false;
    if (x <= 3)
        return true;
    if (x % 2 == 0)
        return false;
    for (int i = 3; i*i <= x + 1; i += 2)
        if (x %i == 0)
            return false;
    return true;
}
/*bool prime (int num)
{
    if (num <=1)
        return false;
    else if (num == 2)
        return true;
    else if (num % 2 == 0)
        return false;
    else
    {
        bool prime = true;
        int divisor = 3;
        double num_d = static_cast<double>(num);
        int upperLimit = static_cast<int>(sqrt(num_d) +1);

        while (divisor <= upperLimit)
        {
            if (num % divisor == 0)
                prime = false;
            divisor +=2;
        }
        return prime;
    }
}*/
int get(int x){
    int i=x;
    bool b=true;
    while(b)
    {
        i++;
        if(prime(i))
            b=false;
    }
    return i;
}
int main(){
    int* a[501];int* t[501];
    int n,m;

    cin>>n;
    cin>>m;

    for(int i=0;i<n;i++)
    {
        a[i] = new int[m];
        t[i] = new int[m];
        for(int j=0;j<m;j++)
        {
            cin>>a[i][j];
            if(prime(a[i][j]))
                t[i][j]=0;
            else
                t[i][j]=get(a[i][j])-a[i][j];
        }
    }

    int i=0,j=0,sum=0;;
    vector<int> r;

    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
            sum+=t[i][j];
        r.push_back(sum);
        sum=0;
    }

    for(int i=0;i<m;i++)
    {
        for(int j=0;j<n;j++)
            sum+=t[j][i];
        r.push_back(sum);
        sum=0;
    }
    int min=10000;
    for(int i=0;i<r.size();i++)
        if(r.at(i)<min)
            min=r.at(i);
    cout<<min<<"\n";

}


#include <iostream>
#include <string>
#include <vector>

using namespace std;

bool prime(int x){
    bool b=true;
    int i=2;
    if(x==1) b=false;
    while((i<x)&&(b))
    {
        if(x % i ==0)
            b=false;
        i++;
    }
    if(b) return true;
    else
        return false;
}
int get(int x){
    int i=x;
    bool b=true;
    while(b)
    {
        i++;
        if(prime(i))
            b=false;
    }
    return i;
}
int main(){
    int a[100][100],t[100][100];
    int n,m;

    cin>>n;
    cin>>m;
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
        {
            cin>>a[i][j];
            if(prime(a[i][j]))
                t[i][j]=0;
            else
                t[i][j]=get(a[i][j])-a[i][j];
        }

    int i=0,j=0,sum=0;;
    vector<int> r;

    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
            sum+=t[i][j];
        r.push_back(sum);
        sum=0;
    }

    for(int i=0;i<m;i++)
    {
        for(int j=0;j<n;j++)
            sum+=t[j][i];
        r.push_back(sum);
        sum=0;
    }
    int min=1000;
    for(int i=0;i<r.size();i++)
        if(r.at(i)<min)
            min=r.at(i);
    cout<<min<<"\n";

}


#include <iostream>
#include <string>
#include <vector>

using namespace std;

bool prime (int num)
{
    if (num <=1)
        return false;
    else if (num == 2)
        return true;
    else if (num % 2 == 0)
        return false;
    else
    {
        bool prime = true;
        int divisor = 3;
        double num_d = static_cast<double>(num);
        int upperLimit = static_cast<int>(sqrt(num_d) +1);

        while (divisor <= upperLimit)
        {
            if (num % divisor == 0)
                prime = false;
            divisor +=2;
        }
        return prime;
    }
}
int get(int x){
    int i=x;
    bool b=true;
    while(b)
    {
        i++;
        if(prime(i))
            b=false;
    }
    return i;
}
int main(){
    int* a[500];int* t[500];
    int n,m;

    cin>>n;
    cin>>m;

    for (int i = 0; i < n; ++i) {
        a[i] = new int[m];
        t[i] = new int[m];
    }
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
        {
            cin>>a[i][j];
            if(prime(a[i][j]))
                t[i][j]=0;
            else
                t[i][j]=get(a[i][j])-a[i][j];
        }

    int i=0,j=0,sum=0;;
    vector<int> r;

    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
            sum+=t[i][j];
        r.push_back(sum);
        sum=0;
    }

    for(int i=0;i<m;i++)
    {
        for(int j=0;j<n;j++)
            sum+=t[j][i];
        r.push_back(sum);
        sum=0;
    }
    int min=10000;
    for(int i=0;i<r.size();i++)
        if(r.at(i)<min)
            min=r.at(i);
    cout<<min<<"\n";

}


#include <iostream>
#include <string>
#include <vector>

using namespace std;
bool prime(int x)
{
    if (x == 0)
        return false;
    if (x == 1)
        return false;
    if (x <= 3)
        return true;
    if (x % 2 == 0)
        return false;
    for (int i = 3; i*i <= x + 1; i += 2)
        if (x %i == 0)
            return false;
    return true;
}
/*bool prime (int num)
{
    if (num <=1)
        return false;
    else if (num == 2)
        return true;
    else if (num % 2 == 0)
        return false;
    else
    {
        bool prime = true;
        int divisor = 3;
        double num_d = static_cast<double>(num);
        int upperLimit = static_cast<int>(sqrt(num_d) +1);

        while (divisor <= upperLimit)
        {
            if (num % divisor == 0)
                prime = false;
            divisor +=2;
        }
        return prime;
    }
}*/
int get(int x){
    int i=x;
    bool b=true;
    while(b)
    {
        i++;
        if(prime(i))
            b=false;
    }
    return i;
}
int main(){
    int* a[500];int* t[500];
    int n,m;

    cin>>n;
    cin>>m;

    for(int i=0;i<n;i++)
    {
        a[i] = new int[m];
        t[i] = new int[m];
        for(int j=0;j<m;j++)
        {
            cin>>a[i][j];
            if(prime(a[i][j]))
                t[i][j]=0;
            else
                t[i][j]=get(a[i][j])-a[i][j];
        }
    }

    int i=0,j=0,sum=0;;
    vector<int> r;

    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
            sum+=t[i][j];
        r.push_back(sum);
        sum=0;
    }

    for(int i=0;i<m;i++)
    {
        for(int j=0;j<n;j++)
            sum+=t[j][i];
        r.push_back(sum);
        sum=0;
    }
    int min=10000;
    for(int i=0;i<r.size();i++)
        if(r.at(i)<min)
            min=r.at(i);
    cout<<min<<"\n";

}


#include <iostream>
#include <string>
#include <vector>

using namespace std;
bool prime(int x)
{
    if (x == 0)
        return false;
    if (x == 1)
        return false;
    if (x <= 3)
        return true;
    if (x % 2 == 0)
        return false;
    for (int i = 3; i*i <= x + 1; i += 2)
        if (x %i == 0)
            return false;
    return true;
}
/*bool prime (int num)
{
    if (num <=1)
        return false;
    else if (num == 2)
        return true;
    else if (num % 2 == 0)
        return false;
    else
    {
        bool prime = true;
        int divisor = 3;
        double num_d = static_cast<double>(num);
        int upperLimit = static_cast<int>(sqrt(num_d) +1);

        while (divisor <= upperLimit)
        {
            if (num % divisor == 0)
                prime = false;
            divisor +=2;
        }
        return prime;
    }
}*/
int get(int x){
    int i=x;
    bool b=true;
    while(b)
    {
        i++;
        if(prime(i))
            b=false;
    }
    return i;
}
int main(){
    int* a[501];int* t[501];
    int n,m;

    cin>>n;
    cin>>m;

    for(int i=0;i<n;i++)
    {
        a[i] = new int[m];
        t[i] = new int[m];
        for(int j=0;j<m;j++)
        {
            cin>>a[i][j];
            if(prime(a[i][j]))
                t[i][j]=0;
            else
                t[i][j]=get(a[i][j])-a[i][j];
        }
    }

    int i=0,j=0,sum=0;;
    vector<int> r;

    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
            sum+=t[i][j];
        r.push_back(sum);
        sum=0;
    }

    for(int i=0;i<m;i++)
    {
        for(int j=0;j<n;j++)
            sum+=t[j][i];
        r.push_back(sum);
        sum=0;
    }
    int min=1000000;
    for(int i=0;i<r.size();i++)
        if(r.at(i)<min)
            min=r.at(i);
    cout<<min<<"\n";

}


#include <iostream>
#include <algorithm>
#include <set>

using namespace std;

const int MAXN = 1000 * 1000;

bool is_prime( int n ) {
    for( int i = 2; i <= sqrt( n + 0.0 ); ++i )
        if( n % i == 0 )
            return false;
    return true;
}

int main() {
    set< int > primes;

    int a[2][ 500 ];
    memset( a, 0, sizeof( a ) );

    for( int i = 2; i < MAXN; ++i )
        if( is_prime( i ) )
            primes.insert( primes.end(), i );

    int m, n;
    cin>>m>>n;

    for( int i = 0; i < m; ++i )
        for( int j = 0; j < n; ++j ) {
            int b;
            cin>>b;
            int k = *(primes.lower_bound( b ));
            k -= b;
            a[ 0 ][ i ] += k;
            a[ 1 ][ j ] += k;
        }

    int min1 = *( min_element( a[ 0 ], a[ 0 ] + m ) );
    int min2 = *( min_element( a[ 1 ], a[ 1 ] + n ) );

    cout<< min( min1, min2 ) <<endl;

    return 0;
}


#include <iostream>
#include <algorithm>
#include <set>

using namespace std;

const int MAXN = 1000 * 100;

bool is_prime( int n ) {
    for( int i = 2; i <= sqrt( n + 0.0 ); ++i )
        if( n % i == 0 )
            return false;
    return true;
}

int main() {
    set< int > primes;

    int a[2][ 500 ];
    memset( a, 0, sizeof( a ) );

    for( int i = 2; i < MAXN; ++i )
        if( is_prime( i ) )
            primes.insert( primes.end(), i );

    int m, n;
    cin>>m>>n;

    for( int i = 0; i < m; ++i )
        for( int j = 0; j < n; ++j ) {
            int b;
            cin>>b;
            int k = *(primes.lower_bound( b ));
            k -= b;
            a[ 0 ][ i ] += k;
            a[ 1 ][ j ] += k;
        }

    int min1 = *( min_element( a[ 0 ], a[ 0 ] + m ) );
    int min2 = *( min_element( a[ 1 ], a[ 1 ] + n ) );

    cout<< min( min1, min2 ) <<endl;

    return 0;
}


#include <iostream>
#include <algorithm>
#include <set>

using namespace std;

const int MAXN = 1000 * 100;

bool is_prime( int n ) {
    for( int i = 2; i <= sqrt( n + 0.0 ); ++i )
        if( n % i == 0 )
            return false;
    return true;
}

int main() {
    set< int > primes;

    int a[2][ 500 ];
    memset( a, 0, sizeof( a ) );

    for( int i = 2; i < MAXN; ++i )
        if( is_prime( i ) )
            primes.insert( primes.end(), i );

    int m, n;
    cin>>m>>n;

    for( int i = 0; i < m; ++i )
        for( int j = 0; j < n; ++j ) {
            int b;
            cin>>b;
            int k = *(primes.lower_bound( b ));
            k -= b;
            a[ 0 ][ i ] += k;
            a[ 1 ][ j ] += k;
        }

    int min1 = *( min_element( a[ 0 ], a[ 0 ] + n ) );
    int min2 = *( min_element( a[ 1 ], a[ 1 ] + m ) );

    cout<< min( min1, min2 ) <<endl;

    return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int c[501][501];
int p[10000];
bool prime(int n)
{
    if (n<2)
        return false;
    for (int i = 2; i*i <= n; i++)
    {
        if (n % i == 0)
            return false;
    }
    return true;
}

int main()
{
    int len = 0;
    int res = 999999999;
    for (int i = 2; i < 100000; i++)
    {
        if (prime(i))
            p[len++] = i;
    }
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; i++)
    {
        int sum = 0;
        for (int j = 0; j < m; j++)
        {
            int x;
            cin >> x;
            //c[i][j] = x;
            int k = 0;
            for (; k < len; k++)
            if (p[k] >= x) break;

            c[i][j] = p[k] - x;
            sum += c[i][j];
        }
        res = min(res, sum);
    }

    for (int i = 0; i < m; i++)
    {
        int sum = 0;
        for (int j = 0; j < n && sum <res; j++)
        {
            sum += c[j][i];
        }
        res = min(res, sum);
    }
    cout << res;
    cin >> n;

}


#include<iostream>
#include<algorithm>
using namespace std;
int c[501][501];
int p[10000];
bool prime(int n)
{
    if (n<2)
        return false;
    for (int i = 2; i*i <= n; i++)
    {
        if (n % i == 0)
            return false;
    }
    return true;
}

int main()
{
    int len = 0;
    int res = 999999999;
    for (int i = 2; i < 100050; i++)
    {
        if (prime(i))
            p[len++] = i;
    }
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; i++)
    {
        int sum = 0;
        for (int j = 0; j < m; j++)
        {
            int x;
            cin >> x;
            //c[i][j] = x;
            int k = x/15;
            for (; k < len; k++)
            if (p[k] >= x) break;

            c[i][j] = p[k] - x;
            sum += c[i][j];
        }
        res = min(res, sum);
    }

    for (int i = 0; i < m; i++)
    {
        int sum = 0;
        for (int j = 0; j < n && sum <res; j++)
        {
            sum += c[j][i];
        }
        res = min(res, sum);
    }
    cout << res;
    cin >> n;

}


#include<iostream>
#include<algorithm>
using namespace std;
int c[501][501];
bool prime(int n)
{
    if (n<2)
        return false;
    for (int i = 2; i*i <= n; i++)
    {
        if (n % i == 0)
            return false;
    }
    return true;
}

int main()
{
    int n, m;
    int res = 99999999;
    cin >> n >> m;
    for (int i = 0; i < n; i++)
    {
        int sum = 0;
        for (int j = 0; j < m; j++)
        {
            int x;
            cin >> x;
            c[i][j] = x;
            while (!prime(x)) x++;

            c[i][j] = x - c[i][j];
            sum += c[i][j];
        }
        res = min(res, sum);
    }

    /*for (int i = 0; i < n; i++)
    {
        int sum = 0;
        for (int j = 0; j < m && sum <res; j++)
        {
            sum += c[i][j];
        }
        res = min(res, sum);
    }*/
    for (int i = 0; i < m; i++)
    {
        int sum = 0;
        for (int j = 0; j < n && sum <res; j++)
        {
            sum += c[j][i];
        }
        res = min(res, sum);
    }
    cout << res;
    cin >> n;

}


#include<iostream>
#include<algorithm>
using namespace std;
int c[501][501];
int p[10000];
bool prime(int n)
{
    if (n<2)
        return false;
    for (int i = 2; i*i <= n; i++)
    {
        if (n % i == 0)
            return false;
    }
    return true;
}

int main()
{
    int len = 0;
    int res = 999999999;
    for (int i = 2; i < 100000; i++)
    {
        if (prime(i))
            p[len++] = i;
    }
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; i++)
    {
        int sum = 0;
        for (int j = 0; j < m; j++)
        {
            int x;
            cin >> x;
            //c[i][j] = x;
            int k = 0;
            for (; k < len; k++)
            if (p[k] >= x) break;

            c[i][j] = p[k] - x;
            sum += c[i][j];
        }
        res = min(res, sum);
    }

    /*for (int i = 0; i < n; i++)
    {
        int sum = 0;
        for (int j = 0; j < m && sum <res ; j++)
        {
            sum += c[i][j];
        }
        res = min(res, sum);
    }*/
    for (int i = 0; i < m; i++)
    {
        int sum = 0;
        for (int j = 0; j < n && sum <res; j++)
        {
            sum += c[j][i];
        }
        res = min(res, sum);
    }
    cout << res;
    cin >> n;

}


#include<iostream>
#include<algorithm>
using namespace std;
int c[501][501];
int p[10000];
bool prime(int n)
{
    if (n<2)
        return false;
    for (int i = 2; i*i <= n; i++)
    {
        if (n % i == 0)
            return false;
    }
    return true;
}

int main()
{
    int len = 0;
    int res = 999999999;
    for (int i = 2; i < 100050; i++)
    {
        if (prime(i))
            p[len++] = i;
    }
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; i++)
    {
        int sum = 0;
        for (int j = 0; j < m; j++)
        {
            int x;
            cin >> x;
            //c[i][j] = x;
            int k = x/10;
            for (; k < len; k++)
            if (p[k] >= x) break;

            c[i][j] = p[k] - x;
            sum += c[i][j];
        }
        res = min(res, sum);
    }

    for (int i = 0; i < m; i++)
    {
        int sum = 0;
        for (int j = 0; j < n && sum <res; j++)
        {
            sum += c[j][i];
        }
        res = min(res, sum);
    }
    cout << res;
    cin >> n;

}


#include<iostream>
#include<algorithm>
using namespace std;
int c[501][501];
int p[10000];
bool prime(int n)
{
    if (n<2)
        return false;
    for (int i = 2; i*i <= n; i++)
    {
        if (n % i == 0)
            return false;
    }
    return true;
}

int main()
{
    int len = 0;
    int res = 999999999;
    for (int i = 2; i < 200000; i++)
    {
        if (prime(i))
            p[len++] = i;
    }
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; i++)
    {
        int sum = 0;
        for (int j = 0; j < m; j++)
        {
            int x;
            cin >> x;
            //c[i][j] = x;
            int k = x/10;
            for (; k < len; k++)
            if (p[k] >= x) break;

            c[i][j] = p[k] - x;
            sum += c[i][j];
        }
        res = min(res, sum);
    }

    for (int i = 0; i < m; i++)
    {
        int sum = 0;
        for (int j = 0; j < n && sum <res; j++)
        {
            sum += c[j][i];
        }
        res = min(res, sum);
    }
    cout << res;
    //cin >> n;
    return 0;

}


#include<iostream>
#include<algorithm>
using namespace std;
int c[501][501];
int p[10000];
bool prime(int n)
{
    if (n<2)
        return false;
    for (int i = 2; i*i <= n; i++)
    {
        if (n % i == 0)
            return false;
    }
    return true;
}

int main()
{
    int len = 0;
    for (int i = 0; i < 100000; i++)
    {
        if (prime(i))
            p[len++] = i;
    }
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
    {
        int x;
        cin >> x;
        //c[i][j] = x;
        int k = 0;
        for (; k < len; k++)
        if (p[k]>=x) break;

        c[i][j] = p[k] - x;
    }
    int res = 999999999;
    for (int i = 0; i < n; i++)
    {
        int sum = 0;
        for (int j = 0; j < m && sum <res ; j++)
        {
            sum += c[i][j];
        }
        res = min(res, sum);
    }
    for (int i = 0; i < m; i++)
    {
        int sum = 0;
        for (int j = 0; j < n && sum <res; j++)
        {
            sum += c[j][i];
        }
        res = min(res, sum);
    }
    cout << res;
    cin >> n;

}


#include<iostream>
#include<algorithm>
using namespace std;
int c[501][501];
bool prime(int n)
{
    if (n<2)
        return false;
    for (int i = 2; i*i <= n; i++)
    {
        if (n % i == 0)
            return false;
    }
    return true;
}

int main()
{
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
    {
        int x;
        cin >> x;
        c[i][j] = x;
        while (!prime(x)) x++;

        c[i][j] = x-c[i][j];
    }
    int res = 99999999;
    for (int i = 0; i < n; i++)
    {
        int sum = 0;
        for (int j = 0; j < m && sum <res ; j++)
        {
            sum += c[i][j];
        }
        res = min(res, sum);
    }
    for (int i = 0; i < m; i++)
    {
        int sum = 0;
        for (int j = 0; j < n && sum <res; j++)
        {
            sum += c[j][i];
        }
        res = min(res, sum);
    }
    cout << res;
    cin >> n;

}


#include<iostream>
#include<algorithm>
using namespace std;
int c[501][501];
int p[10000];
bool prime(int n)
{
    if (n<2)
        return false;
    for (int i = 2; i*i <= n; i++)
    {
        if (n % i == 0)
            return false;
    }
    return true;
}

int main()
{
    int len = 0;
    int res = 999999999;
    for (int i = 2; i < 200000; i++)
    {
        if (prime(i))
            p[len++] = i;
    }
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; i++)
    {
        int sum = 0;
        for (int j = 0; j < m; j++)
        {
            int x;
            cin >> x;
            //c[i][j] = x;
            int k = x/10;
            for (; k < len; k++)
            if (p[k] >= x) break;

            c[i][j] = p[k] - x;
            sum += c[i][j];
        }
        res = min(res, sum);
    }

    for (int i = 0; i < m; i++)
    {
        int sum = 0;
        for (int j = 0; j < n && sum <res; j++)
        {
            sum += c[j][i];
        }
        res = min(res, sum);
    }
    cout << res;
    cin >> n;

}


#include <iostream>

bool IsPrime(int v)
{
    if (1 == v)
        return false;
    for (int i = 2; i * i <= v; ++i)
    {
        if (0 == v % i)
            return false;
    }
    return true;
}

int main()
{
    int n, m;
    std::cin >> n >> m;
    int *arr = new int[n * m];

    for (int i = 0; i != n * m; ++i)
    {
        std::cin >> arr[i];
    }

    int ans = 100000000;
    for (int i = 0; i != n; ++i)
    {
        int cnt = 0;
        for (int j = 0; j != m; ++j)
        {
            if (!IsPrime(arr[i * m + j]))
            {
                int k = arr[i * m + j] + 1;
                for (; ; ++k)
                {
                    if (IsPrime(k))
                        break;
                }
                cnt += k - arr[i * m + j];
            }
        }
        ans = ans > cnt ? cnt : ans;
    }

    for (int j = 0; j != m; ++j)
    {
        int cnt = 0;
        for (int i = 0; i != n; ++i)
        {
            if (!IsPrime(arr[i * m + j]))
            {
                int k = arr[i * m + j] + 1;
                for ( ; ; ++k)
                {
                    if (IsPrime(k))
                        break;
                }
                cnt += k - arr[i * m + j];
            }
        }
        ans = ans > cnt ? cnt : ans;
    }
    std::cout << ans;
    return 0;
}


#include <iostream>

bool IsPrime(int v)
{
    if (1 == v)
        return false;
    for (int i = 2; i * i <= v; ++i)
    {
        if (0 == v % i)
            return false;
    }
    return true;
}

int main()
{
    // 标记所有的非素数
    bool *prime = new bool[1000000];
    std::memset(prime, 0, 1000000 * sizeof(bool));
    prime[1] = 1;
    for (int i = 2; i != 1000000; ++i)
    {
        if (!IsPrime(i))
        {
            prime[i] = true;
        }
    }
    int n, m;
    std::cin >> n >> m;
    int *arr = new int[n * m];

    for (int i = 0; i != n * m; ++i)
    {
        std::cin >> arr[i];
    }

    int ans = 100000000;
    for (int i = 0; i != n; ++i)
    {
        int cnt = 0;
        for (int j = 0; j != m; ++j)
        {
            if (prime[arr[i * m + j]])
            {
                int k = arr[i * m + j] + 1;
                for (; ; ++k)
                {
                    if (!prime[k])
                        break;
                }
                cnt += k - arr[i * m + j];
            }
        }
        ans = ans > cnt ? cnt : ans;
    }

    for (int j = 0; j != m; ++j)
    {
        int cnt = 0;
        for (int i = 0; i != n; ++i)
        {
            if (prime[arr[i * m + j]])
            {
                int k = arr[i * m + j] + 1;
                for ( ; ; ++k)
                {
                    if (!prime[k])
                        break;
                }
                cnt += k - arr[i * m + j];
            }
        }
        ans = ans > cnt ? cnt : ans;
    }
    std::cout << ans;
    return 0;
}


#include <iostream>

bool IsPrime(int v)
{
	if (1 == v)
		return false;
	for (int i = 2; i * i <= v; ++i)
	{
		if (0 == v % i)
			return false;
	}
	return true;
}

int main()
{
	// 标记所有的非素数
	bool *prime = new bool[100000];
	std::memset(prime, 0, 100000 * sizeof(bool));
	prime[1] = 1;
	for (int i = 2; i != 100000; ++i)
	{
		if (!IsPrime(i))
		{
			prime[i] = 1;
		}
	}
	int n, m;
	std::cin >> n >> m;
	int *arr = new int[n * m];

	for (int i = 0; i != n * m; ++i)
	{
		std::cin >> arr[i];
	}

	int ans = 100000000;
	for (int i = 0; i != n; ++i)
	{
		int cnt = 0;
		for (int j = 0; j != m; ++j)
		{
			if (prime[arr[i * m + j]])
			{
				int k = arr[i * m + j] + 1;
				for (; ; ++k)
				{
					if (!prime[k])
						break;
				}
				cnt += k - arr[i * m + j];
			}
		}
		ans = ans > cnt ? cnt : ans;
	}

	for (int j = 0; j != m; ++j)
	{
		int cnt = 0;
		for (int i = 0; i != n; ++i)
		{
			if (prime[arr[i * m + j]])
			{
				int k = arr[i * m + j] + 1;
				for ( ; ; ++k)
				{
					if (!prime[k])
						break;
				}
				cnt += k - arr[i * m + j];
			}
		}
		ans = ans > cnt ? cnt : ans;
	}
	std::cout << ans;
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;

bool isPrime(int x)
{
	if (x < 2)
		return false;
	if (x == 2)
		return true;
	for (int i = 2; i <= (int)sqrt((double)x); i++)
	{
		if (x % i == 0)
			return false;
	}
	return true;
}

int findPrime(vector<int> nto, int p)
{
	int l = 0, r = nto.size() - 1;
	int mid = (l + r) / 2;
	while (l <= r) {
		mid = (l + r) / 2;
		if (nto[mid] < p)
			l = mid + 1;
		else if (nto[mid] > p)
			r = mid - 1;
		else if (nto[mid] == p)
			return p;
	}
	if (nto[mid] > p)
		return nto[mid];
	else if (nto[mid] < p)
		return nto[mid + 1];
}

bool isNotPrime(vector<int> nto, int p)
{
	int l = 0, r = nto.size() - 1;
	int mid = (l + r) / 2;
	while (l <= r) {
		mid = (l + r) / 2;
		if (nto[mid] < p)
			l = mid + 1;
		else if (nto[mid] > p)
			r = mid - 1;
		else if (nto[mid] == p)
			return false;
	}
	return true;
}

int main()
{
	vector<int> nto;
	int m, n;
	int a[501][501];
	int hang[501] = { 0 }, cot[501] = { 0 };
	cin >> n >> m;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
			cin >> a[i][j];
	}
	for (int i = 0; i < 117241; i++)
	{
		if (isPrime(i))
			nto.push_back(i);
	}
	//nto.insert(nto.begin(), 0);
	vector<pair<int, pair<int, int>>> notPrime;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			if (isNotPrime(nto,a[i][j]))
			{
				pair<int, pair<int, int>> x;
				x.first = a[i][j];
				x.second.first = i;
				x.second.second = j;
				notPrime.push_back(x);
			}
		}
	}

	for (int i = 0; i < notPrime.size(); i++)
	{
		int x = findPrime(nto, notPrime[i].first);
		int h = notPrime[i].second.first;
		int c = notPrime[i].second.second;
		hang[h] += x - notPrime[i].first;
		cot[c] += x - notPrime[i].first;
	}
	int res = INT_MAX;
	for (int i = 0; i < n; i++)
	{
		if (res > hang[i])
			res = hang[i];
	}
	for (int i = 0; i < m; i++)
	{
		if (res > cot[i])
			res = cot[i];
	}
	cout << res;
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>

#define MAX_PRIME  117241

using namespace std;

int main()
{
	vector<int> nto;
	int m, n;
	int a[501][501];
	int hang[501] = { 0 }, cot[501] = { 0 };
	cin >> n >> m;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
			cin >> a[i][j];
	}

	bool soNgto[MAX_PRIME];
	for (int i = 0; i < MAX_PRIME; i++)
		soNgto[i] = true;
	soNgto[0] = soNgto[1] = false;
	for (int i = 2; i < MAX_PRIME; i++)
	{
		if (soNgto[i] == false)
			continue;
		soNgto[i] = true;
		int j = 2*i;
		while (j < MAX_PRIME) {
			soNgto[j] = false;
			j = j + i;
		}
	}

	vector<pair<int, pair<int, int>>> notPrime;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			if (!soNgto[a[i][j]])
			{
				pair<int, pair<int, int>> x;
				x.first = a[i][j];
				x.second.first = i;
				x.second.second = j;
				notPrime.push_back(x);
			}
		}
	}

	for (int i = 0; i < notPrime.size(); i++)
	{
		int x;
		for (int j = notPrime[i].first; j < MAX_PRIME; j++)
		{
			if (soNgto[j])
			{
				x = j;
				break;
			}
		}

		int h = notPrime[i].second.first;
		int c = notPrime[i].second.second;
		hang[h] += x - notPrime[i].first;
		cot[c] += x - notPrime[i].first;
	}
	int res = INT_MAX;
	for (int i = 0; i < n; i++)
	{
		if (res > hang[i])
			res = hang[i];
	}
	for (int i = 0; i < m; i++)
	{
		if (res > cot[i])
			res = cot[i];
	}
	cout << res;
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;

bool isPrime(int x)
{
	if (x < 2)
		return false;
	if (x == 2)
		return true;
	for (int i = 2; i <= (int)sqrt((double)x); i++)
	{
		if (x % i == 0)
			return false;
	}
	return true;
}

int findPrime(vector<int> nto, int p)
{
	int l = 0, r = nto.size() - 1;
	int mid = (l + r) / 2;
	while (l <= r) {
		mid = (l + r) / 2;
		if (nto[mid] < p)
			l = mid + 1;
		else if (nto[mid] > p)
			r = mid - 1;
		else if (nto[mid] == p)
			return p;
	}
	if (nto[mid] > p)
		return nto[mid];
	else if (nto[mid] < p)
		return nto[mid + 1];
}

bool isNotPrime(vector<int> nto, int p)
{
	int l = 0, r = nto.size() - 1;
	int mid = (l + r) / 2;
	while (l <= r) {
		mid = (l + r) / 2;
		if (nto[mid] < p)
			l = mid + 1;
		else if (nto[mid] > p)
			r = mid - 1;
		else if (nto[mid] == p)
			return false;
	}
	return true;
}

int main()
{
	vector<int> nto;
	int m, n;
	int a[501][501];
	int hang[501] = { 0 }, cot[501] = { 0 };
	cin >> n >> m;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
			cin >> a[i][j];
	}
	for (int i = 0; i < 117241; i++)
	{
		if (isPrime(i))
			nto.push_back(i);
	}
	//nto.insert(nto.begin(), 0);

	bool soNgto[117241] = { false };
	for (int i = 0; i < nto.size(); i++)
	{
		soNgto[soNgto[i]] = true;
	}

	vector<pair<int, pair<int, int>>> notPrime;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			if (!soNgto[a[i][j]])
			{
				pair<int, pair<int, int>> x;
				x.first = a[i][j];
				x.second.first = i;
				x.second.second = j;
				notPrime.push_back(x);
			}
		}
	}

	for (int i = 0; i < notPrime.size(); i++)
	{
		int x = findPrime(nto, notPrime[i].first);
		int h = notPrime[i].second.first;
		int c = notPrime[i].second.second;
		hang[h] += x - notPrime[i].first;
		cot[c] += x - notPrime[i].first;
	}
	int res = INT_MAX;
	for (int i = 0; i < n; i++)
	{
		if (res > hang[i])
			res = hang[i];
	}
	for (int i = 0; i < m; i++)
	{
		if (res > cot[i])
			res = cot[i];
	}
	cout << res;
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;
/*
bool isPrime(int x)
{
	if (x < 2)
		return false;
	if (x == 2)
		return true;
	for (int i = 2; i <= (int)sqrt((double)x); i++)
	{
		if (x % i == 0)
			return false;
	}
	return true;
}

int findPrime(vector<int> nto, int p)
{
	int l = 0, r = nto.size() - 1;
	int mid = (l + r) / 2;
	while (l <= r) {
		mid = (l + r) / 2;
		if (nto[mid] < p)
			l = mid + 1;
		else if (nto[mid] > p)
			r = mid - 1;
		else if (nto[mid] == p)
			return p;
	}
	if (nto[mid] > p)
		return nto[mid];
	else if (nto[mid] < p)
		return nto[mid + 1];
}

bool isNotPrime(vector<int> nto, int p)
{
	int l = 0, r = nto.size() - 1;
	int mid = (l + r) / 2;
	while (l <= r) {
		mid = (l + r) / 2;
		if (nto[mid] < p)
			l = mid + 1;
		else if (nto[mid] > p)
			r = mid - 1;
		else if (nto[mid] == p)
			return false;
	}
	return true;
}*/

int main()
{
	vector<int> nto;
	int m, n;
	int a[501][501];
	int hang[501] = { 0 }, cot[501] = { 0 };
	cin >> n >> m;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
			cin >> a[i][j];
	}
	/*
	for (int i = 0; i < 117241; i++)
	{
		if (isPrime(i))
			nto.push_back(i);
	}*/
	//nto.insert(nto.begin(), 0);

	int l = 117241;
	bool soNgto[117241];
	for (int i = 0; i < 117241; i++)
		soNgto[i] = true;
	soNgto[0] = soNgto[1] = false;
	for (int i = 2; i < l; i++)
	{
		if (soNgto[i] == false)
			continue;
		soNgto[i] = true;
		int j = 2*i;
		while (j < l) {
			soNgto[j] = false;
			j = j + i;
		}
	}

	vector<pair<int, pair<int, int>>> notPrime;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			if (!soNgto[a[i][j]])
			{
				pair<int, pair<int, int>> x;
				x.first = a[i][j];
				x.second.first = i;
				x.second.second = j;
				notPrime.push_back(x);
			}
		}
	}

	for (int i = 0; i < notPrime.size(); i++)
	{
		int x;
		for (int j = notPrime[i].first; j < l; j++)
		{
			if (soNgto[j])
			{
				x = j;
				break;
			}
		}

		int h = notPrime[i].second.first;
		int c = notPrime[i].second.second;
		hang[h] += x - notPrime[i].first;
		cot[c] += x - notPrime[i].first;
	}
	int res = INT_MAX;
	for (int i = 0; i < n; i++)
	{
		if (res > hang[i])
			res = hang[i];
	}
	for (int i = 0; i < m; i++)
	{
		if (res > cot[i])
			res = cot[i];
	}
	cout << res;
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;

bool isPrime(int x)
{
	if (x < 2)
		return false;
	if (x == 2)
		return true;
	for (int i = 2; i <= (int)sqrt(x); i++)
	{
		if (x % i == 0)
			return false;
	}
	return true;
}

int findPrime(vector<int> nto, int p)
{
	int l = 0, r = nto.size() - 1;
	int mid = (l + r) / 2;
	while (l <= r) {
		mid = (l + r) / 2;
		if (nto[mid] < p)
			l = mid + 1;
		else if (nto[mid] > p)
			r = mid - 1;
		else if (nto[mid] == p)
			return p;
	}
	if (nto[mid] > p)
		return nto[mid];
	else if (nto[mid] < p)
		return nto[mid + 1];
}

int main()
{
	vector<int> nto;
	int m, n;
	int a[501][501];
	int hang[501] = { 0 }, cot[501] = { 0 };
	cin >> n >> m;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
			cin >> a[i][j];
	}
	for (int i = 0; i < 117241; i++)
	{
		if (isPrime(i))
			nto.push_back(i);
	}
	//nto.insert(nto.begin(), 0);
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			if (!isPrime(a[i][j]))
			{
				int x = findPrime(nto, a[i][j]);
				hang[i] += x - a[i][j];
				cot[j] += x - a[i][j];
			}
		}
	}
	int res = INT_MAX;
	for (int i = 0; i < n; i++)
	{
		if (res > hang[i])
			res = hang[i];
	}
	for (int i = 0; i < m; i++)
	{
		if (res > cot[i])
			res = cot[i];
	}
	cout << res;
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;

bool isPrime(int x)
{
	if (x < 2)
		return false;
	if (x == 2)
		return true;
	for (int i = 2; i <= sqrt(x); i++)
	{
		if (x % i == 0)
			return false;
	}
	return true;
}

int findPrime(vector<int> nto, int p)
{
	int l = 0, r = nto.size() - 1;
	int mid = (l + r) / 2;
	while (l <= r) {
		mid = (l + r) / 2;
		if (nto[mid] < p)
			l = mid + 1;
		else if (nto[mid] > p)
			r = mid - 1;
		else if (nto[mid] == p)
			return p;
	}
	if (nto[mid] > p)
		return nto[mid];
	else if (nto[mid] < p)
		return nto[mid + 1];
}

int main()
{
	vector<int> nto;
	int m, n;
	int a[501][501];
	int hang[501] = { 0 }, cot[501] = { 0 };
	cin >> n >> m;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
			cin >> a[i][j];
	}
	for (int i = 0; i < 117241; i++)
	{
		if (isPrime(i))
			nto.push_back(i);
	}
	//nto.insert(nto.begin(), 0);
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			if (!isPrime(a[i][j]))
			{
				int x = findPrime(nto, a[i][j]);
				hang[i] += x - a[i][j];
				cot[j] += x - a[i][j];
			}
		}
	}
	int res = INT_MAX;
	for (int i = 0; i < n; i++)
	{
		if (res > hang[i])
			res = hang[i];
	}
	for (int i = 0; i < m; i++)
	{
		if (res > cot[i])
			res = cot[i];
	}
	cout << res;
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;

bool isPrime(int x)
{
	if (x < 2)
		return false;
	if (x == 2)
		return true;
	for (int i = 2; i <= (int)sqrt((double)x); i++)
	{
		if (x % i == 0)
			return false;
	}
	return true;
}

int findPrime(vector<int> nto, int p)
{
	int l = 0, r = nto.size() - 1;
	int mid = (l + r) / 2;
	while (l <= r) {
		mid = (l + r) / 2;
		if (nto[mid] < p)
			l = mid + 1;
		else if (nto[mid] > p)
			r = mid - 1;
		else if (nto[mid] == p)
			return p;
	}
	if (nto[mid] > p)
		return nto[mid];
	else if (nto[mid] < p)
		return nto[mid + 1];
}

int main()
{
	vector<int> nto;
	int m, n;
	int a[501][501];
	int hang[501] = { 0 }, cot[501] = { 0 };
	cin >> n >> m;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
			cin >> a[i][j];
	}
	for (int i = 0; i < 117241; i++)
	{
		if (isPrime(i))
			nto.push_back(i);
	}
	//nto.insert(nto.begin(), 0);
	vector<pair<int, pair<int, int>>> notPrime;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			if (!isPrime(a[i][j]))
			{
				pair<int, pair<int, int>> x;
				x.first = a[i][j];
				x.second.first = i;
				x.second.second = j;
				notPrime.push_back(x);
			}
		}
	}

	for (int i = 0; i < notPrime.size(); i++)
	{
		int x = findPrime(nto, notPrime[i].first);
		int h = notPrime[i].second.first;
		int c = notPrime[i].second.second;
		hang[h] += x - notPrime[i].first;
		cot[c] += x - notPrime[i].first;
	}
	int res = INT_MAX;
	for (int i = 0; i < n; i++)
	{
		if (res > hang[i])
			res = hang[i];
	}
	for (int i = 0; i < m; i++)
	{
		if (res > cot[i])
			res = cot[i];
	}
	cout << res;
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;

bool isPrime(int x)
{
	if (x < 2)
		return false;
	if (x == 2)
		return true;
	for (int i = 2; i <= (int)sqrt((double)x); i++)
	{
		if (x % i == 0)
			return false;
	}
	return true;
}

int findPrime(vector<int> nto, int p)
{
	int l = 0, r = nto.size() - 1;
	int mid = (l + r) / 2;
	while (l <= r) {
		mid = (l + r) / 2;
		if (nto[mid] < p)
			l = mid + 1;
		else if (nto[mid] > p)
			r = mid - 1;
		else if (nto[mid] == p)
			return p;
	}
	if (nto[mid] > p)
		return nto[mid];
	else if (nto[mid] < p)
		return nto[mid + 1];
}

int main()
{
	vector<int> nto;
	int m, n;
	int a[501][501];
	int hang[501] = { 0 }, cot[501] = { 0 };
	cin >> n >> m;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
			cin >> a[i][j];
	}
	for (int i = 0; i < 117241; i++)
	{
		if (isPrime(i))
			nto.push_back(i);
	}
	//nto.insert(nto.begin(), 0);
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			if (!isPrime(a[i][j]))
			{
				int x = findPrime(nto, a[i][j]);
				hang[i] += x - a[i][j];
				cot[j] += x - a[i][j];
			}
		}
	}
	int res = INT_MAX;
	for (int i = 0; i < n; i++)
	{
		if (res > hang[i])
			res = hang[i];
	}
	for (int i = 0; i < m; i++)
	{
		if (res > cot[i])
			res = cot[i];
	}
	cout << res;
	return 0;
}


#include <stdio.h>
#include <math.h>
using namespace std;

#define MAX 100005

int main()
{
	int min = 100000000;
	int n, m, primeNumber[MAX], row[505] = { 0 }, col[505] = { 0 }, tmp, k;

	primeNumber[1] = 0;
	for (int i = 2; i < MAX; i++)
		primeNumber[i] = 1;

	for (int i = 2; i < MAX; i++)
		if (primeNumber[i] == 1)
		{
			tmp = i + i;
			while (tmp < MAX)
			{
				primeNumber[tmp] = 0;
				tmp += i;
			}
		}

	scanf("%d %d", &n, &m);
	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++)
		{
			scanf("%d", &k);
			tmp = k;
			while (primeNumber[k] == 0)
				k++;
			row[i] += k - tmp;
			col[j] += k - tmp;
		}

	for (int i = 0; i < n; i++)
		if (min > row[i])
			min = row[i];
	for (int j = 0; j < m; j++)
		if (min > col[j])
			min = col[j];

	printf("%d", min);
}


#include <stdio.h>
#include <math.h>
using namespace std;

// check so nguyen to
bool checkPrimeNumber(int a)
{
	if (a == 1)
		return false;

	int t = a / 2;
	for (int i = 2; i <= t; i++)
		if (a % i == 0)
			return false;
	return true;
}

// find prime number
int FindPrimeNumber(int a)
{
	a++;
	while (true)
	{
		if (checkPrimeNumber(a))
			return a;
		a++;
	}
}

int CountIncreaseAtRow(int matrix[][505], int m, int i)
{
	int count = 0;
	for (int j = 0; j < m; j++)
		if (!checkPrimeNumber(matrix[i][j]))
			count += FindPrimeNumber(matrix[i][j]) - matrix[i][j];

	return count;
}

int CountIncreaseAtCol(int matrix[][505], int n, int j)
{
	int count = 0;
	for (int i = 0; i < n; i++)
		if (!checkPrimeNumber(matrix[i][j]))
			count += FindPrimeNumber(matrix[i][j]) - matrix[i][j];

	return count;
}

int main()
{
	int min = 100000000;
	int n, m, a[505][505], row[505] = { 0 }, col[505] = { 0 };

	scanf("%d %d", &n, &m);
	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++)
			scanf("%d", &a[i][j]);

	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++)
		{
				if (row[i] == 0)
				{
					row[i] = CountIncreaseAtRow(a, m, i);
					if (min > row[i])
						min = row[i];
				}
				if (col[j] == 0)
				{
					col[j] = CountIncreaseAtCol(a, n, j);
					if (min > col[j])
						min = col[j];
				}
		}

	printf("%d", min);
}


#include <stdio.h>
#include <math.h>
using namespace std;

#define MAX 100005

int main()
{
	int min = 100000000;
	int n, m, primeNumber[MAX], row[505] = { 0 }, col[505] = { 0 }, tmp, k;

	primeNumber[1] = 0;
	for (int i = 0; i < MAX; i++)
		primeNumber[i] = 1;

	for (int i = 2; i < MAX; i++)
		if (primeNumber[i] == 1)
		{
			tmp = i + i;
			while (tmp < MAX)
			{
				primeNumber[tmp] = 0;
				tmp += i;
			}
		}

	scanf("%d %d", &n, &m);
	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++)
		{
			scanf("%d", &k);
			tmp = k;
			while (primeNumber[k] == 0)
				k++;
			row[i] += k - tmp;
			col[j] += k - tmp;
		}

	for (int i = 0; i < n; i++)
		if (min > row[i])
			min = row[i];
	for (int j = 0; j < m; j++)
		if (min > col[j])
			min = col[j];

	printf("%d", min);
}


#include <stdio.h>
#include <math.h>
using namespace std;

// check so nguyen to
bool checkPrimeNumber(int a)
{
	if (a == 1)
		return false;

	int t = sqrt((double)a);
	for (int i = 2; i <= t; i++)
		if (a % i == 0)
			return false;
	return true;
}

// find prime number
int FindPrimeNumber(int a)
{
	a++;
	while (true)
	{
		if (checkPrimeNumber(a))
			return a;
		a++;
	}
}

int CountIncreaseAtRow(int matrix[][505], int m, int i)
{
	int count = 0;
	for (int j = 0; j < m; j++)
		if (!checkPrimeNumber(matrix[i][j]))
			count += FindPrimeNumber(matrix[i][j]) - matrix[i][j];

	return count;
}

int CountIncreaseAtCol(int matrix[][505], int n, int j)
{
	int count = 0;
	for (int i = 0; i < n; i++)
		if (!checkPrimeNumber(matrix[i][j]))
			count += FindPrimeNumber(matrix[i][j]) - matrix[i][j];

	return count;
}

int main()
{
	int min = 100000000;
	int n, m, a[505][505], row[505] = { 0 }, col[505] = { 0 };

	scanf("%d %d", &n, &m);
	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++)
			scanf("%d", &a[i][j]);

	for (int i = 0; i < n; i++)
	{
		row[i] = CountIncreaseAtRow(a, m, i);
		if (min > row[i])
			min = row[i];
	}
	for (int j = 0; j < m; j++)
	{
		col[j] = CountIncreaseAtCol(a, n, j);
		if (min > col[j])
			min = col[j];
	}

	printf("%d", min);
}


#include <stdio.h>
#include <math.h>
using namespace std;

// check so nguyen to
bool checkPrimeNumber(int a)
{
	if (a == 1)
		return false;

	int t = a / 2;
	for (int i = 2; i <= t; i++)
		if (a % i == 0)
			return false;
	return true;
}

// find prime number
int FindPrimeNumber(int a)
{
	a++;
	while (true)
	{
		if (checkPrimeNumber(a))
			return a;
		a++;
	}
}

int CountIncreaseAtRow(int matrix[][505], int m, int i)
{
	int count = 0;
	for (int j = 0; j < m; j++)
		if (!checkPrimeNumber(matrix[i][j]))
			count += FindPrimeNumber(matrix[i][j]) - matrix[i][j];

	return count;
}

int CountIncreaseAtCol(int matrix[][505], int n, int j)
{
	int count = 0;
	for (int i = 0; i < n; i++)
		if (!checkPrimeNumber(matrix[i][j]))
			count += FindPrimeNumber(matrix[i][j]) - matrix[i][j];

	return count;
}

int main()
{
	int min = 100000000;
	int n, m, a[505][505], row[505] = { 0 }, col[505] = { 0 };

	scanf("%d %d", &n, &m);
	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++)
			scanf("%d", &a[i][j]);

	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++)
		{
			if (checkPrimeNumber(a[i][j]))
			{
				if (row[i] == 0)
				{
					row[i] = CountIncreaseAtRow(a, m, i);
					if (min > row[i])
						min = row[i];
				}
				if (col[j] == 0)
				{
					col[j] = CountIncreaseAtCol(a, n, j);
					if (min > col[j])
						min = col[j];
				}
			}
		}

	printf("%d", min);
}


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<unordered_map>
#include<queue>
#include<stack>
#include<iterator>
#include<cmath>
#include<string>
#include<sstream>
#include<cstring>
#include<ctype.h>
#include<iomanip>
#include<bitset>
#include<stdio.h>
#include<fstream>
#include<stdlib.h>
#include<math.h>
#include<numeric>
using namespace std;

#define all(v)         v.begin(),v.end()
#define sz(V)        ((ll)((v).size()))
#define pp(x)               push_back(x)
#define ck(a)               (a<1 || a>9)
#define lop(i,n)    for(ll i=0;i<n;i++)
#define loop(i,f,l) for(ll i=f;i<l;i++)
#define READ(s)   freopen(s, "r", stdin)
#define WRITE(s) freopen(s, "w", stdout)
#define scl(n)          scanf("%lld",&n)
#define sc(n)             scanf("%d",&n)
#define INF                   1000000000
#define PI             3.141592653589793
#define print(v)      lop(i, v.size()) cout<<v[i]<<" ";
#define read(v)   lop(i, v.size()) cin>>v[i];
typedef string            ss;
typedef long long         ll;
typedef pair <ll, ll>   ii;
typedef pair<string, ll> si;
typedef pair<ll, string> is;
typedef pair<char, ll>  chi;
typedef vector<ii>       vii;
typedef vector<ll>       vi;
typedef vector<vi>       vvi;
typedef vector<string>    vs;
typedef vector<ll>       vll;
//READ("input.txt");WRITE("output.txt");
ll gcd(ll a, ll b) { return (b == 0 ? a : gcd(b, a % b)); }
ll lcm(ll a, ll b){ return ((a*b) / gcd(a, b)); }
ll pw(ll b, ll p){ if (!p) return 1; ll sq = pw(b, p / 2); sq *= sq; if (p % 2) sq *= b; return sq; }
ll sd(ll x){ return x<10 ? x : x % 10 + sd(x / 10); }
double sq(double x){ lop(i, x)if ((double)i*i > x) return (i - 1); return double(1); }

int main()
{
	int n, max = 0, m; cin >> n >> m; vector<vector<int>> v(n, vector<int>(m));
	lop(i, n)
	{
		lop(j, m)
		{
			cin >> v[i][j];
		}
	}
	max = 100001;
	vector <int>arr(max);
	arr[0] = 1; arr[1] = 1;
	for (int i = 2; i *i < max; i++)
	{
		if (arr[i] == 0)
		{
			for (int j = i; j*i < max; j++) arr[i*j] = 1;
		}
	}
	vector<vector<int>> sum(n, vector<int>(m));
	lop(i, n)
	{
		lop(j, m)
		{
			int x = v[i][j];
			while (arr[x] != 0)
				x++;
			sum[i][j] = x-v[i][j];
		}
	}
	int min = accumulate(all(sum[0]),0);
	lop(i, n)
	{
		int x = accumulate(sum[i].begin(), sum[i].end(), 0);
		if (x < min)
			min = x;
	}
	lop(j, m)
	{
		int sums = 0;
		lop(i, n)
		{
			sums += sum[i][j];
		}
		if (sums < min)
			min = sums;
	}
	cout << min;
}


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<unordered_map>
#include<queue>
#include<stack>
#include<iterator>
#include<cmath>
#include<string>
#include<sstream>
#include<cstring>
#include<ctype.h>
#include<iomanip>
#include<bitset>
#include<stdio.h>
#include<fstream>
#include<stdlib.h>
#include<math.h>
#include<numeric>
using namespace std;

#define all(v)         v.begin(),v.end()
#define sz(V)        ((ll)((v).size()))
#define pp(x)               push_back(x)
#define ck(a)               (a<1 || a>9)
#define lop(i,n)    for(ll i=0;i<n;i++)
#define loop(i,f,l) for(ll i=f;i<l;i++)
#define READ(s)   freopen(s, "r", stdin)
#define WRITE(s) freopen(s, "w", stdout)
#define scl(n)          scanf("%lld",&n)
#define sc(n)             scanf("%d",&n)
#define INF                   1000000000
#define PI             3.141592653589793
#define print(v)      lop(i, v.size()) cout<<v[i]<<" ";
#define read(v)   lop(i, v.size()) cin>>v[i];
typedef string            ss;
typedef long long         ll;
typedef pair <ll, ll>   ii;
typedef pair<string, ll> si;
typedef pair<ll, string> is;
typedef pair<char, ll>  chi;
typedef vector<ii>       vii;
typedef vector<ll>       vi;
typedef vector<vi>       vvi;
typedef vector<string>    vs;
typedef vector<ll>       vll;
//READ("input.txt");WRITE("output.txt");
ll gcd(ll a, ll b) { return (b == 0 ? a : gcd(b, a % b)); }
ll lcm(ll a, ll b){ return ((a*b) / gcd(a, b)); }
ll pw(ll b, ll p){ if (!p) return 1; ll sq = pw(b, p / 2); sq *= sq; if (p % 2) sq *= b; return sq; }
ll sd(ll x){ return x<10 ? x : x % 10 + sd(x / 10); }
double sq(double x){ lop(i, x)if ((double)i*i > x) return (i - 1); return double(1); }

int main()
{
    int n, max, m; cin >> n >> m; vector<vector<ll>> v(n, vector<ll>(m));
    lop(i, n)
    {
        lop(j, m)
        {
            cin >> v[i][j];
        }
    }
    max = 100001;
    vector <int>arr(max);
    arr[0] = 1; arr[1] = 1;
    for (int i = 2; i *i < max; i++)
    {
        if (arr[i] == 0)
        {
            for (int j = i; j*i < max; j++) arr[i*j] = 1;
        }
    }
    vector<vector<ll>> sum(n, vector<ll>(m));
    lop(i, n)
    {
        lop(j, m)
        {
            int x = v[i][j];
            while (arr[x] != 0)
                x++;
            sum[i][j] = x-v[i][j];
        }
    }
    ll min = accumulate(all(sum[0]),0);
    lop(i, n)
    {
        int x = accumulate(sum[i].begin(), sum[i].end(), 0);
        if (x < min)
            min = x;
    }
    lop(j, m)
    {
        ll sums = 0;
        lop(i, n)
        {
            sums += sum[i][j];
        }
        if (sums < min)
            min = sums;
    }
    cout << min;
}


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<unordered_map>
#include<queue>
#include<stack>
#include<iterator>
#include<cmath>
#include<string>
#include<sstream>
#include<cstring>
#include<ctype.h>
#include<iomanip>
#include<bitset>
#include<stdio.h>
#include<fstream>
#include<stdlib.h>
#include<math.h>
#include<numeric>
using namespace std;

#define all(v)         v.begin(),v.end()
#define sz(V)        ((ll)((v).size()))
#define pp(x)               push_back(x)
#define ck(a)               (a<1 || a>9)
#define lop(i,n)    for(ll i=0;i<n;i++)
#define loop(i,f,l) for(ll i=f;i<l;i++)
#define READ(s)   freopen(s, "r", stdin)
#define WRITE(s) freopen(s, "w", stdout)
#define scl(n)          scanf("%lld",&n)
#define sc(n)             scanf("%d",&n)
#define INF                   1000000000
#define PI             3.141592653589793
#define print(v)      lop(i, v.size()) cout<<v[i]<<" ";
#define read(v)   lop(i, v.size()) cin>>v[i];
typedef string            ss;
typedef long long         ll;
typedef pair <ll, ll>   ii;
typedef pair<string, ll> si;
typedef pair<ll, string> is;
typedef pair<char, ll>  chi;
typedef vector<ii>       vii;
typedef vector<ll>       vi;
typedef vector<vi>       vvi;
typedef vector<string>    vs;
typedef vector<ll>       vll;
//READ("input.txt");WRITE("output.txt");
ll gcd(ll a, ll b) { return (b == 0 ? a : gcd(b, a % b)); }
ll lcm(ll a, ll b){ return ((a*b) / gcd(a, b)); }
ll pw(ll b, ll p){ if (!p) return 1; ll sq = pw(b, p / 2); sq *= sq; if (p % 2) sq *= b; return sq; }
ll sd(ll x){ return x<10 ? x : x % 10 + sd(x / 10); }
double sq(double x){ lop(i, x)if ((double)i*i > x) return (i - 1); return double(1); }

int main()
{
    int n, max, m; cin >> n >> m; vector<vector<ll>> v(n, vector<ll>(m));
    lop(i, n)
    {
        lop(j, m)
        {
            cin >> v[i][j];
        }
    }
    max = 100002;
    vector <int>arr(max);
    arr[0] = 1; arr[1] = 1;
    for (int i = 2; i *i < max; i++)
    {
        if (arr[i] == 0)
        {
            for (int j = i; j*i < max; j++) arr[i*j] = 1;
        }
    }
    vector<vector<ll>> sum(n, vector<ll>(m));
    lop(i, n)
    {
        lop(j, m)
        {
            int x = v[i][j];
            while (arr[x] != 0)
                x++;
            sum[i][j] = x-v[i][j];
        }
    }
    ll min = accumulate(all(sum[0]),0);
    lop(i, n)
    {
        int x = accumulate(sum[i].begin(), sum[i].end(), 0);
        if (x < min)
            min = x;
    }
    lop(j, m)
    {
        ll sums = 0;
        lop(i, n)
        {
            sums += sum[i][j];
        }
        if (sums < min)
            min = sums;
    }
    cout << min;
}


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<unordered_map>
#include<queue>
#include<stack>
#include<iterator>
#include<cmath>
#include<string>
#include<sstream>
#include<cstring>
#include<ctype.h>
#include<iomanip>
#include<bitset>
#include<stdio.h>
#include<fstream>
#include<stdlib.h>
#include<math.h>
#include<numeric>
using namespace std;

#define all(v)         v.begin(),v.end()
#define sz(V)        ((ll)((v).size()))
#define pp(x)               push_back(x)
#define ck(a)               (a<1 || a>9)
#define lop(i,n)    for(ll i=0;i<n;i++)
#define loop(i,f,l) for(ll i=f;i<l;i++)
#define READ(s)   freopen(s, "r", stdin)
#define WRITE(s) freopen(s, "w", stdout)
#define scl(n)          scanf("%lld",&n)
#define sc(n)             scanf("%d",&n)
#define INF                   1000000000
#define PI             3.141592653589793
#define print(v)      lop(i, v.size()) cout<<v[i]<<" ";
#define read(v)   lop(i, v.size()) cin>>v[i];
typedef string            ss;
typedef long long         ll;
typedef pair <ll, ll>   ii;
typedef pair<string, ll> si;
typedef pair<ll, string> is;
typedef pair<char, ll>  chi;
typedef vector<ii>       vii;
typedef vector<ll>       vi;
typedef vector<vi>       vvi;
typedef vector<string>    vs;
typedef vector<ll>       vll;
//READ("input.txt");WRITE("output.txt");
ll gcd(ll a, ll b) { return (b == 0 ? a : gcd(b, a % b)); }
ll lcm(ll a, ll b){ return ((a*b) / gcd(a, b)); }
ll pw(ll b, ll p){ if (!p) return 1; ll sq = pw(b, p / 2); sq *= sq; if (p % 2) sq *= b; return sq; }
ll sd(ll x){ return x<10 ? x : x % 10 + sd(x / 10); }
double sq(double x){ lop(i, x)if ((double)i*i > x) return (i - 1); return double(1); }

int main()
{
    int n, max, m; cin >> n >> m; vector<vector<ll>> v(n, vector<ll>(m));
    lop(i, n)
    {
        lop(j, m)
        {
            cin >> v[i][j];
        }
    }
    max = 100004;
    vector <int>arr(max);
    arr[0] = 1; arr[1] = 1;
    for (int i = 2; i *i < max; i++)
    {
        if (arr[i] == 0)
        {
            for (int j = i; j*i < max; j++) arr[i*j] = 1;
        }
    }
    vector<vector<ll>> sum(n, vector<ll>(m));
    lop(i, n)
    {
        lop(j, m)
        {
            int x = v[i][j];
            while (arr[x] != 0)
                x++;
            sum[i][j] = x-v[i][j];
        }
    }
    ll min = accumulate(all(sum[0]),0);
    lop(i, n)
    {
        int x = accumulate(sum[i].begin(), sum[i].end(), 0);
        if (x < min)
            min = x;
    }
    lop(j, m)
    {
        ll sums = 0;
        lop(i, n)
        {
            sums += sum[i][j];
        }
        if (sums < min)
            min = sums;
    }
    cout << min;
}


#include<iostream>
#include<stdio.h>
#include<bitset>
#include<vector>
#include<cmath>
#include<algorithm>

using namespace std;
typedef vector<int> vi;
typedef long long ll;

int n ,m;
ll _sieve_size;
bitset<10000010> bs;
vi primes;
ll arr[501][501];
bool arr2[501][501];
int row[501];
int col[501];
void seive(ll upperbound){
	_sieve_size = upperbound + 1;
	bs.set();
	bs[0] = bs[1] = 0;
	for (ll i = 0; i < _sieve_size; i++)
	{
		if (bs[i]){
			for (ll j = i * i; j <= _sieve_size; j += i)
				bs[j] = 0;
			primes.push_back((int)i);
		}
	}
}
bool isPrime(ll N){
	if (N <= _sieve_size)
		return bs[N];
	for (int i = 0; i < (int)primes.size(); i++){
		if (N % primes[i] == 0)
			return false;
		return true;
	}
}
int main(){

	freopen("in.txt", "r", stdin);
	int minn = 1000000000;
	seive(100000);
	cin >> n >> m;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			cin >> arr[i][j];
			if (isPrime(arr[i][j])){
				arr[i][j] = 0;
			}
			else{
			 vector<int>::iterator low = lower_bound(primes.begin(), primes.end(), arr[i][j]);
			 arr[i][j] = (primes[low - primes.begin()] - arr[i][j]);
				/*for (ll k = 0; k < _sieve_size; k++)
				{
					if (arr[i][j] != 0){
						if (primes[k] > arr[i][j])
						{
							arr[i][j] = primes[k] - arr[i][j];
							break;
						}
					}
				}*/
			}
			row[i] += arr[i][j];
			col[j] += arr[i][j];
		}

	}

	int min1 = *min_element(row, row + n);
	int min2 = *min_element(col, col + m);
	cout << min(min1,min2) << endl;
	return 0;
}


#include<iostream>
#include<stdio.h>
#include<bitset>
#include<vector>
using namespace std;
typedef vector<int> vi;
typedef long long ll;

int n ,m;
ll _sieve_size;
bitset<10000010> bs;
vi primes;
ll arr[501][501];
bool arr2[501][501];
void seive(ll upperbound){
	_sieve_size = upperbound + 1;
	bs.set();
	bs[0] = bs[1] = 0;
	for (ll i = 0; i < _sieve_size; i++)
	{
		if (bs[i]){
			for (ll j = i * i; j <= _sieve_size; j += i)
				bs[j] = 0;
			primes.push_back((int)i);
		}
	}
}
bool isPrime(ll N){
	if (N <= _sieve_size)
		return bs[N];
	for (int i = 0; i < (int)primes.size(); i++){
		if (N % primes[i] == 0)
			return false;
		return true;
	}
}
int main(){

	freopen("in.txt", "r", stdin);
	seive(100000);
	cin >> n >> m;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			cin >> arr[i][j];
			if (isPrime(arr[i][j])){
				arr[i][j] = 0;
			}
			else{
				for (ll k = 0; k < _sieve_size; k++)
				{
					if (arr[i][j] != 0){
						if (primes[k] > arr[i][j])
						{
							arr[i][j] = primes[k] - arr[i][j];
							break;
						}
					}
				}
			}
		}
	}
	int minn = 1000000000, summ1 = 0 , summ2 = 0;
	for (int i = 0; i < n; i++)
	{
		summ1 = 0 , summ2 = 0;
		for (int j = 0; j < m; j++)
		{
			summ1 += arr[i][j];
		}

		if (minn > summ1){
			minn = summ1;
		}
	}

	for (int i = 0; i < m; i++)
	{
		summ1 = 0, summ2 = 0;
		for (int j = 0; j < n; j++)
		{
			summ2 += arr[j][i];
		}

		if (minn > summ2){
			minn = summ2;
		}
	}
	cout << minn << endl;
	return 0;
}


#include<iostream>
#include<stdio.h>
#include<bitset>
#include<vector>
using namespace std;
typedef vector<int> vi;
typedef long long ll;

int n ,m;
ll _sieve_size;
bitset<10000010> bs;
vi primes;
ll arr[501][501];
bool arr2[501][501];
void seive(ll upperbound){
	_sieve_size = upperbound + 1;
	bs.set();
	bs[0] = bs[1] = 0;
	for (ll i = 0; i < _sieve_size; i++)
	{
		if (bs[i]){
			for (ll j = i * i; j <= _sieve_size; j += i)
				bs[j] = 0;
			primes.push_back((int)i);
		}
	}
}
bool isPrime(ll N){
	if (N <= _sieve_size)
		return bs[N];
	for (int i = 0; i < (int)primes.size(); i++){
		if (N % primes[i] == 0)
			return false;
		return true;
	}
}
int main(){

	//freopen("in.txt", "r", stdin);
	seive(100000);
	cin >> n >> m;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			cin >> arr[i][j];
			if (isPrime(arr[i][j])){
				arr[i][j] = 0;
			}
			else{
				for (ll k = 0; k < _sieve_size; k++)
				{
					if (arr[i][j] != 0){
						if (primes[k] > arr[i][j])
						{
							arr[i][j] = primes[k] - arr[i][j];
							break;
						}
					}
				}
			}
		}
	}
	int minn = 1000000000, summ1 = 0 , summ2 = 0;
	for (int i = 0; i < n; i++)
	{
		summ1 = 0 , summ2 = 0;
		for (int j = 0; j < m; j++)
		{
			summ1 += arr[i][j];
		}

		if (minn > summ1){
			minn = summ1;
		}
	}

	for (int i = 0; i < m; i++)
	{
		summ1 = 0, summ2 = 0;
		for (int j = 0; j < n; j++)
		{
			summ2 += arr[j][i];
		}

		if (minn > summ2){
			minn = summ2;
		}
	}
	cout << minn << endl;
	return 0;
}


//#include <iostream>
//#include<stdio.h>
//#include<algorithm>
//#include<vector>
//
//
//using namespace std;
//
//int n;
//int arr[100001];
//int arr2[5001] = { 0 };
//int arr3[100001];
//vector<int> vec;
//int main(){
//	freopen("in.txt", "r", stdin);
//	int maxx = 0;
//	cin >> n;
//	for (int i = 0; i < n; i++)
//	{
//		cin >> arr[i];
//		arr2[arr[i]]++;
//		if (maxx < arr[i]){
//			maxx = arr[i];
//
//		}
//	}
//	arr2[maxx] = 0;
//	int counter = 0 , j = 0;
//	for (int i = 0; i < maxx; i++)
//	{
//		if (arr2[i] > 0){
//			arr2[i]--;
//			arr3[j++] = i;
//			counter++;
//		}
//	}
//
//	counter++;
//	arr3[j++] = maxx;
//	for (int i = maxx; i >= 0; i--)
//	{
//		if (arr2[i] > 0){
//			arr2[i]--;
//			counter++;
//			arr3[j++] = i;
//		}
//	}
//	cout << counter << endl;
//	cout << arr3[0] ;
//	for (int i = 1; i < j; i++)
//	{
//		cout <<" " << arr3[i];
//	}
//	cout << endl;
//
//	return 0;
//}

#include<iostream>
#include<stdio.h>
#include<bitset>
#include<vector>
using namespace std;
typedef vector<int> vi;
typedef long long ll;

int n ,m;
ll _sieve_size;
bitset<10000010> bs;
vi primes;
ll arr[501][501];
bool arr2[501][501];
int row[501];
int col[501];
void seive(ll upperbound){
	_sieve_size = upperbound + 1;
	bs.set();
	bs[0] = bs[1] = 0;
	for (ll i = 0; i < _sieve_size; i++)
	{
		if (bs[i]){
			for (ll j = i * i; j <= _sieve_size; j += i)
				bs[j] = 0;
			primes.push_back((int)i);
		}
	}
}
bool isPrime(ll N){
	if (N <= _sieve_size)
		return bs[N];
	for (int i = 0; i < (int)primes.size(); i++){
		if (N % primes[i] == 0)
			return false;
		return true;
	}
}
int main(){

	//freopen("in.txt", "r", stdin);
	seive(100000);
	cin >> n >> m;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			cin >> arr[i][j];
			if (isPrime(arr[i][j])){
				arr[i][j] = 0;
			}
			else{
				for (ll k = 0; k < _sieve_size; k++)
				{
					if (arr[i][j] != 0){
						if (primes[k] > arr[i][j])
						{
							arr[i][j] = primes[k] - arr[i][j];
							break;
						}
					}
				}
			}
			row[i] += arr[i][j];
			col[j] += arr[i][j];
		}
	}
	int minn = 1000000000, summ1 = 0, summ2 = 0;
	for (int i = 0; i < n; i++)
	{
		if (minn > row[i])
			minn = row[i];
	}

	for (int i = 0; i < m; i++)
	{
		if (minn > col[i]){
			minn = col[i];
		}
	}
	cout << minn << endl;
	return 0;
}

//#include<iostream>
//#include<stdio.h>
//#include<queue>
//
//using namespace std;
//
//typedef pair<int, int > ii;
//int n, m;
//bool vis[10001];
//void BFS(int start){
//	queue<ii>pq;
//	pq.push(make_pair(start, 1));
//	vis[start] = 1;
//	while (!pq.empty()){
//		ii x = pq.front(); pq.pop();
//		vis[x.first] = 1;
//
//		if (x.first * 2 == m || x.first - 1 == m)
//		{
//			cout << x.second<< endl;
//			break;
//		}
//		if (!vis[x.first * 2] && x.first * 2 <= 10000)
//		{
//			pq.push(make_pair(x.first * 2, x.second + 1));
//			vis[x.first * 2] = 1;
//		}
//		if (!vis[x.first - 1] && x.first - 1 > 0){
//			pq.push(make_pair(x.first - 1, x.second + 1));
//			vis[x.first -  1] = 1;
//		}
//	}
//}
//
//int main(){
//	freopen("in.txt", "r", stdin);
//	cin >> n >> m;
//	BFS(n);
//	return 0;
//}
//#include<iostream>
//#include<queue>
//#include <stdio.h>
//using namespace std;
//
//
//int adjList[2001][2001];
//bool vis[2001];
//bool vis2[2001];
//int n, a;
//int maxx = 0;
//int counter = 0;
//
//void BFS(int start){
//	queue<int>pq;
//	pq.push(start);
//	vis[start] = 1;
//	while (!pq.empty()){
//		int x = pq.front(); pq.pop();
//		counter++;
//		vis[x] = 1;
//		for (int i = 1; i <= n; i++)
//		{
//			if (!vis[i] && adjList[x][i] == 1){
//				pq.push(i);
//				cout << x << " " << i << endl;
//			}
//		}
//	}
//}
//
//void DFS(int start, int c){
//	if (maxx < c)
//		maxx = c;
//	vis[start] = 1;
//	c++;
//	for (int i = 1; i <= n; i++)
//	{
//		if (!vis[i] && adjList[start][i] == 1){
//			DFS(i,c);
//		}
//	}
//	vis[start] = 0;
//	c--;
//}
//
//
//int main(){
//	freopen("in.txt", "r", stdin);
//
//	cin >> n;
//	memset(vis, false, sizeof vis);
//	memset(vis2, false, sizeof vis);
//	counter = 0;
//	for (int i = 1; i <= n; i++)
//	{
//		cin >> a;
//		if (a == -1)
//			adjList[0][i] = 1;
//		else
//			adjList[a][i] = 1;
//	}
//	DFS(0,0);
//	cout << maxx << endl;
//	//int tc, n;
//	//cin >> tc;
//	//memset(vis, false, sizeof vis);
//	//while (tc--)
//	//{
//
//	//	cin >> n;
//	//	for (int i = 0; i < n; i++)
//	//	{
//	//		cin >> a >> b;
//	//		adjList[a][b] = 1;
//	//	}
//	//}
//
//	///*adjList[1][2] = 1;
//	//adjList[1][3] = 1;
//	//adjList[1][4] = 1;
//	//adjList[2][5] = 1;
//	//adjList[4][6] = 1;*/
//
//	//BFS(1);
//	return 0;
//}


//#include <iostream>
//#include<stdio.h>
//#include<algorithm>
//#include<vector>
//
//
//using namespace std;
//
//int n;
//int arr[100001];
//int arr2[5001] = { 0 };
//int arr3[100001];
//vector<int> vec;
//int main(){
//	freopen("in.txt", "r", stdin);
//	int maxx = 0;
//	cin >> n;
//	for (int i = 0; i < n; i++)
//	{
//		cin >> arr[i];
//		arr2[arr[i]]++;
//		if (maxx < arr[i]){
//			maxx = arr[i];
//
//		}
//	}
//	arr2[maxx] = 0;
//	int counter = 0 , j = 0;
//	for (int i = 0; i < maxx; i++)
//	{
//		if (arr2[i] > 0){
//			arr2[i]--;
//			arr3[j++] = i;
//			counter++;
//		}
//	}
//
//	counter++;
//	arr3[j++] = maxx;
//	for (int i = maxx; i >= 0; i--)
//	{
//		if (arr2[i] > 0){
//			arr2[i]--;
//			counter++;
//			arr3[j++] = i;
//		}
//	}
//	cout << counter << endl;
//	cout << arr3[0] ;
//	for (int i = 1; i < j; i++)
//	{
//		cout <<" " << arr3[i];
//	}
//	cout << endl;
//
//	return 0;
//}

#include<iostream>
#include<stdio.h>
#include<bitset>
#include<vector>
#include<cmath>
#include<algorithm>

using namespace std;
typedef vector<int> vi;
typedef long long ll;

int n ,m;
ll _sieve_size;
bitset<10000010> bs;
vi primes;
ll arr[501][501];
bool arr2[501][501];
int row[501];
int col[501];
void seive(ll upperbound){
	_sieve_size = upperbound + 1;
	bs.set();
	bs[0] = bs[1] = 0;
	for (ll i = 0; i < _sieve_size; i++)
	{
		if (bs[i]){
			for (ll j = i * i; j <= _sieve_size; j += i)
				bs[j] = 0;
			primes.push_back((int)i);
		}
	}
}
bool isPrime(ll N){
	if (N <= _sieve_size)
		return bs[N];
	for (int i = 0; i < (int)primes.size(); i++){
		if (N % primes[i] == 0)
			return false;
		return true;
	}
}
int main(){

	//freopen("in.txt", "r", stdin);
	int minn = 1000000000;
	seive(100000);
	cin >> n >> m;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			cin >> arr[i][j];
			if (isPrime(arr[i][j])){
				arr[i][j] = 0;
			}
			else{
			 vector<int>::iterator low = lower_bound(primes.begin(), primes.end(), arr[i][j]);
			 arr[i][j] = primes[low - primes.begin()] - arr[i][j];
				/*for (ll k = 0; k < _sieve_size; k++)
				{
					if (arr[i][j] != 0){
						if (primes[k] > arr[i][j])
						{
							arr[i][j] = primes[k] - arr[i][j];
							break;
						}
					}
				}*/
			}
			row[i] += arr[i][j];
			col[j] += arr[i][j];
		}

	}

	int min1 = *min_element(row, row + n);
	int min2 = *min_element(col, col + m);
	/*for (int i = 0; i < n; i++)
	{
		if (minn > row[i])
			minn = row[i];
	}

	for (int i = 0; i < m; i++)
	{
		if (minn > col[i]){
			minn = col[i];
		}
	}*/
	cout << min(min1,min2) << endl;
	return 0;
}

//#include<iostream>
//#include<stdio.h>
//#include<queue>
//
//using namespace std;
//
//typedef pair<int, int > ii;
//int n, m;
//bool vis[10001];
//void BFS(int start){
//	queue<ii>pq;
//	pq.push(make_pair(start, 1));
//	vis[start] = 1;
//	while (!pq.empty()){
//		ii x = pq.front(); pq.pop();
//		vis[x.first] = 1;
//
//		if (x.first * 2 == m || x.first - 1 == m)
//		{
//			cout << x.second<< endl;
//			break;
//		}
//		if (!vis[x.first * 2] && x.first * 2 <= 10000)
//		{
//			pq.push(make_pair(x.first * 2, x.second + 1));
//			vis[x.first * 2] = 1;
//		}
//		if (!vis[x.first - 1] && x.first - 1 > 0){
//			pq.push(make_pair(x.first - 1, x.second + 1));
//			vis[x.first -  1] = 1;
//		}
//	}
//}
//
//int main(){
//	freopen("in.txt", "r", stdin);
//	cin >> n >> m;
//	BFS(n);
//	return 0;
//}
//#include<iostream>
//#include<queue>
//#include <stdio.h>
//using namespace std;
//
//
//int adjList[2001][2001];
//bool vis[2001];
//bool vis2[2001];
//int n, a;
//int maxx = 0;
//int counter = 0;
//
//void BFS(int start){
//	queue<int>pq;
//	pq.push(start);
//	vis[start] = 1;
//	while (!pq.empty()){
//		int x = pq.front(); pq.pop();
//		counter++;
//		vis[x] = 1;
//		for (int i = 1; i <= n; i++)
//		{
//			if (!vis[i] && adjList[x][i] == 1){
//				pq.push(i);
//				cout << x << " " << i << endl;
//			}
//		}
//	}
//}
//
//void DFS(int start, int c){
//	if (maxx < c)
//		maxx = c;
//	vis[start] = 1;
//	c++;
//	for (int i = 1; i <= n; i++)
//	{
//		if (!vis[i] && adjList[start][i] == 1){
//			DFS(i,c);
//		}
//	}
//	vis[start] = 0;
//	c--;
//}
//
//
//int main(){
//	freopen("in.txt", "r", stdin);
//
//	cin >> n;
//	memset(vis, false, sizeof vis);
//	memset(vis2, false, sizeof vis);
//	counter = 0;
//	for (int i = 1; i <= n; i++)
//	{
//		cin >> a;
//		if (a == -1)
//			adjList[0][i] = 1;
//		else
//			adjList[a][i] = 1;
//	}
//	DFS(0,0);
//	cout << maxx << endl;
//	//int tc, n;
//	//cin >> tc;
//	//memset(vis, false, sizeof vis);
//	//while (tc--)
//	//{
//
//	//	cin >> n;
//	//	for (int i = 0; i < n; i++)
//	//	{
//	//		cin >> a >> b;
//	//		adjList[a][b] = 1;
//	//	}
//	//}
//
//	///*adjList[1][2] = 1;
//	//adjList[1][3] = 1;
//	//adjList[1][4] = 1;
//	//adjList[2][5] = 1;
//	//adjList[4][6] = 1;*/
//
//	//BFS(1);
//	return 0;
//}


#include<iostream>
#include<stdio.h>
#include<bitset>
#include<vector>
#include<cmath>
#include<algorithm>

using namespace std;
typedef vector<int> vi;
typedef long long ll;

int n ,m;
ll _sieve_size;
bitset<10000010> bs;
vi primes;
ll arr[501][501];
bool arr2[501][501];
int row[501];
int col[501];
void seive(ll upperbound){
	_sieve_size = upperbound + 1;
	bs.set();
	bs[0] = bs[1] = 0;
	for (ll i = 0; i < _sieve_size; i++)
	{
		if (bs[i]){
			for (ll j = i * i; j <= _sieve_size; j += i)
				bs[j] = 0;
			primes.push_back((int)i);
		}
	}
}
bool isPrime(ll N){
	if (N <= _sieve_size)
		return bs[N];
	for (int i = 0; i < (int)primes.size(); i++){
		if (N % primes[i] == 0)
			return false;
		return true;
	}
}
int main(){

	//freopen("in.txt", "r", stdin);
	int minn = 1000000000;
	seive(100000);
	cin >> n >> m;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			cin >> arr[i][j];
			if (isPrime(arr[i][j])){
				arr[i][j] = 0;
			}
			else{
			 vector<int>::iterator low = lower_bound(primes.begin(), primes.end(), arr[i][j]);
			 arr[i][j] = (primes[low - primes.begin()] - arr[i][j]);
				/*for (ll k = 0; k < _sieve_size; k++)
				{
					if (arr[i][j] != 0){
						if (primes[k] > arr[i][j])
						{
							arr[i][j] = primes[k] - arr[i][j];
							break;
						}
					}
				}*/
			}
			row[i] += arr[i][j];
			col[j] += arr[i][j];
		}

	}

	int min1 = *min_element(row, row + n);
	int min2 = *min_element(col, col + m);
	cout << min(min1,min2) << endl;
	return 0;
}