def get_index(a):
    if a % N == 0:
        row1 = a // N
        col1 = N
    else:
        row1 = a // N + 1
        col1 = a % N
    return row1, col1


def get_min(lst):
    ans1 = lst[0]
    for i in range(4):
        if lst[i] < ans1:
            ans1 = lst[i]
    return ans1


N = eval(input())
lst = []
for i in range(N + 1):
    lst.append([])
for i in range(N + 1):
    for j in range(N + 1):
        lst[i].append([0, 0, 0, 0])
num = input().split(" ")
for i in range(1, N**2 + 1):
    row, col = get_index(i)
    lst[row][col][0] = row - 1
    lst[row][col][1] = N - row
    lst[row][col][2] = col - 1
    lst[row][col][3] = N - col
ans = 0
for i in range(N**2):
    k = eval(num[i])
    row, col = get_index(k)
    ans += get_min(lst[row][col])
    for j in range(1, col):
        lst[row][j][3] -= 1
    for j in range(col + 1, N + 1):
        lst[row][j][2] -= 1
    for j in range(1, row):
        lst[j][col][1] -= 1
    for j in range(row + 1, N + 1):
        lst[j][col][0] -= 1
print(ans)