#P15647. [ICPC 2022 Tehran R] Magic with Cards
[ICPC 2022 Tehran R] Magic with Cards
Problem Description
Mahsa has been practicing shuffling cards for a few months now. Tonight, she finally decided to invite her friends over and show off her new skills. So she picks up a deck with cards, shows her friends the face of the cards without changing the deck order and asks someone to pick two positions and in the deck. Then, she tells everyone that she is going to move the card in the -th position to the -th position by applying only two types of shuffles.
Assume the cards in the deck are . Mahsa can perform these two shuffles as many times as she wants:
- Riffle: Divide the cards into two parts and and produce $\langle c_{1}, c_{n+1}, c_{2}, c_{n+2}, \ldots, c_{n}, c_{2n} \rangle$.
- Scuffle: From , produce $\langle c_{2}, c_{1}, c_{4}, c_{3}, \ldots, c_{2n}, c_{2n-1} \rangle$.
Help Mahsa find out the minimum number of shuffles she needs, and she'll figure out the rest.
Input Format
The input consists of a single line containing three space-separated integers , and ( and ).
Output Format
Print a single integer, the minimum number of shuffles required to bring the -th card to -th position. If it is not possible to do so, print instead.
4 3 8
3
5 4 1
5
1 1 1
0