const isEdge = (n, x, y) => {
  return x === 0 || y === 0 || x === n - 1 || y === n - 1
}

const directions = [
  [0, 1],
  [0, -1],
  [1, 0],
  [-1, 0]
]

const processData = (lines) => {

  const n = +lines[0]
  const seats = new Array(n)
  for(let i=0; i<seats.length; i++) {
    seats[i] = new Array(n).fill(1)
  }
  const s = lines[1].split(' ').map(x => ([(x-1)%n, Math.floor((x-1)/n)]))
  let sum = 0
  for (const [x, y] of s) {
    let map = {[`${x}_${y}`]: 1}
    let queue = [[[x, y], 0]]
    let priorityQueue = []
    let iterator = 0
    let priorityIterator = 0
    // console.log(x, y)
    // console.log(seats.map(line => line.join(' ')).join('\n'), '\n')
    while (true) {
      // console.log('here')
      // console.log(queue, priorityQueue)
      if (priorityIterator < priorityQueue.length) {
        const el = priorityQueue[priorityIterator]
        if (isEdge(n, el[0][0], el[0][1])) {
          sum += el[1]
          break
        } else {
          for (const d of directions) {
            const newX = el[0][0] + d[0]
            const newY = el[0][1] + d[1]
            const hash = `${newX}_${newY}`
            if (!map[hash]) {
              map[hash] = 1
              if (seats[newX][newY] === 0) {
                priorityQueue.push([[newX, newY], el[1] + seats[newX][newY]])
              } else {
                queue.push([[newX, newY], el[1] + seats[newX][newY]])
              }
            }
          }
        }
        priorityIterator++
      } else {
        const el = queue[iterator]
        if (isEdge(n, el[0][0], el[0][1])) {
          sum += el[1]
          break
        } else {
          for (const d of directions) {
            const newX = el[0][0] + d[0]
            const newY = el[0][1] + d[1]
            const hash = `${newX}_${newY}`
            if (!map[hash]) {
              map[hash] = 1
              if (seats[newX][newY] === 0) {
                priorityQueue.push([[newX, newY], el[1] + seats[newX][newY]])
              } else {
                queue.push([[newX, newY], el[1] + seats[newX][newY]])
              }
            }
          }
        }
        iterator++
      }
    }
    seats[x][y] = 0
  }
  console.log(sum)
}

let i = ''
process.stdin.on('data', c => i += c)
process.stdin.on('end', () => {
  const {EOL} = require('os')
  const lines = i.split(EOL) /*your input text, split by lines*/
  processData(lines)
})
