# -*- coding: utf-8 -*-
import sys
# sys.setrecursionlimit(10**6)
buff_readline = sys.stdin.buffer.readline

INF = 2**62-1


def read_int():
    return int(buff_readline())


def read_int_n():
    return list(map(int, buff_readline().split()))


def slv(N, P):
    P = [p-1 for p in P]
    cost = [1] * N**2
    occupy = [1] * N**2
    for i in range(N):
        for j in range(N):
            cost[i*N+j] = min(i, j, N-i-1, N-j-1)

    # print('--- init')
    # for i in range(N):
    #     print([cost[i*N+j] for j in range(N)])

    ans = 0
    for p in P:
        ans += cost[p]
        occupy[p] = 0
        i = p // N
        j = p % N
        s = [(i, j)]
        while s:
            x, y = s.pop()
            pi = x*N + y
            for i, j in [(x, y-1), (x, y+1), (x-1, y), (x+1, y)]:
                if 0 <= i < N and 0 <= j < N:
                    ni = i*N + j
                    nc = cost[pi] + occupy[pi]
                    if nc < cost[ni]:
                        s.append((i, j))
                        cost[ni] = nc

    return ans


def main():
    N = read_int()
    P = read_int_n()
    print(slv(N, P))

    # N = 500
    # P = list(range(1, N**2+1))
    # random.shuffle(P)
    # print(slv(N, P))


if __name__ == '__main__':
    main()
