cin = open(0).read().strip().split('\n')
n = int(cin[0])
p = list(map(int, cin[1].split(' ')))

from collections import deque

import numpy as np
seet = np.arange(n*n).reshape(n,n)
space = np.ones(n*n).reshape(n,n)

dx = [1, 0, -1, 0]
dy = [0, -1, 0, 1]

ans = 0
for pi in p:
    row, col = divmod(pi-1, n)
    dist = np.full(n*n, -1).reshape(n,n)
    ret = list()
    q = deque()
    q.append((row, col))
    dist[row, col] = 0
    while q:
        ph, pw = q.popleft()
        for dyi, dxi in zip(dy,dx):
            if 0<=ph+dyi<n and 0<=pw+dxi<n:
                if dist[ph+dyi,pw+dxi] == -1:
                    q.append((ph+dyi,pw+dxi))
                    dist[ph+dyi,pw+dxi] = dist[ph,pw]+int(space[ph+dyi,pw+dxi]==1)
            else:
                ret.append(dist[ph,pw])
    ans += min(ret)
    space[row, col] = 0

print(int(ans))