Program

Time limit2sMemory limit512 MB

Summary
Given a sequence of assignment and conditional assignment instructions starting from X=1, delete the fewest instructions so the final value is k, for every k.
Level

Hard8 of 10

Topics
Graph, Shortest path, BFS, Implementation
Solved
No attempts yet

Problem

You are given a program that operates on an integer variable XX, which is initially equal to 1. The program consists of nn instructions of two types:

  • 1 p (1≤p≤n1 \le p \le n) assigns the value pp to variable XX.
  • 2 p q (1≤p,q≤n1 \le p, q \le n, p≠qp \neq q) assigns the value qq to variable XX only if the current value of XX is pp.

In one step, you can remove any single instruction from the program. You cannot reorder instructions or add new instructions. What is the minimum number of steps required to get a program that, after it runs, leaves variable XX with the value kk? Solve this problem for each kk from 1 to nn.

Input

The first line of input contains a single integer nn (2≤n≤1062 \le n \le 10^6), the number of instructions in the program.

The next nn lines contain descriptions of instructions in the format described above.

Output

Output nn integers, where the ii-th integer is the minimum number of steps required to make the program assign the value ii to variable XX, or −1-1 if it is impossible.

Examples2

  1. Example 1

    Input
    3
    1 1
    1 2
    1 3
    
    Expected output
    2 1 0
    
  2. Example 2

    Input
    4
    2 1 2
    1 3
    2 2 3
    2 3 1
    
    Expected output
    0 2 1 -1