import numpy as np
import math
import copy
import collections
from collections import deque

N = int(input())
P = list(map(int, input().split()))
P = list(map(lambda x: x - 1, P))

NN = N * N
exist = [True for i in range(NN)]
exist2 = [True for i in range(NN)]
ans = 0
for i in range(NN):
    #print("ans:", ans)
    que = deque()
    next_que = deque()
    next_que.append(P[i])
    goal = False
    #print("start:", P[i] + 1)
    exist[P[i]] = False
    exist2 = copy.deepcopy(exist)
    visited = [False for i in range(NN)]
    visited[P[i]] = True
    while(not goal):
        #visited = [False for i in range(NN)]
        que = copy.deepcopy(next_que)
        next_que.clear()
        hate = []
        while(len(que) > 0 and not goal):
            p = que.popleft()
            #print(p + 1)
            for nex in [p - 1, p + 1, p + N, p - N]:
                if nex < 0 or nex > NN or p % N == 0 or p % N == N - 1:
                    goal = True
                    break
                if not visited[nex]:
                    if not exist2[nex]:
                        que.append(nex)
                        visited[nex] = True
                    else:
                        next_que.append(nex)
                        hate.append(nex)

        if not goal:
            ans += 1
            #print(list(map(lambda x: x + 1, hate)))
            for h in hate:
                exist2[h] = False

print(ans)
