import copy

def fillDistanceTable(PersonMap, DistanceMap, n, shortest, target, index):
    if shortest > n * n:
        print("Error")
        exit()
    if DistanceMap[target] > n * n:
        flag = False
        for i in range(n * n):
            if DistanceMap[i] > n * n:
                if DistanceMap[ i - 1 ] <= shortest or DistanceMap[ i + 1 ] <= shortest or DistanceMap[ i - n ] <= shortest or DistanceMap[ i + n ] <= shortest:
                    setDistance(PersonMap, DistanceMap, i, shortest, index)
                    flag = True
        if flag:
            fillDistanceTable(PersonMap, DistanceMap, n, shortest, target, index)
        else:
            fillDistanceTable(PersonMap, DistanceMap, n, shortest + 1, target, index)
        
def setDistance(PersonMap, DistanceMap, i, dist, index):
    if PersonMap[i] > index:
        DistanceMap[i] = dist + 1
    else:
        DistanceMap[i] = dist

def checkBestRoot(PersonMap, n, target, index):
    DistanceMap =  [n * n + 1] * (n * n)
    for i in range(n):
        setDistance(PersonMap, DistanceMap, i, 0, index)
        setDistance(PersonMap, DistanceMap, n * n - n + i, 0, index)
        setDistance(PersonMap, DistanceMap, n * i, 0, index)
        setDistance(PersonMap, DistanceMap, n * i + n - 1, 0, index)
    fillDistanceTable(PersonMap, DistanceMap, n, 0, target, index)
    return DistanceMap[target]

def main():
    ans = 0
    n = int(input())
    seq = list(map(int,input().split()))
    PersonMap = [0] * (n * n)
    for i in range(n*n):
        PersonMap[seq[i] - 1] = i

    for i in range(n*n):
        ans = ans + checkBestRoot(PersonMap, n, seq[i] - 1, i)

    print(ans)

main()