{"raw_statement":[{"iden":"problem statement","content":"You are given two grids, A and B, each with $H$ rows and $W$ columns.\nFor each pair of integers $(i, j)$ satisfying $1 \\leq i \\leq H$ and $1 \\leq j \\leq W$, let $(i, j)$ denote the cell in the $i$\\-th row and $j$\\-th column. In grid A, cell $(i, j)$ contains the integer $A_{i, j}$. In grid B, cell $(i, j)$ contains the integer $B_{i, j}$.\nYou will repeat the following operation any number of times, possibly zero. In each operation, you perform one of the following:\n\n*   Choose an integer $i$ satisfying $1 \\leq i \\leq H-1$ and swap the $i$\\-th and $(i+1)$\\-th rows in grid A.\n*   Choose an integer $i$ satisfying $1 \\leq i \\leq W-1$ and swap the $i$\\-th and $(i+1)$\\-th columns in grid A.\n\nDetermine whether it is possible to make grid A identical to grid B by repeating the above operation. If it is possible, print the minimum number of operations required to do so.\nHere, grid A is identical to grid B if and only if, for all pairs of integers $(i, j)$ satisfying $1 \\leq i \\leq H$ and $1 \\leq j \\leq W$, the integer written in cell $(i, j)$ of grid A is equal to the integer written in cell $(i, j)$ of grid B."},{"iden":"constraints","content":"*   All input values are integers.\n*   $2 \\leq H, W \\leq 5$\n*   $1 \\leq A_{i, j}, B_{i, j} \\leq 10^9$"},{"iden":"input","content":"The input is given from Standard Input in the following format:\n\n$H$ $W$\n$A_{1, 1}$ $A_{1, 2}$ $\\cdots$ $A_{1, W}$\n$A_{2, 1}$ $A_{2, 2}$ $\\cdots$ $A_{2, W}$\n$\\vdots$\n$A_{H, 1}$ $A_{H, 2}$ $\\cdots$ $A_{H, W}$\n$B_{1, 1}$ $B_{1, 2}$ $\\cdots$ $B_{1, W}$\n$B_{2, 1}$ $B_{2, 2}$ $\\cdots$ $B_{2, W}$\n$\\vdots$\n$B_{H, 1}$ $B_{H, 2}$ $\\cdots$ $B_{H, W}$"},{"iden":"sample input 1","content":"4 5\n1 2 3 4 5\n6 7 8 9 10\n11 12 13 14 15\n16 17 18 19 20\n1 3 2 5 4\n11 13 12 15 14\n6 8 7 10 9\n16 18 17 20 19"},{"iden":"sample output 1","content":"3\n\nSwapping the fourth and fifth columns of the initial grid A yields the following grid:\n\n1 2 3 5 4\n6 7 8 10 9\n11 12 13 15 14\n16 17 18 20 19\n\nThen, swapping the second and third rows yields the following grid:\n\n1 2 3 5 4\n11 12 13 15 14\n6 7 8 10 9\n16 17 18 20 19\n\nFinally, swapping the second and third columns yields the following grid, which is identical to grid B:\n\n1 3 2 5 4\n11 13 12 15 14\n6 8 7 10 9\n16 18 17 20 19\n\nYou can make grid A identical to grid B with the three operations above and cannot do so with fewer operations, so print $3$."},{"iden":"sample input 2","content":"2 2\n1 1\n1 1\n1 1\n1 1000000000"},{"iden":"sample output 2","content":"\\-1\n\nThere is no way to perform the operation to make grid A match grid B, so print `-1`."},{"iden":"sample input 3","content":"3 3\n8 1 6\n3 5 7\n4 9 2\n8 1 6\n3 5 7\n4 9 2"},{"iden":"sample output 3","content":"0\n\nGrid A is already identical to grid B at the beginning."},{"iden":"sample input 4","content":"5 5\n710511029 136397527 763027379 644706927 447672230\n979861204 57882493 442931589 951053644 152300688\n43971370 126515475 962139996 541282303 834022578\n312523039 506696497 664922712 414720753 304621362\n325269832 191410838 286751784 732741849 806602693\n806602693 732741849 286751784 191410838 325269832\n304621362 414720753 664922712 506696497 312523039\n834022578 541282303 962139996 126515475 43971370\n152300688 951053644 442931589 57882493 979861204\n447672230 644706927 763027379 136397527 710511029"},{"iden":"sample output 4","content":"20"}],"translated_statement":null,"sample_group":[],"show_order":["default"],"formal_statement":null,"simple_statement":null,"has_page_source":true}