eolymp
bolt
Try our new interface for solving problems
Problems

Immovable Elements of a Matrix

Immovable Elements of a Matrix

Time limit 1 second
Memory limit 64 MiB

Any matrix A, that contains N rows and M columns, occupies NxM cells in computer memory – as well as any matrix B, that contains M rows и N columns. Rows are indexed with integers from 0 to N-1, columns – with integers from 0 to M-1. Matrices are stored in computer memory in row-major order, which for the matrix A means such a sequence: A[0,0], A[0,1], ..., A[0,M-1], A[1,0], A[1,1], ..., A[N-1,M-1], so that adjacent elements of the sequence occupy adjacent cells of memory. Say that a matrix B is a transposed matrix A, if B[i,j]=A[j,i] for any permissible i and j. Clearly, the matrix A when transposed can be put in the matrix A itself. Then the transposition can be thought of as a permutation of some elements of the matrix A. It is also clear that not all elements of the matrix will really move some stay immovable. For example, transposition of any matrix leaves the first element of the first row and the last element of the last row immovable.

Examples

Input example #1
2
3 3
3 5
Output example #1
3
3