from collections import deque
def main():
    n = int(input())
    p = list(map(int, input().split()))
    for i in range(len(p)):
        p[i] -= 1
    
    to_outsides = [[0]*n for _ in range(n)]
    not_exist = [[False]*n for _ in range(n)]
    
    for i in range(n):
        for j in range(n):
            to_outside = [i,n-1-i,j,n-1-j]
            to_outsides[i][j] = min(to_outside)
    
    directions = [[-1,0],[1,0],[0,-1],[0,1]]

    answer = 0
    for i, person in enumerate(p):
        exited = [person//n, person%n]
        answer += to_outsides[exited[0]][exited[1]]
        not_exist[exited[0]][exited[1]] = True
        seen = []
        seen.append([exited[0],exited[1]])
        todo = deque([exited])
        renews = []
        while len(todo)>0:
            now = todo.pop()
            now_to_outside = to_outsides[now[0]][now[1]]
            renews.append(now)
            for direction in directions:
                try:
                    checking = [now[0]+direction[0],now[1]+direction[1]]
                    checking_to_outside = to_outsides[checking[0]][checking[1]]
                except:
                    continue
                if [checking[0],checking[1]] not in seen:
                    if ((not_exist[checking[0]][checking[1]] == True and checking_to_outside==now_to_outside)or\
                     checking_to_outside>now_to_outside):
                        todo.append([checking[0],checking[1]])
                        seen.append([checking[0],checking[1]])
                        
        for renew in renews:
            if to_outsides[renew[0]][renew[1]] != 0:
                to_outsides[renew[0]][renew[1]]-=1
    print(answer)
main()