import sequtils, strutils, algorithm, tables

type PriorityQueue[T] = object
  data:seq[T]
  comp:proc(x,y:T):int
proc initPriorityQueue[T]():PriorityQueue[T] =
  PriorityQueue[T](data:newSeq[T](),
                   comp:system.cmp[T])
proc initPriorityQueue[T](comp:proc(x,y:T):int):PriorityQueue[T] =
  PriorityQueue[T](data:newSeq[T](),
                   comp:comp)
proc `[]`[T](q: PriorityQueue[T], n: int): T = q.data[n]
proc `[]=`[T](q: var PriorityQueue[T], n: int, item: T) = q.data[n] = item
proc len[T](q:PriorityQueue[T]): int = len(q.data)
proc shiftup[T](q: var PriorityQueue[T], idx:int) =
  var i = idx
  while i != 0:
    let p = (i - 1) shr 1
    if q.comp(q[i], q[p]) >= 0:
      break
    swap(q.data[i], q.data[p])
    i = p
proc shiftdown[T](q: var PriorityQueue[T], idx: int) =
  var i = idx
  let n = len(q)
  while 2*i + 1 < n:
    var
      a = 2*i + 1
      b = 2*i + 2
    if b < n and q.comp(q[a], q[b]) > 0:
      a = b
    if q.comp(q[i], q[a]) <= 0:
      break
    swap(q.data[a], q.data[i])
    i = a
proc push[T](q: var PriorityQueue[T], item: T) =
  let n = len(q)
  q.data.add(item)
  shiftup(q, n)
proc pop[T](q: var PriorityQueue[T]): T =
  swap(q.data[0], q.data[^1])
  result = q.data.pop
  shiftdown(q, 0)

const
  dxs = [1, 0, -1, 0]
  dys = [0, 1, 0, -1]
let
  n = stdin.readline.parseInt
  ps = stdin.readline.split.map(parseInt)
var
  bs = newSeq[bool](n * n)
  ans = 0

for p in reversed(ps):
  var
    q = initPriorityQueue[(int, int)]()
    memo = initTable[int, int]()
  q.push((0, p-1))
  while len(q) > 0:
    let
      (c, z) = q.pop
      x = z mod n
      y = z div n
    if x == 0 or x == n-1 or y == 0 or y == n-1:
      inc ans, c
      break
    if z in memo and memo[z] <= c: # or c >= min([x, n-1-x, y, n-1-y]):
      continue
    memo[z] = c
    for i in 0..<4:
      let
        dx = dxs[i]
        dy = dys[i]
      if x + dx < 0 or x + dx >= n or  y + dy < 0 or y + dy >= n:
          continue
      if bs[(x + dx) + (y + dy) * n]:
        q.push((c+1, (x + dx) + (y + dy) * n))
      else:
        q.push((c, (x + dx) + (y + dy) * n))
 
  bs[p-1] = true

echo ans
