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()
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()
    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(ans)
