import numpy as np
n = int(input())
P = list(map(int,input().split()))
zaseki = np.array([[1] * n for i in [1]*n])
ans = 0
for i in P:
    x = i%n-1
    y = (i-1)//n
    zaseki[y][x] = 0
    ans += min(sum(zaseki[y,:1+x]), sum(zaseki[y,x:]), sum(zaseki[:y+1,x]), sum(zaseki[y:,x]))
print(ans)