#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-6;
const int PRECISION = 20;
const int MOD = 1e9 + 7;
struct node {
  long long val;
  vector<long long> formula;
  node() { val = -1; }
};
struct group {
  long long mul, last, gcd;
  group(long long m, long long l, long long lm) {
    mul = m;
    last = l;
    gcd = lm;
  }
};
bool comp(pair<int, int> p1, pair<int, int> p2) {
  return p1.second < p2.second;
}
int bs(vector<int> vc, int target) {
  int low = 0, high = vc.size() - 1;
  while (high > low) {
    int mid = ((high - low + 1) / 2) + low;
    if (vc[mid] <= target)
      low = mid;
    else
      high = mid - 1;
  }
  return low;
}
long long gcd(long long a, long long b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
long long fastPow(long long a, long long n) {
  if (n == 0) return 1;
  if (n == 1) return a;
  long long x = fastPow(a, n / 2);
  x = (x * x);
  return (n % 2 == 0 ? x : (x * a));
}
int char2index(char a) { return a >= 'a' ? a - 'a' : a - 'A' + 26; }
int flipCase(char a) {
  return a >= 'a' ? char2index(a - ('a' - 'A')) : char2index(a + ('a' - 'A'));
}
void prefix(vector<long long> &arr) {
  for (int i = 1; i < arr.size(); i++) arr[i] += arr[i - 1];
}
long long eval(vector<long long> &coeffs, long long x) {
  long long ans = 0;
  long long xs = 1;
  for (long long i = coeffs.size() - 1; i >= 0; i--) {
    ans += (coeffs[i] * xs);
    xs *= x;
  }
  return ans;
}
int bracketToInt(char a) {
  switch (a) {
    case '(':
      return 1;
    case ')':
      return -1;
    case '{':
      return 2;
    case '}':
      return -2;
    case '[':
      return 3;
    case ']':
      return -3;
    case '<':
      return 4;
    case '>':
      return -4;
    default:
      return -1;
  }
}
long long findOpposite(long long x, long long y, long long n) {
  long long ans = 0;
  if (x - 2 >= 1) {
    if (y - 1 >= 1) ans++;
    if (y + 1 < n) ans++;
  }
  if (x + 2 <= n) {
    if (y - 1 >= 1) ans++;
    if (y + 1 <= n) ans++;
  }
  if (y - 2 >= 1) {
    if (x + 1 <= n) ans++;
    if (x - 1 >= 1) ans++;
  }
  if (y + 2 < n) {
    if (x + 1 <= n) ans++;
    if (x - 1 >= 1) ans++;
  }
  return ans;
}
vector<vector<long long>> ans;
long long solve(long long col, set<long long> cols, set<long long> rows,
                set<long long> diff, set<long long> diff2,
                vector<long long> soFar) {
  if (cols.count(col)) col++;
  if (col > 8) {
    ans.push_back(soFar);
    return 1;
  }
  long long i = col;
  cols.insert(i);
  for (long long j = 1; j <= 8; j++) {
    if (rows.count(j) || diff.count(j - i) || diff2.count(j - (9 - i)))
      continue;
    rows.insert(j);
    diff.insert(j - i);
    diff2.insert(j - (9 - i));
    soFar.push_back(j);
    solve(i + 1, cols, rows, diff, diff2, soFar);
    soFar.pop_back();
    rows.erase(j);
    diff.erase(j - i);
    diff2.erase(j - (9 - i));
  }
  cols.erase(col);
  return 1;
}
long long bs(const vector<long long> &v, long long target) {
  long long low = 0;
  long long high = v.size() - 1;
  while (low < high) {
    long long mid = ((high - low) / 2) + low;
    if (v[mid] < target)
      low = mid + 1;
    else
      high = mid;
  }
  return low;
}
bool customComp(pair<long long, long long> p1, pair<long long, long long> p2) {
  return p1.second < p2.second;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long t;
  t = 1;
  while (t--) {
    long long x, y;
    cin >> x >> y;
    vector<long long> primes;
    vector<long long> rows(x, 0);
    vector<long long> cols(y, 0);
    vector<bool> nums(100022, false);
    for (long long i = 2; i <= (sqrt(100022) + 1); i++) {
      if (!nums[i]) {
        for (long long j = i * i; j < 100022; j += i) {
          nums[j] = true;
        }
      }
    }
    for (int i = 2; i < 100022; i++) {
      if (!nums[i]) primes.push_back(i);
    }
    for (long long i = 0; i < x; i++) {
      for (long long j = 0; j < y; j++) {
        long long d;
        cin >> d;
        long long diff = primes[bs(primes, d)] - d;
        rows[i] += diff;
        cols[j] += diff;
      }
    }
    long long ans = LLONG_MAX;
    for (long long i = 0; i < x; i++) {
      ans = std::min(ans, rows[i]);
    }
    for (long long i = 0; i < y; i++) {
      ans = std::min(ans, cols[i]);
    }
    cout << ans;
    return 0;
  }
}
