from collections import defaultdict, deque
import sys
input = sys.stdin.buffer.readline

N = int(input())


def coordinate(P):
    p, q = divmod(P, N)
    if q == 0:
        return p - 1, N - 1
    else:
        return p, q - 1


def around(i, j):
    ans = []
    if i > 0:
        ans.append((i - 1, j))
    if i < N - 1:
        ans.append((i + 1, j))
    if j > 0:
        ans.append((i, j - 1))
    if j < N - 1:
        ans.append((i, j + 1))
    return ans


def main():
    P_list = list(map(int, input().split()))
    costs = []
    for _ in range(N):
        costs.append([0] * N)
    for i in range(N):
        for j in range(N):
            costs[i][j] = min([i - 0, N - 1 - i, j - 0, N - 1 - j])

    ans = 0
    got_out = defaultdict(bool)

    for P in P_list:
        i, j = coordinate(P)
        cost = costs[i][j]
        ans += cost
        got_out[(i, j)] = True
        li = around(i, j)
        s = {(i, j)}
        # 抜けた席の周辺
        d = deque()
        for t in li:
            if not got_out[t]:
                costs[t[0]][t[1]] = min(costs[t[0]][t[1]], cost)
            else:
                d.append(t)
            s.add(t)

        while d:
            e = d.popleft()
            for t in (set(around(e[0], e[1])) - s):
                if not got_out[t]:
                    costs[t[0]][t[1]] = min(costs[t[0]][t[1]], cost)
                else:
                    d.append(t)
                s.add(t)
    print(ans)


if __name__ == '__main__':
    main()
