PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#SP0000051. Ma trận nguyên tố (Prime Matrix)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Ma trận nguyên tố (Prime Matrix)

Prime Matrix

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

Given an n×mn\times m matrix of positive integers, one operation increments a chosen cell by exactly 11. A matrix is prime if some entire row consists of prime numbers or some entire column consists of prime numbers. Find the minimum total number of operations required to make the matrix prime.

Input

The first line contains integers n,mn,m. Each of the next nn lines contains exactly mm integers ai,1,…,ai,ma_{i,1},\ldots,a_{i,m}, in column order.

Output

Print the minimum number of operations; print 0 if the matrix already meets the condition.

Subtasks

  • Subtask 1 (20 points): 1≤n≤31\le n\le 3; 1≤m≤31\le m\le 3; 1≤ai,j≤1000001\le a_{i,j}\le 100000 for every cell.
  • Subtask 2 (30 points): 1≤n≤501\le n\le 50; 1≤m≤501\le m\le 50; 1≤ai,j≤1000001\le a_{i,j}\le 100000 for every cell.
  • Subtask 3 (50 points): 1≤n≤5001\le n\le 500; 1≤m≤5001\le m\le 500; 1≤ai,j≤1000001\le a_{i,j}\le 100000 for every cell.

Examples

Input

3 3
1 2 3
5 6 1
4 4 1

Output

1

Explanation

In the first row, 22 and 33 are prime while 11 is not. Increment the entry at (1,1)(1,1) once from 11 to 22, producing the all-prime row 2,2,32,2,3. No row or column was all-prime initially, so the minimum is 1.