#include using namespace std; const long long INF = 1000000000000000; template struct binary_indexed_tree{ int N; vector BIT; binary_indexed_tree(){ } binary_indexed_tree(int N): N(N), BIT(N + 1, 0){ } void add(int i, T x){ i++; while (i <= N){ BIT[i] += x; i += i & -i; } } T sum(int i){ T ans = 0; while (i > 0){ ans += BIT[i]; i -= i & -i; } return ans; } T sum(int L, int R){ return sum(R) - sum(L); } }; int main(){ int M, K; cin >> M >> K; vector A(M * K); for (int i = 0; i < M * K; i++){ cin >> A[i]; } vector cnt(M, 0); vector p(M * K); vector> B(K); for (int i = 0; i < M * K; i++){ p[i] = cnt[A[i]]; B[cnt[A[i]]].push_back(A[i]); cnt[A[i]]++; } long long ans1 = 0; binary_indexed_tree BIT1(K); for (int i = 0; i < M * K; i++){ ans1 += BIT1.sum(p[i] + 1, K); BIT1.add(p[i], 1); } vector op(M, 0); for (int i = 0; i < K; i++){ vector pos(M); for (int j = 0; j < M; j++){ pos[B[i][j]] = j; } long long C = 0; binary_indexed_tree BIT2(M); for (int j = 0; j < M; j++){ C += BIT2.sum(B[i][j] + 1, M); BIT2.add(B[i][j], 1); } op[0] += C; for (int j = 0; j < M - 1; j++){ C -= pos[j]; C += M - 1 - pos[j]; op[j + 1] += C; } } long long ans2 = INF; for (int i = 0; i < M; i++){ ans2 = min(ans2, op[i]); } cout << ans1 + ans2 << endl; }