N = gets.chomp.to_i
P = gets.chomp.split.map(&:to_i)

go = Array.new(N) { Array.new(N, 1000) }

N.times do |i|
  N.times do |j|
    go[i][j] = [go[i][j], j].min
    go[i][N-j-1] = [go[i][N-j-1], j].min
  end
end

N.times do |i|
  N.times do |j|
    go[i][j] = [go[i][j], i].min
    go[N-i-1][j] = [go[N-i-1][j], i].min
  end
end

left = Array.new(N) { Array.new(N, false) }

ans = 0

P.each do |p|
  i, j = p.divmod(N)
  if j == 0
    i -= 1
    j = N-1
  else
    j -= 1
  end
  count = go[i][j]
  ans += count

  left[i][j] = true

  stack = [[i,j,count]]
  while !stack.empty?
    i, j, count = stack.pop

    go[i][j] = count

    if !left[i][j]
      count += 1
    end

    if i > 0 # 上へ
      if go[i-1][j] > count
        stack.push([i-1, j, count])
      end
    end

    if j > 0 # 左へ
      if go[i][j-1] > count
        stack.push([i, j-1, count])
      end
    end

    if i < N-1 # 下へ
      if go[i+1][j] > count
        stack.push([i+1, j, count])
      end
    end

    if j < N-1 # 右へ
      if go[i][j+1] > count
        stack.push([i, j+1, count])
      end
    end
  end
end

p ans
