#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 2n2n cards, shows her friends the face of the cards without changing the deck order and asks someone to pick two positions ii and jj in the deck. Then, she tells everyone that she is going to move the card in the ii-th position to the jj-th position by applying only two types of shuffles.

Assume the cards in the deck are c1,c2,,c2n\langle c_{1}, c_{2}, \ldots, c_{2n} \rangle. Mahsa can perform these two shuffles as many times as she wants:

  • Riffle: Divide the cards into two parts c1,,cn\langle c_{1}, \ldots, c_{n} \rangle and cn+1,,c2n\langle c_{n+1}, \ldots, c_{2n} \rangle and produce $\langle c_{1}, c_{n+1}, c_{2}, c_{n+2}, \ldots, c_{n}, c_{2n} \rangle$.
  • Scuffle: From c1,c2,,c2n\langle c_{1}, c_{2}, \ldots, c_{2n} \rangle, 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 nn, ii and jj (1n1051 \leq n \leq 10^{5} and 1i,j2n1 \leq i, j \leq 2n).

Output Format

Print a single integer, the minimum number of shuffles required to bring the ii-th card to jj-th position. If it is not possible to do so, print 1-1 instead.

4 3 8
3
5 4 1
5
1 1 1 
0