### BUGGY CODE ###

# -*- coding: utf-8 -*-
from collections import defaultdict
import bisect
import heapq
import math
import random
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub


import sys
# sys.setrecursionlimit(10**6)
# buff_readline = sys.stdin.buffer.readline
buff_readline = sys.stdin.readline
readline = sys.stdin.readline

INF = 2**62-1


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


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


def read_float():
    return float(buff_readline())


def read_float_n():
    return list(map(float, buff_readline().split()))


def read_str():
    return readline().strip()


def read_str_n():
    return readline().strip().split()

def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, P):
    P = [p-1 for p in P]
    cost = [1] * N**2
    occupy = [True] * 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] = False
        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] + (1 if occupy[pi] else 0)
                    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()


### FIXED CODE ###

# -*- coding: utf-8 -*-
import sys
buff_readline = sys.stdin.buffer.readline


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


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


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

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


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


if __name__ == '__main__':
    main()