#!/usr/bin/env python
from collections import deque

def main():
    n = int(input())
    p = list(map(int, input().split()))

    xy_lst = []
    for q in p:
        x, y = divmod(q-1, n)
        xy_lst.append([x+1, y+1])

    seat = [[-1 for i in range(n+2)] for j in range(n+2)]
    for i in range(1, n+1):
        for j in range(1, n+1):
            m = min(i-1, j-1, n-i, n-j)
            seat[i][j] = m
    yet = [[0 for i in range(n+2)] for j in range(n+2)]
    for i in range(1, n+1):
        for j in range(1, n+1):
            yet[i][j] = 1

    cnt = 0
    direct = [[-1,0], [1,0], [0,-1], [0,1]]
    for xy in xy_lst:
        x, y = xy
        cnt += seat[x][y]
        seat[x][y] -= 1
        yet[x][y] = 0
        que = deque([xy])

        while que:
            x0, y0 = que.popleft()
            h = seat[x0][y0]
            for d in direct:
                x1, y1 = x0 + d[0], y0 + d[1]
                if seat[x1][y1] > h + yet[x1][y1]:
                    seat[x1][y1] = h + yet[x1][y1]
                    que.append([x1, y1])

    print(cnt)


if __name__ == '__main__':
    main()
