n = int(input())
seat = [[True]*n for i in range(n)]
lst = list(map(int, input().split()))

def place(m):
  m -= 1
  return [m//n, m%n]

ans = 0
for i in range(n**2):
  h = place(lst[i])
  seat[h[0]][h[1]] = False
  score = float('inf')
  
  can = 0
  for j in range(h[0]+1):
    if seat[h[0]-j][h[1]]:
      can += 1
  score = min(score, can)
  
  can = 0
  for j in range(n-h[0]):
    if seat[h[0]+j][h[1]]:
      can += 1
  score = min(score, can)
  
  can = 0
  for j in range(h[1]+1):
    if seat[h[0]][h[1]-j]:
      can += 1
  score = min(score, can)
  
  can = 0
  for j in range(n-h[1]):
    if seat[h[0]][h[1]+j]:
      can += 1
  score = min(score, can)
  
  ans += score

print(ans)

