#!usr/bin/env python3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007


def solve():
    n = I()
    p = LI()
    ans = 0
    s = [[min(y,x,n-y-1,n-x-1) for x in range(n)] for y in range(n)]
    v = [[set([(y,x+1),(y,x-1),(y+1,x),(y-1,x)]) for x in range(n+1)] for y in range(n+1)]
    for i in p:
        y = (i-1)//n
        x = (i-1)%n
        dyx = s[y][x]
        for ny,nx in v[y][x]:
            if 0 <= ny < n and 0 <= nx < n:
                nd = s[ny][nx]+1
                if nd < dyx:
                    dyx = nd
        s[y][x] = dyx
        ans += dyx
        for ny,nx in v[y][x]:
            v[ny][nx] |= v[y][x]
            if 0 <= ny < n and 0 <= nx < n:
                if dyx < s[ny][nx]:
                    s[ny][nx] = dyx
    print(ans)
    return

#Solve
if __name__ == "__main__":
    solve()
