Under construction

Still being built. Things may change

Circus

Interview

Time limit1sMemory limit1024 MB

Summary
Find the fewest die rolls needed to move a piece from square 1 to square N past snakes and ladders, and print one optimal sequence of die results.
Level

Easy3 of 10

Topics
BFS, Graph
Solved
No attempts yet

Statement

Juku is not very good at board games. So before the next game night, he decided to practice playing Circus alone.

Circus is a simple board game. The board has NN squares, numbered 1 to NN. Every player's piece starts on square 1. On a turn, a player rolls a six-sided die and moves the piece forward by the number of pips shown (1 to 6). Some squares have a snake or a ladder starting on them. Snakes move a piece back, and ladders move it forward. If a piece stops on a square where a snake starts, the piece must be moved to the other end of the snake. Likewise, if a piece stops on a square where a ladder starts, the piece must be moved to the other end of the ladder. A player wins when the piece reaches square NN or goes past it.

Juku wants to know how much room he still has to improve, so he asks for the minimum number of die rolls needed to win the game.

Input

The first line contains the number of squares NN and the total number of snakes and ladders MM (2≤N≤1052 \le N \le 10^5, 0≤M0 \le M). Each of the next MM lines describes either a ladder or a snake with two integers AA and LL (1<A<N1 < A < N, 1≤L≤N1 \le L \le N, A≠LA \neq L):

  • If A<LA < L, the line describes a ladder that starts on square AA and leads to square LL.
  • If A>LA > L, the line describes a snake that starts on square AA and leads to square LL.

It is guaranteed that all values of AA are pairwise distinct across all lines, and that no value of LL coincides with any value of AA.

Output

On the first line, print the minimum number of die rolls needed to win. On the second line, print the sequence of die results separated by spaces. If several sequences are possible, print any of them. If the game cannot be won, print the single line \verb'EI SAA'.

Examples3

  1. Example 1

    Input
    30 0
    
    Expected output
    5
    5 6 6 6 6
    
  2. Example 2

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

    Input
    31 6
    5 2
    6 2
    7 2
    8 2
    9 2
    10 2
    
    Expected output
    EI SAA