n = int(input())
ps = list(map(int, input().split()))
rem_num = {i: {"l": 0, "r": 0, "u": 0, "b": 0} for i in range(1, n*n+1)}

for i in range(1, n*n+1):
    rem_num[i]["u"] = (i-1)//n
    rem_num[i]["b"] = n - (i-1)//n - 1
    rem_num[i]["l"] = (i-1) % n
    rem_num[i]["r"] = n - (i-1) % n - 1

ans = 0
for p in ps:
    # print(f"{p} {rem_num[p]} {min(rem_num[p].values())}")
    ans += min(rem_num[p].values())
    for i in range(p + n, n*n+1, n):
        rem_num[i]["u"] -= 1
    for i in range(p - n, 0, -n):
        rem_num[i]["b"] -= 1
    for i in range(p + 1, n*((p-1)//n + 1)):
        rem_num[i]["l"] -= 1
    for i in range(p - 1, n*((p-1)//n), -1):
        rem_num[i]["r"] -= 1

print(ans)
