You've got an *n* × *m* matrix. The matrix consists of integers. In one move, you can apply a single transformation to the matrix: choose an arbitrary element of the matrix and increase it by 1. Each element can be increased an arbitrary number of times.

You are really curious about prime numbers. Let us remind you that a prime number is a positive integer that has exactly two distinct positive integer divisors: itself and number one. For example, numbers 2, 3, 5 are prime and numbers 1, 4, 6 are not. 

A matrix is prime if at least one of the two following conditions fulfills:
 -  the matrix has a row with prime numbers only; -  the matrix has a column with prime numbers only; 
Your task is to count the minimum number of moves needed to get a prime matrix from the one you've got.
The first line contains two integers *n*, *m* (1 ≤ *n*, *m* ≤ 500) — the number of rows and columns in the matrix, correspondingly.

Each of the following *n* lines contains *m* integers — the initial matrix. All matrix elements are positive integers. All numbers in the initial matrix do not exceed 10<sup class="upper-index">5</sup>.

The numbers in the lines are separated by single spaces.
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;

vector<int> sieve_of_eratosthenes(int limit) {
    vector<int> primes;
    vector<bool> is_prime(limit + 1, true);
    for (int p = 2; p <= limit; ++p) {
        if (is_prime[p]) {
            primes.push_back(p);
            for (int i = p * p; i <= limit; i += p) {
                is_prime[i] = false;
            }
        }
    }
    return primes;
}

int main() {
    int n, m;
    cin >> n >> m;

    vector<vector<int>> matrix(n, vector<int>(m));
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            cin >> matrix[i][j];
        }
    }

    vector<int> primes = sieve_of_eratosthenes(100000);
    int min_moves = 1e9; // Initialize to a large value

    
    for (const auto& row : matrix) {
        int row_moves = 0;
        for (const auto& num : row) {
            auto it = lower_bound(primes.begin(), primes.end(), num);
            row_moves += *it - num;
        }
        min_moves = min(min_moves, row_moves);
    }


    for (int j = 0; j < m; ++j) {
        int col_moves = 0;
        for (int i = 0; i < n; ++i) {
            auto it = lower_bound(primes.begin(), primes.end(), matrix[i][j]);
            col_moves += *it - matrix[i][j];
        }
        min_moves = min(min_moves, col_moves);
    }

    cout << min_moves << endl;

    return 0;
}

		 			  	  		       				  	 			