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


M = 500

import numpy as np
pot = np.full((N+2, N+2), M)
pot[0,: ] = -1
pot[-1,:] = -1
pot[:, 0] = -1
pot[:, -1]= -1

def rep(X):
    i = 1
    while i <= X:
        yield (X+1)*((i+1)%2) + (i+1)//2 * (-1)**((i+1)%2)
        i += 1

def localmin(a, i, j):
    a[i, j] = min(a[i-1, j], a[i, j-1], a[i, j+1], a[i+1, j]) + 1
    
for i in rep(N):
    for j in rep(N):
        localmin(pot, i, j)

def leave(a, i, j):
    c = a[i, j]
    localmin(a, i, j)
    return c


cost = 0

for p in P:
    i = (p-1)//N + 1
    j = (p-1)%N + 1
    cost += leave(pot, i,j)

print(cost)