from copy import deepcopy n = int(input()) a = [list(map(int, input().split())) for i in range(n)] ans = [0 for i in range(n)] imos = [[0 for i in range(n)] for i in range(n)] for i in range(n): for j in range(n): imos[0][a[i][j] - 1] += max(i, j) ans = imos[0] for i in range(1, n): imos[i] = deepcopy(imos[i - 1]) for j in range(n): for k in range(n): if j - i < 0 and abs(j - i) <= k: break if j - i >= 0 and abs(j - i) < k: break if j - i < 0: imos[i][a[j][k] - 1] += 1 else: imos[i][a[j][k] - 1] -= 1 for j in range(n): ans[j] = min(imos[i][j], ans[j]) print(sum(ans))