
N = int(input())
P = list(map(int, input().split()))

weight = [[0] * (N) for _ in range(N)]
path = [[1] * (N) for _ in range(N)]

for i in P:
    weight[(i-1)//N][(i-1)%N] = min((i-1)//N, N-(i-1)//N-1, (i-1)%N, N-(i-1)%N-1)

def dfs(h, w):
    try:
        f = True if weight[h][w] < weight[h][w +1] else False
        weight[h][w +1] = min(weight[h][w], weight[h][w +1])
        if (path[h][w +1] == 0) and f:
            dfs(h, w +1)
    except:
        pass
    try:
        f = True if weight[h][w] < weight[h][w -1] else False
        weight[h][w -1] = min(weight[h][w], weight[h][w -1])
        if (path[h][w -1] == 0) and f:
            dfs(h, w -1)
    except:
        pass
    try:
        f = True if weight[h][w] < weight[h +1][w] else False
        weight[h +1][w] = min(weight[h][w], weight[h +1][w])
        if (path[h +1][w] == 0) and f:
            dfs(h +1, w)
    except:
        pass
    try:
        f = True if weight[h][w] < weight[h -1][w] else False
        weight[h -1][w] = min(weight[h][w], weight[h][w])
        if (path[h -1][w] == 0) and f:
            dfs(h -1, w)
    except:
        pass
        
count = 0
for i in P:
    dfs((i-1)//N, (i-1)%N)

    path[(i-1)//N][(i-1)%N] = 0

    """
    print(weight[(i-1)//N][(i-1)%N])
    for j in range(N):
        print(weight[j], path[j])
    """
        
    count += weight[(i-1)//N][(i-1)%N]
    
print(count)