The Conga Line
At the party, n dancers numbered 1 to n form a conga line, standing in that
order from front to back: dancer 1 leads, dancer 2 is right behind them, and
so on down to dancer n at the very back.
Over the course of the night, m events happen to the line, each one of three
kinds:
BEHIND x y— dancerxslips out of their current spot and cuts back in directly behind dancery(dancerydoes not move;xsimply ends up immediately behind them).FRONT x— dancerxslips out of their current spot and runs all the way to the front of the line, becoming the new leader.LEAVE x— dancerxslips out of the line and goes home for the night. They take no further part in the party.
You may assume the party is well organized: every event names dancers who are
still at the party (nobody who has already done LEAVE is ever named again,
either as x or as y), and in a BEHIND x y event x and y are always
two different dancers.
After all m events, print the dancers who are still in the line, from front
to back.
Input
The first line contains two integers n and m (1 ≤ n ≤ 2·10^5,
0 ≤ m ≤ 2·10^5).
Each of the next m lines describes one event, in one of the forms above,
where x and y are dancer numbers between 1 and n.
Output
Print the dancers still in the line after all events, from front to back, separated by single spaces, on one line. If nobody is left, print an empty line.
Notes
In the first example, dancer 1 cuts in behind dancer 3, turning
1 2 3 4 5 into 2 3 1 4 5. Then dancer 5 runs to the front, giving
5 2 3 1 4. Finally dancer 3 leaves, giving the final line 5 2 1 4.