Mirko received a birthday present from his aunt in the US – a brand-new doubly-linked list (an example of which is shown in the figure below). The list contains
- A) Move node
in front of node . - B) Move node
after node .

An example of a list with

The list after the move A 1 4
.

The list after another move, B 3 5
.
Mirko played with his new toy for hours, writing down each move on a piece of paper so that he can reconstruct the list's initial state (nodes
When he decided to reconstruct the list, Mirko was astonished to find that there is no easy way to invert the moves and restore the list's initial state. Mirko cannot know where node
Seeing how Mirko is still recovering from the shock, write a program that finds a minimal sequence of moves that restored the list's initial state from Mirko's logs.
Input Specification
The first line of input contains two integers
Each of the next A
or B
) and two integers
Output Specification
Output the minimum number of moves (call this number
Each of the next
Note: The sequence need not be unique.
Scoring
If both the number
If your program outputs the correct number
Sample Input 1
2 1
A 2 1
Sample Output 1
1
A 1 2
Sample Input 2
4 3
B 1 2
A 4 3
B 1 4
Sample Output 2
2
A 1 2
B 4 3
Sample Input 3
6 5
A 1 4
B 2 5
B 4 2
B 6 3
A 3 5
Sample Output 3
3
A 4 5
B 6 5
A 2 3
Comments