from copy import deepcopy, copy # imosじゃない n = int(input()) a = [list(map(int, input().split())) for i in range(n)] ans = [0 for i in range(n)] imo = [0 for i in range(n)] tmp = [0 for i in range(n)] for i in range(n): for j in range(n): imo[a[i][j] - 1] += max(i, j) if i >= j: tmp[a[i][j] - 1] -= 1 ans = copy(imo) # print(tmp) for i in range(1, n): for j in range(n): if j - i + 1 == 0: tmp[a[i - 1][0] - 1] += 2 elif j - i + 1 < 0: tmp[a[j][abs(j - i + 1)] - 1] += 1 else: tmp[a[j][abs(j - i + 1)] - 1] += 1 for j in range(n): imo[j] += tmp[j] ans[j] = min(imo[j], ans[j]) print(sum(ans))