def main():
    from collections import deque
    # import sys
    # input = sys.stdin.readline

    N = int(input())
    *p, = (int(x) - 1 for x in input().split())

    H = [[-1] * (N + 1) for _ in range(N + 1)]  # 離席コスト,J字にコスト-1の区画を設ける
    for r in range((N + 1) // 2 + 1):
        dr = min(r, N - 1 - r)
        for c in range((N + 1) // 2 + 1):
            dc = min(c, N - 1 - c)
            H[r][c] = H[N - 1 - r][c] = H[N - 1 - r][N - 1 - c] = H[r][N - 1 - c] = min(dr, dc)
    S = [[1] * N for _ in range(N)]  # 着席

    ans = 0
    for pi in p:
        pi_r, pi_c = divmod(pi, N)

        hpi = H[pi_r][pi_c]  # piの離席コスト
        ans += hpi  # piの離席コストを加算
        S[pi_r][pi_c] = 0  # piを空席にする

        # BFSで離席コストを更新
        dq = deque()
        dq.append((pi_r, pi_c, hpi))
        while dq:
            r, c, h = dq.popleft()
            for nr, nc in ((r - 1, c), (r, c - 1), (r + 1, c), (r, c + 1)):
                if H[nr][nc] > h:
                    H[nr][nc] = h
                    dq.append((nr, nc, h + S[nr][nc]))
    print(ans)


if __name__ == '__main__':
    main()
