{"raw_statement":[{"iden":"problem statement","content":"An integer sequence where no two adjacent elements are the same is called a **good sequence**.\nYou are given two good sequences of length $N$: $A=(A_1,A_2,\\dots,A_N)$ and $B=(B_1,B_2,\\dots,B_N)$. Each element of $A$ and $B$ is between $0$ and $M-1$, inclusive.\nYou can perform the following operations on $A$ any number of times, possibly zero:\n\n*   Choose an integer $i$ between $1$ and $N$, inclusive, and perform one of the following:\n    *   Set $A_i \\leftarrow (A_i + 1) \\bmod M$.\n    *   Set $A_i \\leftarrow (A_i - 1) \\bmod M$. Here, $(-1) \\bmod M = M - 1$.\n\nHowever, you cannot perform an operation that makes $A$ no longer a good sequence.\nDetermine if it is possible to make $A$ equal to $B$, and if it is possible, find the minimum number of operations required to do so."},{"iden":"constraints","content":"*   $2 \\leq N \\leq 2 \\times 10^5$\n*   $2 \\leq M \\leq 10^6$\n*   $0\\leq A_i,B_i< M(1\\leq i\\leq N)$\n*   $A_i\\ne A_{i+1}(1\\leq i\\leq N-1)$\n*   $B_i\\ne B_{i+1}(1\\leq i\\leq N-1)$\n*   All input values are integers."},{"iden":"input","content":"The input is given from Standard Input in the following format:\n\n$N$ $M$\n$A_1$ $A_2$ $\\dots$ $A_N$\n$B_1$ $B_2$ $\\dots$ $B_N$"},{"iden":"sample input 1","content":"3 9\n2 0 1\n4 8 1"},{"iden":"sample output 1","content":"3\n\nYou can achieve the goal in three operations as follows:\n\n*   Set $A_1 \\leftarrow (A_1 + 1) \\bmod M$. Now $A = (3, 0, 1)$.\n*   Set $A_2 \\leftarrow (A_2 - 1) \\bmod M$. Now $A = (3, 8, 1)$.\n*   Set $A_1 \\leftarrow (A_1 + 1) \\bmod M$. Now $A = (4, 8, 1)$.\n\nIt is impossible to achieve the goal in two or fewer operations, so the answer is 3.\nFor example, you cannot set $A_2 \\leftarrow (A_2 + 1) \\bmod M$ in the first operation, because it would make $A = (2, 1, 1)$, which is not a good sequence."},{"iden":"sample input 2","content":"3 9\n1 8 2\n1 8 2"},{"iden":"sample output 2","content":"0\n\n$A$ and $B$ might be equal from the beginning."},{"iden":"sample input 3","content":"24 182\n128 115 133 52 166 92 164 119 143 99 54 162 86 2 59 166 24 78 81 5 109 67 172 99\n136 103 136 28 16 52 2 85 134 64 123 74 64 28 85 161 19 74 14 110 125 104 180 75"},{"iden":"sample output 3","content":"811"}],"translated_statement":null,"sample_group":[],"show_order":["default"],"formal_statement":null,"simple_statement":null,"has_page_source":true}