n = int(input())
p = list(map(int, input().split()))
a = [[1]*n for i in range(n)]

ans = 0
for i in range(len(p)):
    # print(a)
    p[i] -= 1
    a[p[i] // n][p[i] % n] = 0
    s1 = 0;
    for j in range(p[i] % n, n):
        s1 += a[p[i] // n][j]
    s2 = 0
    for j in range(p[i] % n, -1, -1):
        s2 += a[p[i] // n][j]
    s3 = 0
    for j in range(p[i] // n, n):
        s3 += a[j][p[i] % n]
    s4 = 0
    for j in range(p[i] // n, -1, -1):
        s4 += a[j][p[i] % n]
    ans += min(s1, s2, s3, s4)
print(ans)