import numpy as np
N = int(input())
# = map(int, input().split())
P = list(map(int, input().split()))
# = [int(input()) for i in range(N)]
# = [list(map(int, input().split())) for i in range(N)]
seat = np.ones((N, N))
total = 0

for i in range(N**2):
    tate = (P[i]-1)//N
    yoko = (P[i]-1)%N
    if yoko == 0 or yoko == N-1 or tate == 0:
        seat[tate][yoko] = 0
    else:
        shortest = N
        count = 0
        for i in range(yoko):
            if seat[tate][i] == 1:
                count+=1
        if count < shortest:
            shortest = count
        count = 0
        for i in range(N-yoko):
            if seat[tate][N-1-i] == 1:
                count += 1
        if count < shortest:
            shortest = count
#        count = 0
#        for i in range(tate):
#            if seat[i][yoko] == 1:
#                count+=1
#        if count < shortest:
#            shortest = count
#        count = 0
#        for i in range(N-tate):
#            if seat[N-1-i][yoko] == 1:
#                count += 1
#        if count < shortest:
#            shortest = count
        total += shortest
        seat[tate][yoko] = 0

print(total)
