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

p2i = {}

for i in range(N*N):
  p2i[i] = [(P[i]-1)//N, (P[i]-1)%N]
print(p2i)

table = [[0 for j in range(N)] for i in range(N)]
for i in range(N*N):
    table[p2i[i][0]][p2i[i][1]] = i
print(table)

S = 0

for n in range(N*N):
  above = 0
  below = 0
  left = 0
  right = 0

  if p2i[n][0] != 0 and p2i[n][0] != 0:
    for i in range(p2i[n][0]):
      above += (table[i][p2i[n][1]] != 0)
    for i in range(p2i[n][0]+1, N):
      below += (table[i][p2i[n][1]] != 0)
  
  if p2i[n][1] != 0 and p2i[n][1] != 0:
    for i in range(p2i[n][1]):
      left += (table[p2i[n][0]][i] != 0)
    for i in range(p2i[n][1]+1, N):
      right += (table[p2i[n][0]][i] != 0)

  S += min(above, below, left, right)
  table[p2i[n][0]][p2i[n][1]] = 0

print(S)