Under construction

Still being built. Things may change

Flight Plans

Time limit1sMemory limit512 MB

Summary
Each airport lists either its outgoing destinations or its non-destinations; find the fewest flights from s to t in the resulting implicit graph.
Level

Medium7 of 10

Topics
Graph, BFS, Implementation, Hash map
Solved
No attempts yet

Problem

As you are probably aware, flight pricing can sometimes be surprisingly complex. For example, it can often be cheaper to take a much longer flight with several legs instead of flying directly between two airports. One of the reasons pricing seems to be complex is that airlines often try to obfuscate exactly how pricing works, in order to ensure that their customers choose more expensive flights.

One particular airline has decided to take this obfuscation to the next level; they do not even offer an automated search of their flights. Instead, they describe their flights in a very peculiar format. For every one of their NN airports (which are numbered between 00 and N−1N - 1), they list either:

  • what airports they travel to from this airport, or
  • what airports they do not travel to from this airport.

To compensate for this complexity, the airline sets the price of every direct flight between two airports to the same amount.

Can you write a program that, given the descriptions of all the flights the airline provides, determine the minimum number of flights required to travel from airport ss to airport tt?

Input

The first line of input contains an integer 1≤N≤1051 \le N \le 10^5, the number of airports, and the two integers ss and tt (0≤s,t<N0 \le s, t < N, s≠ts \neq t).

The next NN lines each describe the outgoing flights of an airport, starting with airport 00. The line starts with a letter. If this letter is N, you will get a list of all destination airports from this airport. If this letter is C, you will get a list of all airports that are not destinations from this airport.

Following this letter is an integer mm, the number of airports in the list. Finally, there will mm unique numbers aia_i (0≤ai<N0 \le a_i < N) on the line, the airports in the list.

The sum of mm over all airports is at most 2⋅1052 \cdot 10^5.

Output

Output a single integer, the minimum number of flights required to travel from airport ss to airport tt.

If there is no path, output "impossible".

Examples2

  1. Example 1

    Input
    4 0 1
    N 1 2
    C 1 2
    N 1 3
    C 1 1
    
    Expected output
    impossible
    
  2. Example 2

    Input
    4 0 1
    N 1 2
    C 1 2
    N 1 3
    C 1 0
    
    Expected output
    3