import sys, bisect, math, itertools, heapq, collections
from operator import itemgetter
# a.sort(key=itemgetter(i)) # i番目要素でsort
from functools import lru_cache
# @lru_cache(maxsize=None)
sys.setrecursionlimit(10**8)
input = sys.stdin.readline
INF = float('inf')
mod = 10**9 + 7
eps = 10**-7


def inp():
    '''
    一つの整数
    '''
    return int(input())


def inpl():
    '''
    一行に複数の整数
    '''
    return list(map(int, input().split()))


def str_inp():
    '''
    文字列をリストとして読み込む
    '''
    return list(input()[:-1])


n = inp()
p = inpl()
# with open("./temp_input.txt", "r") as f:
#     l = f.readlines()
#     n = int(l[0])
#     p = list(map(int, l[1].split()))
dic = collections.defaultdict(set)
for i in range(n**2):
    chair = i + 1
    if chair > n:
        dic[chair].add(chair - n)
    else:
        dic[chair].add(0)
    if (chair - 1) % n > 0:
        dic[chair].add(chair - 1)
    else:
        dic[chair].add(0)
    if (chair) % n > 0:
        dic[chair].add(chair + 1)
    else:
        dic[chair].add(0)
    if (chair) <= n * (n - 1):
        dic[chair].add(chair + n)
    else:
        dic[chair].add(0)
ans = 0
exited = set()
exited.add(0)
for i in range(n**2):
    chair = p[i]
    queue = [(0, i) for i in dic[chair]]
    visited = set()
    if 0 in dic[chair]:
        while len(queue):
            hate, now_chair = heapq.heappop(queue)
            if now_chair in visited:
                continue
            if (0 not in dic[now_chair]):
                dic[now_chair].add(0)
                if (now_chair in exited):
                    for j in dic[now_chair]:
                        heapq.heappush(queue, (hate, j))
            visited.add(now_chair)
    else:
        while True:
            hate, now_chair = heapq.heappop(queue)
            if now_chair in visited:
                continue
            if now_chair == 0:
                ans += hate
                break
            elif now_chair not in exited:
                hate += 1

            for j in dic[now_chair]:
                heapq.heappush(queue, (hate, j))
            visited.add(now_chair)
        exited.add(chair)
    # print(chair, ans)
# print(dic)
print(ans)
