import sys
input = sys.stdin.readline
from collections import deque
def main():
    N = int(input())
    P = list(map(int, input().split()))
    graph = {}
    exits = [0 for _ in range(N**2)]
    for i in range(N**2):
        graph[i] = set()
    ans = 0
    for i in range(N**2):
        current = P[i]-1
        up = current // N
        left = current % N
        down = N - (current // N) - 1
        right = N - (current % N) - 1
        d1 = min(up, left, down, right)
        d2 = N**2
        if d1 > 0:
            dist = [-1 for _ in range(N**2)]
            dist[current] = 0
            que = deque([])
            que.append(current)
            while len(que) != 0:
                v = que.popleft()
                for node in graph[v]:
                    if dist[node] != -1:continue
                    dist[node] = dist[v]+1
                    up2 = node // N
                    left2 = node % N
                    down2 = N - (node // N) - 1
                    right2 = N - (node % N) - 1
                    d2 = min(d2, up2, left2, down2, right2)
                    que.append(node)
        ans += min(d1, d2)
        exits[current] = 1
        if current == 0:
            for v in [P[i], current+N]:
                if exits[v] == 1:
                    graph[current].add(v)
                graph[v].add(current)
        elif current == N-1:
            for v in [P[i]-2, current+N]:
                if exits[v] == 1:
                    graph[current].add(v)
                graph[v].add(current)
        elif current == N**2-N:
            for v in [P[i], current-N]:
                if exits[v] == 1:
                    graph[current].add(v)
                graph[v].add(current)
        elif current == N**2-1:
            for v in [P[i]-2, current-N]:
                if exits[v] == 1:
                    graph[current].add(v)
                graph[v].add(current)
        elif current < N:
            for v in [P[i]-2, P[i], current+N]:
                if exits[v] == 1:
                    graph[current].add(v)
                graph[v].add(current)
        elif current > N**2-N-1:
            for v in [P[i]-2, P[i], current-N]:
                if exits[v] == 1:
                    graph[current].add(v)
                graph[v].add(current)
        elif (current) % N == 0:
            for v in [P[i], current-N, current+N]:
                if exits[v] == 1:
                    graph[current].add(v)
                graph[v].add(current)
        elif P[i] % N == 0:
            for v in [P[i]-2, current-N, current+N]:
                if exits[v] == 1:
                    graph[current].add(v)
                graph[v].add(current)
        else:
            for v in [P[i]-2, P[i], current-N, current+N]:
                if exits[v] == 1:
                    graph[current].add(v)
                graph[v].add(current)
    print(ans)
if __name__ == "__main__":
    main()