import sys
input = sys.stdin.readline
from collections import deque
def main():
    N = int(input())
    P = list(map(int, input().split()))
    costs = [-1 for _ in range(N**2)]
    exits = [0 for _ in range(N**2)]
    for i in range(N**2):
        q, mod = divmod(i, N)
        up, down, left, right = q, N-q-1, mod, N-mod-1
        costs[i] = min(up, down, left, right)
    ans = 0
    for i in range(N**2):
        current = P[i]-1
        ans += costs[current]
        exits[current] = 1
        dist = [-1 for _ in range(N**2)]
        que = deque([])
        dist[current] = 0
        que.append(current)
        while len(que) != 0:
            v = que.popleft()
            arround = []
            for nv in [v-1, v+1, v+N, v-N]:
                if 0 < nv < N**2 and costs[nv] > 0:
                    if exits[v] == 1 or dist[nv] == 1:
                        arround.append(nv)
            for nv in arround:
                if dist[nv] != -1:continue
                dist[nv] += 1
                costs[nv] = min(costs[v], costs[nv])
                que.append(nv)
    print(ans)
if __name__ == "__main__":
    main()