#include <bits/stdc++.h>
using namespace std;
int n, m, a;
int step[505][505];
int prime[100005];
void IsPrime() {
  int i;
  prime[0] = prime[1] = 0;
  prime[2] = 1;
  for (i = 3; i <= 100005; i++) prime[i] = i % 2 == 0 ? 0 : 1;
  int t = (int)sqrt(100005 * 1.0);
  for (i = 3; i <= t; i++)
    if (prime[i])
      for (int j = i * i; j < 100005; j += 2 * i) prime[j] = 0;
}
int f(int n) {
  if (prime[n] == 1)
    return 0;
  else
    for (int i = n; i <= 100005; i++)
      if (prime[i] == 1) return i - n;
}
int main() {
  IsPrime();
  while (scanf("%d%d", &n, &m) != EOF) {
    memset(step, 0, sizeof(step));
    int i, j;
    for (i = 1; i <= n; i++)
      for (j = 1; j <= m; j++) {
        scanf("%d", &a);
        step[i][j] = f(a);
      }
    int sum, Min;
    sum = 0;
    Min = 99999999;
    for (i = 1; i <= n; i++) {
      sum = 0;
      for (j = 1; j <= m; j++) sum += step[i][j];
      if (sum < Min) Min = sum;
    }
    for (j = 1; j <= m; j++) {
      sum = 0;
      for (i = 1; i <= n; i++) sum += step[i][j];
      if (sum < Min) Min = sum;
    }
    printf("%d\n", Min);
  }
  return 0;
}
