#!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 = [[max(0,min(y,x,n-y-1,n-x-1)+1) for x in range(n+2)] for y in range(n+2)]
    r = [[(y,x+1) for x in range(n+2)] for y in range(n+2)]
    l = [[(y,x-1) for x in range(n+2)] for y in range(n+2)]
    d = [[(y+1,x) for x in range(n+2)] for y in range(n+2)]
    u = [[(y-1,x) for x in range(n+2)] for y in range(n+2)]

    for i in p:
        y = (i-1)//n
        x = (i-1)%n
        dyx = s[y][x]
        ri = r[y][x]
        le = l[y][x]
        do = d[y][x]
        up = u[y][x]
        for ny,nx in (ri,le,do,up):
            nd = s[ny][nx]+1
            if nd < dyx:
                dyx = nd
        s[y][x] = dyx
        ans += dyx
        for ny,nx in (ri,le,do,up):
            if dyx < s[ny][nx]:
                s[ny][nx] = dyx
        r[y][le[1]] = ri
        l[y][ri[1]] = le
        d[up[0]][x] = do
        u[do[0]][x] = up
    print(ans-n**2)
    return

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