import sys
input = sys.stdin.readline

N = int(input())
P = list(map(int, input().split()))
seat = [False] * N ** 2
ans = 0

for p in P:
  p -= 1
  seat[p] = True
  x = p % N
  y = p // N
  seatX = [n for n in range(x, N**2, N)]
  seatY = [n for n in range(N*y, N*(y+1))]
  up = []
  down = []
  right = []
  left = []
  for sx in seatX:
    if sx < p:
      up.append(seat[sx])
    elif sx > p:
      down.append(seat[sx])
  for sy in seatY:
    if sy < p:
      left.append(seat[sy])
    elif sy > p:
      right.append(seat[sy])
  upc = up.count(False)
  downc = down.count(False)
  rightc = right.count(False)
  leftc = left.count(False)
  move = min(upc, downc, rightc, leftc)
  ans += move


print(ans)