from sys import stdin

def checkans(N,arg,i):
    # 上をチェック
    uecount = 0
    bai = 1
    while (i + N * bai) < len(arg)-1:
        # print(i+N*bai)
        if arg[i+N*bai] != 0:
            uecount += 1
        bai += 1
    # 下をチェック
    shitacount = 0
    bai = 1
    while i - N * bai > 0:
        if arg[i - N * bai] != 0:
            shitacount += 1
        bai += 1
    # 右をチェック
    migicount = 0
    for a in range(i+1, N*(i//N+1)):
        if arg[a] != 0:
            migicount += 1
    # 左をチェック
    hidaricount = 0
    for a in range(N*(i//N),i):
        if arg[a] != 0:
            hidaricount += 1
    # print(f'uecount,shitacount,migicount,hidaricount {uecount} {shitacount} {migicount} {hidaricount}')
    return min(uecount, shitacount, migicount, hidaricount)

N = int(input())
P = list(map(int,stdin.readline().split()))
seki = [a+1 for a in range(N**2)]

ans = 0
for a,b in enumerate(P):
    ans += checkans(N,seki,b-1)
    seki[P[a]-1] = 0
    # print(f'ans = {ans}')
    # print(f'P = {seki}')

print(ans)
