from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
mod = 1000000007
m = [(0, 1), (1, 0), (-1, 0), (0, -1)]
inf = float('INF')

#solve
def solve():
    n = II()
    p = LI()
    dp = [[min(i, j, n - i - 1, n - j - 1) for i in range(n)] for j in range(n)]
    c = [[1 for i in range(n)] for j in range(n)]
    ans = 0
    for pi in p:
        y, x = divmod(pi - 1, n)
        ans += dp[y][x]
        c[y][x] = 0
        q = deque()
        q.append((y, x))
        while q:
            y, x = q.popleft()
            score = dp[y][x] + c[y][x]
            for my, mx in m:
                my += y
                mx += x
                if 0 < my < n - 1 and 0 < mx < n - 1:
                    if dp[my][mx] > score:
                        dp[my][mx] = score
                        if c[my][mx]:
                            q.append((my, mx))
                        else:
                            q.appendleft((my, mx))
    print(ans)
    return


#main
if __name__ == '__main__':
    solve()
