<|instruction|>
Solve the below problem. The solution should start with an abridged problem statement. Then key observations. Then full solution based on the observations. Then C++ and Python implementations with comments.

126. Boxes

time limit per test: 0.25 sec.
memory limit per test: 4096 KB


There are two boxes. There are A balls in the first box, and B balls in the second box (0 < A + B < 2147483648). It is possible to move balls from one box to another. From one box into another one should move as many balls as the other box already contains. You have to determine, whether it is possible to move all balls into one box.


Input

The first line contains two integers A and B, delimited by space.


Output

First line should contain the number N - the number of moves which are required to move all balls into one box, or -1 if it is impossible.


Sample Input

Sample Output

2 6
Sample Output

2
Author	: Victor G. Samoilov, Herman "Smash" Narkaytis
Resource	: 5th Southern Subregional Contest. Saratov 2002
Date	: 2002-10-10

<|response|>
1. Abridged Problem Statement  
   Two boxes contain A and B balls (A+B>0). In each move, you may transfer from one box to the other exactly as many balls as are currently in the destination box. Determine the minimum number of moves to gather all balls into a single box, or report –1 if it is impossible.

2. Key Observations  
   • Total number of balls T = A + B stays constant throughout.  
   • Without loss of generality, label the boxes so that a ≤ b at each step.  
   • A move from the larger box (b) into the smaller box (a) changes the counts to:  
     – a becomes a + a = 2 a  
     – b becomes b – a  
     In other words, (a, b) → (2a, b–a).  
   • If at any point a = 0, then all T balls are in one box and we are done.  
   • If it is ever possible to collect all balls in one box, this “always move from larger into smaller” strategy will reach a = 0 in at most O(log T) moves.  
   • Since T < 2^31, doubling the smaller pile more than ~31 times would exceed T. It suffices to try up to 60 moves; if a never reaches zero, the answer is –1.

3. Full Solution Approach  
   1. Read A and B into 64-bit integers a and b.  
   2. For step = 0,1,2,… up to 60:  
      a. If a > b, swap(a, b) so that a ≤ b.  
      b. If a == 0, output the current step count and terminate.  
      c. Perform the move:  
         • b := b – a  
         • a := 2 * a  
   3. If after 60 moves a is never zero, output –1.

4. C++ Implementation with Detailed Comments  
```cpp
#include <bits/stdc++.h>
using namespace std;

// We use 64-bit integers because A+B can be up to nearly 2^31.
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    long long a, b;
    cin >> a >> b;               // Read initial counts

    // Try at most 60 moves
    for (int moves = 0; moves <= 60; moves++) {
        // Ensure a is the smaller or equal box
        if (a > b) 
            swap(a, b);

        // If the smaller box is empty, all balls are in the other box
        if (a == 0) {
            cout << moves << "\n";
            return 0;
        }

        // Move from the larger box (b) into the smaller one (a):
        // - the larger loses 'a' balls
        // - the smaller doubles
        b -= a;
        a *= 2;
    }

    // If we did not reach a == 0 within 60 moves, it's impossible
    cout << -1 << "\n";
    return 0;
}
```

5. Python Implementation with Detailed Comments  
```python
import sys

def min_moves_to_unify(a, b):
    # We only need to try up to 60 moves
    for moves in range(61):
        # Ensure a <= b by swapping if needed
        if a > b:
            a, b = b, a
        # If the smaller pile is empty, we are done
        if a == 0:
            return moves
        # Move from b to a: b loses a, a doubles
        b -= a
        a <<= 1  # same as a = a * 2
    # If no solution found in 60 moves, it's impossible
    return -1

def main():
    data = sys.stdin.read().strip().split()
    if not data:
        return
    A, B = map(int, data[:2])
    print(min_moves_to_unify(A, B))

if __name__ == "__main__":
    main()
```

Explanation Highlights:  
- We always transfer from the box with more balls into the one with fewer balls.  
- This move pattern strictly decreases one box and doubles the other, preserving the total.  
- If it is possible to consolidate all balls into one box, this greedy process will reach one box having zero balls within a small number of steps (≤ 60). Otherwise, we report –1.