Under construction

Still being built. Things may change

Spaceships

Time limit10sMemory limit256 MB

Summary
Ships add and remove one-way routes over time, and each query asks for the star that minimizes total hops from two given stars, or -1 if none exists.
Level

Hard9 of 10

Topics
Graph, Shortest path, Divide and conquer, Union-find
Solved
No attempts yet

Problem

Far away in a galaxy, there are NN civilized stars numbered 1 through NN. Each star manages one spaceship. A spaceship is either in use, traveling to some other star, or idle. If the spaceship managed by star aa is in use and heading to star bb, it shuttles between stars aa and bb many times. When the spaceship goes from star aa to star bb, ordinary passengers can ride it from aa to bb. When it returns from bb to aa, however, ordinary passengers cannot board it, because of fuel issues, cargo loading, and similar reasons. While the spaceship managed by star aa is idle, it waits at star aa.

All spaceships are idle now. The schedule of future status changes is already set. Each change is one of the following two kinds.

  • An idle spaceship managed by star aa is set to in use, heading to star bb. This is done only when ordinary passengers cannot ride that spaceship several times to go from bb to aa.
  • An in-use spaceship managed by star aa is set to idle.

Two people planning a trip through this galaxy prepare several questions to decide where to meet. At some moment, one person is at star aa and the other is at star bb. Can the two of them meet as ordinary passengers using the spaceships? If they can, which star gives the fewest spaceship uses? In other words, does a star cc exist such that ordinary passengers can ride spaceships several times to get from aa to cc and from bb to cc? If it exists, which star cc minimizes the sum of the number of spaceship uses needed to go from aa to cc and the number needed to go from bb to cc?

Input

The first line contains two integers NN and QQ separated by a space. NN is the number of stars, and QQ is the total number of status changes and questions.

The following QQ lines describe the status changes and questions in chronological order. Line ii contains two or three integers separated by spaces. Let TiT_i be the first integer. Then one of the following holds.

  • If Ti=1T_i = 1, the line contains TiT_i, AiA_i, BiB_i. The spaceship managed by star AiA_i is set to in use, heading to star BiB_i. It is guaranteed that 1≤Ai≤N1 \le A_i \le N, 1≤Bi≤N1 \le B_i \le N, Ai≠BiA_i \ne B_i, that this spaceship is idle at this moment, and that ordinary passengers cannot ride it several times to go from BiB_i to AiA_i at this moment.
  • If Ti=2T_i = 2, the line contains TiT_i, AiA_i. The spaceship managed by star AiA_i is set to idle. It is guaranteed that 1≤Ai≤N1 \le A_i \le N and that this spaceship is in use at this moment.
  • If Ti=3T_i = 3, the line contains TiT_i, AiA_i, BiB_i. This is a question: at this moment, one person is at star AiA_i and the other is at star BiB_i. It is guaranteed that 1≤Ai≤N1 \le A_i \le N, 1≤Bi≤N1 \le B_i \le N, Ai≠BiA_i \ne B_i.

Output

For each question, in order, print one line. If the two can meet, print the number of the star where they should meet to minimize the number of spaceship uses. If they cannot meet, print -1.

Constraints

  • 2≤N≤10000002 \le N \le 1000000
  • 1≤Q≤10000001 \le Q \le 1000000

Examples2

  1. Example 1

    Input
    6 5
    1 2 4
    3 2 6
    1 4 3
    1 6 4
    3 2 6
    
    Expected output
    -1
    4
    
  2. Example 2

    Input
    8 36
    1 1 2
    1 6 5
    1 7 8
    3 5 6
    1 5 4
    1 8 1
    3 7 2
    3 3 8
    3 1 8
    1 3 2
    1 4 1
    3 8 5
    3 4 3
    2 4
    3 6 8
    1 2 5
    3 6 8
    2 8
    3 1 4
    3 6 8
    3 6 3
    2 3
    3 1 2
    1 4 3
    3 2 6
    1 8 3
    3 1 7
    3 1 6
    3 5 4
    2 2
    2 5
    1 3 6
    1 2 7
    3 1 4
    3 1 5
    3 6 7
    
    Expected output
    5
    2
    -1
    1
    1
    2
    -1
    5
    4
    -1
    5
    2
    5
    3
    5
    4
    3
    5
    6