Valerija works as a valet at a fancy restaurant. Her job is to wait for the arrival of distinguished guests, politely greet them, obtain the keys of their vehicles, and park their vehicles at a nearby parking lot. Once the event is over, she makes sure that each of the guests regains custody of their vehicle and happily leaves the venue.
One evening, shortly after she finished parking all of the vehicles, she noticed a particularly interesting
property regarding their colors. Namely, it turned out there were exactly
The parking lot itself is organized as a sequence of

Valerija would like to repark the vehicles so that each pair of vehicles of the same color is parked in the same parking space. She doesn't care which parking space will contain which color, and which specific vehicle will be at the top or bottom of the parking space. She will repark the vehicles in a series of drives. In each drive, she will sit in a parked vehicle that is able to exit its current parking space, and will drive it to another parking space that is either:
- empty, in which case she parks it as a bottom vehicle, or
- contains a single parked vehicle of the same color as the one she's currently driving, in which case she parks it as a top vehicle.
Valerija wants to minimize the number of drives needed to repark the vehicles according to her wishes. Your task is to help her by finding the shortest sequence of drives that would achieve her goal, or determine that no such sequence exists.
Input Specification
The first line contains two space-separated integers
The
Output Specification
If there is no sequence of drives that could repark the vehicles according to Valerija's wishes, print -1
in
the only line.
Otherwise, the first line should contain an integer
The
Constraints
For all subtasks:
If your solution correctly determines the smallest number of drives in all test cases of a certain subtask,
but incorrectly outputs the description of drives in some of them (or doesn't output it at all), it will
receive
Subtask | Score | Constraints |
---|---|---|
All parking spaces are initially either empty or full, and |
||
All parking spaces are initially either empty or full. | ||
No additional constraints. |
Sample Input 1
4 5
1 0
2 0
1 3
4 4
3 2
Sample Output 1
3
5 2
3 5
3 1
Explanation for Sample 1
The image from the task description depicts the initial state of the parking lot in this example. Notice that in this case, each drive is forced, i.e. there is only one valid first drive, only one valid second drive, and two equivalent third drives, after which we reach the end goal.
Sample Input 2
4 5
0 0
2 1
3 1
3 4
2 4
Sample Output 2
-1
Sample Input 3
5 7
1 0
2 1
2 3
4 3
5 4
5 0
0 0
Sample Output 3
6
2 1
3 7
4 7
2 3
5 4
5 6
Comments