import sys
from collections import deque
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    N = int(readline())
    P = list(map(int, readline().split()))
    seat = [[1]*N for _ in range(N)]
    dx = [1,0,-1,0]
    dy = [0,1,0,-1]
    ans = 0
    for i in range(N**2):
        person = P[i]-1
        sy = person//N
        sx = person%N
        seat[sy][sx] = 0
        if sy==0 or sy==N-1 or sx==0 or sx==N-1:
            continue
        hate = [[INF]*N for _ in range(N)]
        hate[sy][sx]=0
        d = deque([[sy,sx]])
        while d:
            q = d.popleft()
            y = q[0]
            x = q[1]
            if y==0 or y==N-1 or x==0 or x==N-1:
                continue
            for i in range(4):
                ny = y + dy[i]
                nx = x + dx[i]
                if 0<ny<N-1 and 0<nx<N-1:
                    if hate[ny][nx]>hate[y][x]+seat[ny][nx]:
                        hate[ny][nx] = hate[y][x] + seat[ny][nx]
                        d.append((ny,nx))
                else:
                    if hate[ny][nx]>hate[y][x]+seat[ny][nx]:
                        hate[ny][nx] = hate[y][x] + seat[ny][nx]
        tmp = min(min(hate[0]),min(hate[N-1]),min([x[0] for x in hate]),min([x[N-1] for x in hate]))
        ans += tmp
    print(ans)

if __name__ == '__main__':
    main()
