#!/usr/bin/env python3
import sys
input = sys.stdin.readline
from collections import deque

n = int(input())

field = [[-1] * (n + 2)]
for _ in range(n):
    field.append([-1] + [1] * n + [-1])
field.append([-1] * (n + 2))
p = [int(item) - 1 for item in input().split()]


dxs = [0, 1, 0, -1]
dys = [1, 0, -1, 0]

def bfs(x, y):
    dq = deque()
    dq.append((x, y, 0))
    visited = [[0] * (n + 2) for _ in range(n + 2)]
    visited[x][y] = 1
    field[x][y] = 0
    while dq:
        x, y, d = dq.popleft()
        for dx, dy in zip(dxs, dys):
            nx = x + dx; ny = y + dy
            if visited[nx][ny]:
                continue
            if field[nx][ny] == -1:
                return d
            visited[nx][ny] = 1
            if field[nx][ny] == 0:
                dq.appendleft((nx, ny, d))
            else:
                dq.append((nx, ny, d + 1))

ans = 0
for item in p:
    i = item // n
    j = item % n
    ans += bfs(i + 1, j + 1)

print(ans)