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

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

ans = 0
for pi in p:
    row, col = divmod(pi-1, n)
    hor = min(dist[:row, col].sum(), dist[row+1:, col].sum())
    var = min(dist[row, :col].sum(), dist[row, col+1:].sum())
    if row==1 and col==1:
        aaaa = dist
    ans += min(hor, var)
    dist[row][col] = 0

print(int(ans))