def initDistanceTable(n):
    DistanceMap = [1] * (n * n)
    for x in range(n):
        for y in range(n):
            DistanceMap[x + y * n] = getShortest(x, y, n)
    return DistanceMap

def removeTargetSub(PersonMap, DistanceMap, FlagMap, x, y, dx, dy, index, n):
    if x + dx < 0 or x + dx >= n or y + dy < 0 or y + dy >= n:
        return
    t1 = x + n * y
    t2 = x + dx + n * (y + dy)
    if FlagMap[t2] < index:
        if DistanceMap[t1] + 1 == DistanceMap[t2] and PersonMap[t2] < index:
            removeTarget(PersonMap, DistanceMap, FlagMap, x + dx, y + dy, index, n)
            return
        if DistanceMap[t1] + 2 == DistanceMap[t2] and PersonMap[t2] > index:
            removeTarget(PersonMap, DistanceMap, FlagMap, x + dx, y + dy, index, n)
            return

def removeTarget(PersonMap, DistanceMap, FlagMap, x, y, index, n):
    t = x + n * y
    DistanceMap[t] -= 1
    FlagMap[t] = index
    removeTargetSub(PersonMap, DistanceMap, FlagMap, x, y, -1, 0, index, n)
    removeTargetSub(PersonMap, DistanceMap, FlagMap, x, y, 1, 0, index, n)
    removeTargetSub(PersonMap, DistanceMap, FlagMap, x, y, 0, -1, index, n)
    removeTargetSub(PersonMap, DistanceMap, FlagMap, x, y, 0, 1, index, n)

def getShortest(x, y, n):
    return min([x, n - 1 - x, y, n - 1 - y]) + 1


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

    DistanceMap = initDistanceTable(n)
    for i in range(n*n):
        ans = ans + DistanceMap[seq[i] - 1] - 1
        removeTarget(PersonMap, DistanceMap, FlagMap, (seq[i] - 1) % n, (seq[i] - 1) // n, i, n)

    print(ans)

main()