#P12239. Minimum Rounds in the Doubling Game

    ID: 14345 Type: Default 1000ms 256MiB

Minimum Rounds in the Doubling Game

Minimum Rounds in the Doubling Game

Two players, Xiao Lan and Xiao Qiao, play a game starting with a score of \(1\) each. In every round a winner and a loser are determined. The winner’s score is multiplied by \(4\) while the loser’s score is multiplied by \(2\). They play several rounds in a game. Hence, if in a game Xiao Lan wins \(w\) rounds (and loses the remaining \(n-w\) rounds in a game with \(n\) rounds total), his score becomes

[ \text{Blue's score} = 4^{w} \cdot 2^{n-w} = 2^{n+w}, ]

and Xiao Qiao’s score becomes

[ \text{Qiao's score} = 4^{n-w} \cdot 2^{w} = 2^{2n-w}. ]

After many games, they recorded the final scores of each game modulo \(998\,244\,353\). Unfortunately, they forgot the number of rounds each game lasted. Given the two recorded scores \(A\) and \(B\) (where \(A\) is Xiao Lan's score mod \(998\,244\,353\) and \(B\) is Xiao Qiao's score mod \(998\,244\,353\)), your task is to determine the minimum number of rounds \(n\) required to obtain these scores.

More formally, there should exist an integer \(w\) with \(0 \le w \le n\) such that

[ A \equiv 2^{n+w} \pmod{998,244,353}, \quad B \equiv 2^{2n-w} \pmod{998,244,353}. ]

If no such nonnegative integers \(n\) and \(w\) exist then output \(-1\).

Note: The answer is the minimum possible \(n\) (which can be \(0\) if no rounds were played). It is guaranteed that if the recorded scores are correct, then there is a unique minimal \(n\).

inputFormat

The input consists of a single line containing two space‐separated integers \(A\) and \(B\) representing the final scores modulo \(998\,244\,353\).

outputFormat

Output a single integer representing the minimum number of rounds \(n\) needed to obtain the given scores. If there is no solution, output \(-1\).

sample

1 1
0