#include <bits/stdc++.h>
using namespace std;
template <typename T>
void _read(T& t);
template <typename T>
void _read(vector<T>& v);
template <typename T1, typename T2>
void _read(pair<T1, T2>& p);
template <typename T>
void _read(T& t) {
  cin >> t;
}
template <typename T>
void _read(vector<T>& v) {
  for (unsigned _i = 0; _i < v.size(); _i++) _read(v[_i]);
}
template <typename T1, typename T2>
void _read(pair<T1, T2>& p) {
  _read(p.first);
  _read(p.second);
}
void _masterread() {}
template <typename T, typename... V>
void _masterread(T& t, V&... v) {
  _read(t);
  _masterread(v...);
}
template <typename T>
void _print(T t);
template <typename T>
void _print(vector<T>& v);
template <typename T1, typename T2>
void _print(pair<T1, T2>& p);
template <typename T>
void _print(T t) {
  cout << t;
}
template <typename T>
void _print(vector<T>& v) {
  for (unsigned _i = 0; _i < v.size(); _i++)
    _print(v[_i]), cout << (_i == v.size() - 1 ? "" : " ");
}
template <typename T1, typename T2>
void _print(pair<T1, T2>& p) {
  _print(p.first);
  cout << " ";
  _print(p.second);
}
void _masterprint() { cout << '\n'; }
template <typename T, typename... V>
void _masterprint(T t, V... v) {
  _print(t);
  if (sizeof...(v)) cout << " ";
  _masterprint(v...);
}
template <typename T>
void _debug(T t);
template <typename T1, typename T2>
void _debug(pair<T1, T2> p);
template <typename T>
void _debug(vector<T> v);
template <typename T>
void _debug(T t) {
  cerr << t;
}
template <typename T1, typename T2>
void _debug(pair<T1, T2> p) {
  cerr << "{";
  _debug(p.first);
  cerr << ", ";
  _debug(p.second);
  cerr << "}";
}
template <typename T>
void _debug(vector<T> v) {
  cerr << "(";
  for (unsigned _i = 0; _i < v.size(); _i++)
    _debug(v[_i]), cerr << (_i == v.size() - 1 ? "" : ", ");
  cerr << ")";
}
void _masterdebug() { cerr << "]\n"; }
template <typename T, typename... V>
void _masterdebug(T t, V... v) {
  _debug(t);
  if (sizeof...(v)) cerr << ", ";
  _masterdebug(v...);
}
const long long M = 1e9 + 7;
vector<long long> P;
void find_primes(long long n) {
  vector<bool> cnt(n + 1, 0);
  for (long long i = 2; i <= n; i++) {
    for (long long j = i + i; j <= n; j += i) cnt[j] = true;
  }
  for (long long i = 2; i <= n; i++)
    if (!cnt[i]) P.push_back(i);
}
void solve() {
  long long n, m;
  _masterread(n, m);
  vector<vector<long long>> arr(n, vector<long long>(m));
  _masterread(arr);
  vector<vector<long long>> need(n, vector<long long>(m));
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < m; j++) {
      auto bst = lower_bound(P.begin(), P.end(), arr[i][j]);
      need[i][j] = *bst - arr[i][j];
    }
  }
  vector<long long> rows(n), cols(m);
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < m; j++) {
      rows[i] += need[i][j];
      cols[j] += need[i][j];
    }
  }
  long long mn = LLONG_MAX;
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < m; j++) {
      mn = min(mn, rows[i]);
      mn = min(mn, cols[j]);
    }
  }
  _masterprint(mn);
}
signed main() {
  find_primes(2e5);
  cin.sync_with_stdio(false), cin.tie(nullptr);
  solve();
}
