from collections import deque

N = int(input())
offset = 0
field = N
people = [[1 for _ in range(field)] for _ in range(field)]
HL = [0] *N
HR = [0]*N
Hcnt = 0
for P in input().split():
    visited = [[600 for _ in range(field)] for _ in range(field)]
    seen = [[0 for _ in range(field)] for _ in range(field)]

    nowx = (int(P)-1)//N
    nowy = (int(P)-1)%N
    todo = deque()
    todo.append((nowx,nowy))
    mv = [(0,1),(1,0),(-1,0),(0,-1)]

    seen[nowx][nowy] += 1
    people[nowx][nowy] = 0
    visited[nowx][nowy] = 0
    flag = False
    while len(todo) > 0:
        nowx, nowy = todo.popleft()
        for i in range(len(mv)):
            dx, dy = mv[i]
            nx = nowx + dx 
            ny = nowy + dy

            if nx >= N or nx < 0 or ny >= N or ny < 0: 
                    continue               
            if visited[nx][ny] > visited[nowx][nowy] + people[nx][ny]:
                visited[nx][ny] = visited[nowx][nowy] + people[nx][ny]
            seen[nx][ny] +=1
            if seen[nx][ny] > 3:continue
            todo.append((nx,ny))
        if flag:
            break

    for n in range(N):
        HL[n] = visited[n][0]
        HR[n] = visited[n][N-1]
    Hcnt += min(min(visited[0]),min(visited[N-1]),min(HL),min(HR))

print(Hcnt)