# coding=utf-8
# Author: lee215
# Date:

import sys
sys.setrecursionlimit(10**6)

get = lambda: int(raw_input().strip())
getl = lambda: raw_input().strip()
gets = lambda: map(int, raw_input().strip().split())
getss = lambda n: [gets() for _ in xrange(n)]

raw_input = input
xrange = range

n = get()
A = gets()


dp = [[n] * n for i in xrange(n)]
for i in xrange(n):
    for j in xrange(n):
        dp[i][j] = min(i, j, n - 1 - i, n - 1 - j)
occ = [[1] * n for i in xrange(n)]


def dfs(i, j, x, force=False):
    if not force:
        if not (0 <= i < n and 0 <= j < n): return
        if dp[i][j] <= x: return
    dp[i][j] = x
    x += occ[i][j]
    dfs(i + 1, j, x)
    dfs(i - 1, j, x)
    dfs(i, j + 1, x)
    dfs(i, j - 1, x)

res = 0
for a in A:
    a -= 1
    i, j = a // n, a % n
    res += dp[i][j]
    occ[i][j] = 0
    dfs(i, j, dp[i][j], True)
print(res)
