def solve():
    n = int(input())
    p = [int(x) for x in input().split()]

    ans = 0
    seat = [[min(i, j, n-i-1, n-j-1) for j in range(n)] for i in range(n)]
    reserved = [[True for j in range(n)] for i in range(n)]

    vec = [(-1, 0), (1, 0), (0, -1), (0, 1)]

    for i in range(n**2):
        v = p[i]-1
        x, y = v//n, v % n
        reserved[x][y] = False
        ans += seat[x][y]
        q = [(x, y)]
        while q:
            x, y = q.pop()
            for dx, dy in vec:
                x1 = x+dx
                y1 = y+dy
                if 0 <= x1 < n and 0 <= y1 < n and seat[x1][y1] > seat[x][y] + reserved[x][y]:
                    seat[x1][y1] = seat[x][y] + reserved[x][y]
                    q.append((x1, y1))
    print(ans)


solve()
