#include #include int main() { int N; std::cin >> N; std::vector> A(N, std::vector(N)); std::vector b(N), c(N); for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) { std::cin >> A[i][j]; A[i][j]--; b[A[i][j]] += std::max(i, j); c[A[i][j]] += std::max(i, j); } for (int k = 1; k < N; k++) { for (int i = 0; i < k; i++) for (int j = 0; j < k - i; j++) b[A[i][j]]++; for (int i = k; i < N; i++) for (int j = 0; j <= i - k; j++) b[A[i][j]]--; for (int i = 0; i < N; i++) c[i] = std::min(c[i], b[i]); } int ans = 0; for (int i = 0; i < N; i++) ans += c[i]; std::cout << ans << "\n"; }