import sys

sys.setrecursionlimit(25000)


def _i(): return int(sys.stdin.readline().strip())


def _ia(): return map(int, sys.stdin.readline().strip().split())


visited = {}


def update(n, cost, idx):
    global visited
    ci = cost[idx] + visited.get(idx, 1)

    # left
    if idx % n != 0:
        idx_ = idx - 1
        if cost[idx_] > ci:
            cost[idx_] = ci
            update(n, cost, idx_)

    # right
    idx_ = idx + 1
    if idx_ % n != 0:
        if cost[idx_] > ci:
            cost[idx_] = ci
            update(n, cost, idx_)

    # front
    idx_ = idx - n
    if idx_ >= 0:
        if cost[idx_] > ci:
            cost[idx_] = ci
            update(n, cost, idx_)

    # back
    idx_ = idx + n
    if idx_ // n < n:
        if cost[idx_] > ci:
            cost[idx_] = ci
            update(n, cost, idx_)


def main():
    global visited

    n = _i()
    p = list(map(lambda x: int(x)-1, _ia()))
    cost = [min(i//n, i % n, n-1-(i % n), n-1-i//n) for i in range(n**2)]

    tot = 0
    for pi in p:
        visited[pi] = 0
        ci = cost[pi]
        tot += ci
        update(n, cost, pi)

    return tot


if __name__ == "__main__":
    print(main())
