Magic with Cards | 프로그래밍의 벗 PivotOJ
PivotOJ

Magic with Cards

시간 제한: 2000ms메모리 제한: 1024MB출처: ICPC Asia Tehran Regional Contest 2022BOJ 28233

문제

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

Assume the cards in the deck are ⟨c1, c2, ..., c2n⟩. Mahsa can perform these two shuffles as many times as she wants:

Riffle: Divide the cards into two parts⟨c1, c2, ..., cn⟩ and ⟨cn+1, ..., c2n⟩ and produce ⟨c1, cn+1, c2, cn+2, ..., cn, c2n⟩.

Scuffle: From ⟨c1, c2, ..., c2n⟩, produce ⟨c2, c1, c4, c3, ..., c2n, c2n-1⟩.

Help Mahsa find out the minimum number of shuffles she needs, and she’ll figure out the rest.

입력

The input consists of a single line containing three space-separated integers n, i and j (1 ⩽ n ⩽ 105 and 1 ⩽ i, j ⩽ 2n).

출력

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

예제

예제 1

입력
4 3 8
출력
3

예제 2

입력
5 4 1
출력
5

예제 3

입력
1 1 1
출력
0
코드를 제출하려면 로그인하세요.