#!/usr/bin/env python
# -*- coding: utf-8 -*-
import collections
import itertools


class Cell(object):

  def __init__(self, x, y):
    self.x = x
    self.y = y
    self.value = None
    self.exist = 1
    self.neighbors = set()

  def __hash__(self):
    return self.x + self.y * 500

  def __eq__(self, obj):
    return self.x == obj.x and self.y == obj.y


def main():
  N = int(input())
  cells = [[Cell(x, y) for x in range(N)] for y in range(N)]
  queue = collections.deque()
  count = 0

  for x, y in itertools.product(range(N), repeat=2):
    c = cells[y][x]

    if x == 0:
      c.value = 0
    else:
      c.neighbors.add(cells[y][x - 1])

    if x == N - 1:
      c.value = 0
    else:
      c.neighbors.add(cells[y][x + 1])

    if y == 0:
      c.value = 0
    else:
      c.neighbors.add(cells[y - 1][x])

    if y == N - 1:
      c.value = 0
    else:
      c.neighbors.add(cells[y + 1][x])

    if c.value == 0:
      queue.append(c)

  while len(queue) != 0:
    c = queue.popleft()

    for n in c.neighbors:
      if n.value is None:
        n.value = c.value + 1
        queue.append(n)

  for i in map(int, input().split()):
    x, y = divmod(i - 1, N)
    count += cells[y][x].value
    cell = cells[y][x]

    cell.value -= 1
    cell.exist = 0

    for n in list(cell.neighbors):
      if n.exist != 0:
        continue

      cell.neighbors |= n.neighbors
      cells[n.y][n.x] = cell

    queue = collections.deque()
    queue.append(cells[y][x])

    while len(queue) != 0:
      c = queue.popleft()

      for n in c.neighbors:
        if n.value > c.value + n.exist:
          n.value = c.value + n.exist
          queue.append(n)

  print(count)


if __name__ == '__main__':
  main()
