# -*- 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


class UnionFind():
    def __init__(self):
        self.__table = {}
        self.__size = defaultdict(lambda: 1)
        self.__rank = defaultdict(lambda: 1)

    def __root(self, x):
        if x not in self.__table:
            self.__table[x] = x
        elif x != self.__table[x]:
            self.__table[x] = self.__root(self.__table[x])
        return self.__table[x]
    def root(self, x):
        # print(self.__table)
        return self.__root(x)

    def same(self, x, y):
        return self.__root(x) == self.__root(y)

    def union(self, x, y):
        x = self.__root(x)
        y = self.__root(y)
        if x == y:
            return x

        if self.__rank[x] < self.__rank[y]:
            self.__table[x] = y
            self.__size[y] += self.__size[x]
        else:
            self.__table[y] = x
            self.__size[x] += self.__size[y]
            if self.__rank[x] == self.__rank[y]:
                self.__rank[x] += 1
        return self.__root(x)

    def size(self, x):
        return self.__size[self.__root(x)]

    def num_of_group(self):
        g = 0
        for k, v in self.__table.items():
            if k == v:
                g += 1
        return g



@mt
def slv(N, P):
    P = [p-1 for p in P]

    cd = {}
    for x in range(N):
        for y in range(N):
            cost = min(x, y, N-x-1, N-y-1)
            i = x*N + y
            cd[i] = cost
    done = set()
    ans = 0
    for p in P:
        x = p // N
        y = p % N
        ans += cd[p]
        s = [(x, y, cd[p])]
        done.add(p)
        while s:
            # print(s)
            x, y, c = s.pop()
            for i, j in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
                if 0 <= x+i < N and 0 <= y+j < N:
                    k = (x+i)*N + (y+j)
                    o = -1 if k in done else 0
                    if c + o < cd[k]:
                        s.append((x+i, y+j, cd[k]))
                        cd[k] = min(cd[k], c+o)

        # print('----', p)
        # for i in range(N):
        #     print([cd[i*N+j] for j in range(N)])
    return ans


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


if __name__ == '__main__':
    main()
