def main():
  global hl,n,st
  n = int(input())
  p = [int(i)-1 for i in input().split()]
  st = [[1]*n for _ in range(n)]
  ans = 0
  for i in p:
    ph,pw = (i//n,i%n)
    st[ph][pw] = 0
    if ph != 0 and pw != 0 and ph != n-1 and pw != n-1:
      hl = [[n]*n for _ in range(n)]
      hl[ph][pw] = 0
      f(ph,pw)
      hlmin = min(min(hl[0]),min(hl[-1]),min([min(j[0],j[-1]) for j in hl[1:-1]]))
      ans += hlmin
  print(ans)

def f(h,w):
  global hl,n,st
  if h == 0 or w == 0 or h == n-1 or w == n-1:
    return
  now = hl[h][w]
  for i in ((1,0),(-1,0),(0,1),(0,-1)):
    if hl[h+i[0]][w+i[1]] > now + st[h+i[0]][w+i[1]]:
      hl[h+i[0]][w+i[1]] = now + st[h+i[0]][w+i[1]]
      f(h+i[0],w+i[1])

main()