We need to parse the problem. There are two numbers a and b (1 ≤ a, b ≤ 10^9). The fox can perform operations on either piece (presumably on each piece independently). For a piece with mass m, the fox can do one of three possible transformations if certain divisibility conditions hold:
- If m is divisible by 2, then she can eat exactly half of the piece, i.e., replace m with m/2. So operation: m -> m/2 (if 2|m).
- If m is divisible by 3, then she can eat exactly two-thirds, meaning she leaves one-third? Wait: "if the mass of a certain piece is divisible by three, then I can eat exactly two-thirds". That means she eats 2/3 of the piece, leaving 1/3. So operation: m -> m/3 (since after eating 2/3, the remaining is 1/3). But careful: "eat exactly two-thirds" means she consumes two thirds of the piece, so the remaining mass is one third. So yes: m -> m/3.
- If m is divisible by 5, then she can eat four-fifths, meaning she leaves one-fifth. So operation: m -> m/5.
Thus, from a number, we can apply operations: if divisible by 2, we can divide by 2; if divisible by 3, divide by 3; if divisible by 5, divide by 5. But note: Are we allowed to divide by 2, 3, or 5 only when that condition holds, and each operation counts as one step. We can apply operations to either piece (a or b) in any order. Goal: make the two pieces equal. We need minimum total number of operations.
If initially a == b, answer 0.
If impossible, output -1.
Observations: The operations only reduce the mass (since dividing by numbers >1). So the mass can only decrease. We cannot increase mass. So if we want to make two numbers equal, we must reduce both numbers by dividing by 2,3,5 until they become equal. Since they can only go down, the final equal value must be a common divisor of the original numbers (after applying some series of divisions). Actually, because we can only divide by these primes, the numbers we can get from a given starting number are numbers of the form a / (2^i * 3^j * 5^k) where i,j,k are non-negative integers, provided that the division steps are valid (i.e., each intermediate step is divisible by the factor we are dividing). Since we can only divide when divisible, the order matters but essentially we can divide by 2,3,5 as many times as possible; after each division, the number changes, and further divisions may become possible or not. However, any sequence of allowed divisions will result in a number that is a divisor of the original number and only contains primes 2,3,5? Not necessarily: If we divide by 2,3,5 only, we are essentially removing factors of 2,3,5 from the number. But the number might have other prime factors. For example, a = 7. Since 7 is not divisible by 2,3,5, no operation can be applied to it. So it stays at 7 forever. So from any starting number, the reachable numbers are those you get by repeatedly dividing by 2,3,5 while the number remains integer. This is equivalent to: you can remove factors of 2,3,5 as much as you want, but you cannot remove other prime factors. So the final number will be the original number divided by some product (2^i * 3^j * 5^k), provided that the division yields an integer, i.e., the original number must contain at least i factors of 2, j of 3, k of 5. So the reachable set from n is: n' = n / (2^i 3^j 5^k) for some nonnegative integers i,j,k such that n is divisible by 2^i 3^j 5^k. That means n' has the same prime factors as n except possibly fewer 2,3,5, but it retains any other prime factors (like 7, 11, etc.) unchanged. So operations only affect the exponents of 2,3,5. Other prime factors cannot be altered.
Now, we want to make a and b equal after operations. Let the final equal mass be g. Then g must be reachable from both a and b. That means g = a / (2^{i1} 3^{j1} 5^{k1}) = b / (2^{i2} 3^{j2} 5^{k2}) for some nonnegative exponents. Equivalently, a / b = (2^{i1}3^{j1}5^{k1}) / (2^{i2}3^{j2}5^{k2}) = 2^{i1-i2} 3^{j1-j2} 5^{k1-k2}. So the ratio a/b must be a rational number whose numerator and denominator are composed only of primes 2,3,5. Moreover, after removing common factors, the ratio must be of the form 2^x * 3^y * 5^z where x,y,z can be any integers (positive, negative, or zero). Actually, if we write a and b in terms of their prime factorizations: a = 2^a2 * 3^a3 * 5^a5 * r_a, where r_a is the product of all other primes (not 2,3,5). Similarly b = 2^b2 * 3^b3 * 5^b5 * r_b. For g to be reachable, g must have the same r_a = r_b? Because we cannot alter other primes. From a, g must have the same r_a factor (since we only divide by 2,3,5, the leftover part r_a remains unchanged). So for a and b to have a common reachable value g, we need r_a = r_b. In other words, after removing all factors of 2,3,5 from a and b, the remaining "core" numbers must be equal. Because that core cannot be changed. So the necessary and sufficient condition for possibility is that a / (2^a2 3^a3 5^a5) equals b / (2^b2 3^b3 5^b5). Let's denote that core as c. So c = a / (2^a2 3^a3 5^a5) = b / (2^b2 3^b3 5^b5) must hold. Then we can reduce both a and b to c * (2^{a2'} 3^{a3'} 5^{a5'})? Wait, more precisely: from a we can only remove some factors of 2,3,5; we cannot change c. So any reachable number from a is of the form c * 2^{x2} * 3^{x3} * 5^{x5} where 0 ≤ x2 ≤ a2, 0 ≤ x3 ≤ a3, 0 ≤ x5 ≤ a5. Similarly from b: c * 2^{y2} * 3^{y3} * 5^{y5} where 0 ≤ y2 ≤ b2, 0 ≤ y3 ≤ b3, 0 ≤ y5 ≤ b5. To have a common number, we need to find exponents e2,e3,e5 such that e2 ≤ a2, e3 ≤ a3, e5 ≤ a5 and also e2 ≤ b2? Wait, both must be able to reach the same number. For a to reach g = c * 2^{e2} * 3^{e3} * 5^{e5}, we need that after dividing a, we have removed (a2 - e2) factors of 2, (a3 - e3) factors of 3, (a5 - e5) factors of 5. That's possible if e2 ≤ a2, e3 ≤ a3, e5 ≤ a5. Similarly, from b, we need e2 ≤ b2, e3 ≤ b3, e5 ≤ b5. So we need a common exponent vector (e2, e3, e5) that is ≤ both (a2,a3,a5) and (b2,b3,b5). The greatest such common exponent is the componentwise minimum: min(a2,b2), min(a3,b3), min(a5,b5). Indeed, we can choose any exponent ≤ each maximum, but to minimize operations we might want to reduce as much as necessary? Wait, we want to make the pieces equal. We can choose any g reachable from both. Since we can only reduce, the final equal mass could be smaller than both. But we want to minimize the number of operations. So we need to find a common reachable mass g (i.e., choose exponents e2,e3,e5) such that the total number of divisions performed on both pieces is minimized.
Operations: Each division by 2,3,5 counts as one operation. Starting from a, we need to remove (a2 - e2) factors of 2, (a3 - e3) factors of 3, (a5 - e5) factors of 5. But the order may matter for counting? Actually, each division reduces the current number by dividing by one of the allowed primes. Since each division reduces the exponent of that prime by 1 (if divisible). So to reduce exponent of 2 from a2 to e2, we need (a2 - e2) divisions by 2. Similarly for 3 and 5. So total operations on piece a = (a2 - e2) + (a3 - e3) + (a5 - e5). Similarly for piece b: (b2 - e2) + (b3 - e3) + (b5 - e5). The total operations = sum over both pieces.
We need to choose e2 ≤ a2,b2; e3 ≤ a3,b3; e5 ≤ a5,b5 to minimize total = (a2 + a3 + a5) + (b2 + b3 + b5) - 2(e2+e3+e5). Since (a2+a3+a5)+(b2+b3+b5) is constant, minimizing total is equivalent to maximizing e2+e3+e5, i.e., the sum of the exponents of the final common number. So we want the largest possible common exponents. However, we cannot arbitrarily increase exponents; we can only reduce from a and b. e2 cannot exceed min(a2,b2) because we need it to be ≤ both. Similarly for e3 and e5. So the maximum possible e2 = min(a2,b2), e3 = min(a3,b3), e5 = min(a5,b5). So the optimal is to set e2 = min(a2,b2), e3 = min(a3,b3), e5 = min(a5,b5). That yields the final equal mass g = c * 2^{min(a2,b2)} * 3^{min(a3,b3)} * 5^{min(a5,b5)}. This is essentially the "greatest common divisor in terms of 2,3,5" while preserving the core c. Actually, it's like: a = c * 2^{a2}3^{a3}5^{a5}, b = c * 2^{b2}3^{b3}5^{b5}. Then the maximal common reachable number is c * 2^{min(a2,b2)} * 3^{min(a3,b3)} * 5^{min(a5,b5)}. This is indeed the largest number that both can be reduced to? But note that we could also reduce further (i.e., lower exponents) to a smaller number, but that would increase total operations because we would need to do more divisions (since we remove more factors). Actually, if we reduce further, we would have to remove additional factors from both pieces, increasing operations. So the minimal total operations is achieved by making the final number as large as possible while still reachable by both. That is, we want to stop at the largest common divisor that can be reached by dividing only by 2,3,5. However, is there any scenario where we might want to reduce one piece more than the other in a certain prime factor? Wait, the exponents e2, e3, e5 must be the same for both final numbers. But they could be less than min for some prime, but then we would have to remove extra factors from both pieces, resulting in more operations. Since each extra removal adds 2 operations total (one from each piece for that factor). So indeed maximizing each e individually maximizes the sum. There's no trade-off because e2, e3, e5 are independent. So the optimal is e_i = min(a_i, b_i). That gives minimal total operations = (a2+b2 - 2min(a2,b2)) + similarly for 3 and 5 = |a2 - b2| + |a3 - b3| + |a5 - b5|. Because a2+b2 - 2min = max - min? Actually, if a2 >= b2, then a2+b2 - 2b2 = a2 - b2 = |a2 - b2|. If a2 <= b2, then a2+b2 - 2a2 = b2 - a2 = |a2 - b2|. So indeed total operations = |a2 - b2| + |a3 - b3| + |a5 - b5|. This makes intuitive sense: we need to remove the excess prime factors (2,3,5) from the larger exponent pieces to match the smaller exponents. That is, we reduce the piece with higher exponent down to the lower exponent by dividing by that prime as many times as needed.
But we must also consider that the core c must be equal. If c differs, it's impossible. So algorithm:
- For a and b, factor out all 2,3,5. That is, compute a2 = exponent of 2 in a, similarly a3, a5, and the rest a_rest = a / (2^{a2} * 3^{a3} * 5^{a5}). Similarly for b. If a_rest != b_rest, return -1.
- Else, answer = |a2 - b2| + |a3 - b3| + |a5 - b5|.
Check examples:
Example 1: a=15, b=20.
15 = 3*5 => a2=0, a3=1, a5=1, a_rest=1.
20 = 2^2 *5 => b2=2, b3=0, b5=1, b_rest=1. So rest equal. Then differences: |0-2|=2, |1-0|=1, |1-1|=0 => total=3. Output 3. matches.
Example 2: 14, 8.
14 = 2*7 => a2=1, a3=0, a5=0, a_rest=7.
8 = 2^3 => b2=3, b3=0, b5=0, b_rest=1? Actually 8/2^3 =1, so b_rest=1. 7 != 1 => impossible => -1. matches.
Example 3: 6,6 => a2=1,a3=1,a5=0,b same => differences 0 => answer 0.
Edge cases: a and b up to 1e9, so factoring 2,3,5 is trivial.
Thus solution appears simple. But we must ensure that this reasoning is correct: Is it always optimal to remove exactly the differences? Could there be cases where it's better to remove additional factors beyond the min to achieve equality faster? That would increase operations because you'd need to remove more from both pieces, but maybe you could remove extra from one piece and then also remove something else? Actually, if we remove extra factors from both pieces, total operations increase because we add operations to both. But what if removing extra factors enables additional operations on the other piece? That doesn't make sense; operations are independent on each piece. The only way to make pieces equal is to bring them to a common number. If we go to a smaller common number, we need to remove more factors from both pieces. That always increases total number of divisions (except possibly if one piece cannot be reduced enough? but we can reduce any piece as long as divisible). So minimal is indeed the largest common reachable number.
But we need to confirm that the operations allowed exactly correspond to dividing by 2,3,5 individually, i.e., each operation reduces the exponent of that prime by exactly 1. That's true because if divisible by 2, we can do m -> m/2 (half). That reduces exponent of 2 by 1 (and possibly other primes remain same). However, note: if a number is divisible by 2 but also by other primes, dividing by 2 only reduces the power of 2 by one, not affecting other primes. So yes, it's exactly reducing exponent of 2 by 1. Similarly dividing by 3 reduces exponent of 3 by 1, by 5 reduces exponent of 5 by 1.
One nuance: The fox says: "If the mass of a certain piece is divisible by two, then I can eat exactly a half of the piece. If the mass of a certain piece is divisible by three, then I can eat exactly two-thirds, and if the mass is divisible by five, then I can eat four-fifths." So indeed each operation reduces mass by factor 2, 3, or 5 respectively. However, note the phrasing: for divisible by 3, she eats exactly two-thirds, so remaining is one-third; that's dividing by 3. For divisible by 5, she eats four-fifths, remaining one-fifth, i.e., dividing by 5. So correct.
Thus total operations = sum of absolute differences of exponents of 2,3,5.
Now, what about the possibility of performing operations on both pieces interleaved? That doesn't affect count.
Hence solution is straightforward.
Now confirm with some other cases. Suppose a=2, b=4. a2=1, b2=2; rest =1 for both. differences: |1-2|=1 => answer=1. Indeed, fox can take half of b (4->2) or half of a? a=2 cannot become 4 because cannot increase. So she can reduce b to 2 with one operation: OK.
Suppose a=6, b=10. 6=23, 10=25. rests: 6/(23)=1, 10/(25)=1 => rest equal. Exponents: a2=1,a3=1,a5=0; b2=1,b3=0,b5=1. diffs: |1-1|+|1-0|+|0-1| = 0+1+1=2. Is it possible in 2 operations? Reduce 6: need to remove factor 3 (divide by 3) to lose the 3, but also need to remove factor 5 from 10? Actually, we need final common number. The minimal common reachable number is 2^{min(1,1)}=2^1, 3^{min(1,0)}=3^0,5^{min(0,1)}=5^0 => final=2 * rest=2. So we need to reduce 6 to 2: divide by 3 (one op). 10 to 2: divide by 5 (one op). Total 2. Works.
Now, is there any scenario where it's impossible even if rest equal? Actually rest equal is necessary and sufficient because the core that is not divisible by 2,3,5 cannot be changed. If cores are equal, we can always reduce each piece to the target by dividing by appropriate primes as long as the exponent differences are non-negative? But what if a piece has insufficient exponent to reduce to min? Wait, we set e_i = min(a_i,b_i). For piece a, we need to reduce from a_i to min, so we need to remove (a_i - min). This is possible only if a_i >= min, which is true because min ≤ a_i. Similarly for b. So reachable. So condition for possibility is that after removing all 2,3,5 factors, the remaining numbers are equal. That's it.
Thus solution: compute for a and b the number after dividing out all factors of 2,3,5. Let's denote na = a, nb = b. While na%2==0: cnt2a++; na/=2. Similarly for 3,5. Then restA = na. Same for b. If restA != restB: print -1. Else print abs(cnt2a-cnt2b) + abs(cnt3a-cnt3b) + abs(cnt5a-cnt5b).
But wait: There is one subtlety: The fox's operations are as described. However, dividing by 3 or 5 reduces the number by factor 3 or 5, but also may affect divisibility by other primes? It's just division. So indeed each step reduces exponent of that prime by 1. So it's correct.
Thus solution is trivial. But is there any twist? Let's read problem statement again: "If the mass of a certain piece is divisible by two, then I can eat exactly a half of the piece. If the mass of a certain piece is divisible by three, then I can eat exactly two-thirds, and if the mass is divisible by five, then I can eat four-fifths. I'll eat a little here and there and make the pieces equal". So indeed each operation reduces mass by factor 2,3,5 respectively.
Now, the goal: minimize number of operations. Since each operation reduces mass (i.e., we can't increase), the total number of operations is the number of division steps applied to both pieces.
Our reasoning that minimal total steps equals sum of absolute differences of exponents of 2,3,5 is sound because those operations are independent and we want to match the exponents to the min. However, could there be a scenario where performing a division on one piece could also affect the ability to later divide by another prime? But that's irrelevant: we can always apply divisions in any order as long as at each step the number is divisible by the prime we are dividing by. Since we are dividing by 2,3,5 only, and we start with a number that has certain exponents, we can always remove all desired factors in any order because dividing by a prime doesn't affect divisibility by other primes in terms of exponent: e.g., if you have exponent e2 for 2 and e3 for 3, dividing by 3 reduces e3 by 1, but doesn't affect e2. The number remains divisible by 2 if e2>0. So you can always remove all excess factors in any sequence.
Thus answer is straightforward.
But wait: The problem might allow operations on both pieces simultaneously? It says: "the fox should make the pieces equal as quickly as possible. Find the minimum number of operations the fox needs to make pieces equal." The fox can apply operations to either piece. Each operation is presumably on one piece. So total count is sum.
We need to output -1 if impossible.
Now consider a=1, b=7. a_rest after removing 2,3,5 is 1, b_rest=7 (since 7 has no 2,3,5). 1 != 7 => impossible => -1. That makes sense; you cannot change 7.
Now test: a=2*7=14, b=7. a_rest=7, b_rest=7. exponents: a2=1, others 0; b2=0. differences=1 => answer=1? But can we make them equal? a=14, b=7. We can divide 14 by 2 to get 7. So one operation. Works.
Now, what about a=237=42, b=57=35. a_rest=7? 42/(23)=7, b_rest=7. exponents: a2=1,a3=1,a5=0; b2=0,b3=0,b5=1. diffs=1+1+1=3. That means we need 3 ops: reduce 42 to 7? Actually target largest common = 7 * 2^min(1,0)=2^0, 3^min(1,0)=3^0,5^min(0,1)=5^0 => 7. So from 42 to 7: need remove factor 2 and factor 3 => two divisions. From 35 to 7: need remove factor 5 => one division. Total 3. Works.
Now consider a=223=12, b=233=18. rest after removing 2,3? 12/(2^23^1)=1, 18/(2^13^2)=1, rest=1. exponents: a2=2,a3=1,a5=0; b2=1,b3=2. diffs=1+1=2. Target largest common = 2^1*3^1=6. Operations: 12->6 divide by 2 (one op). 18->6 divide by 3 (one op). Total 2.
Now, is there any scenario where we might want to reduce more than min on both to achieve equality with fewer operations? Suppose we have a=8 (2^3), b=4 (2^2). According to our formula, diffs = |3-2| = 1 => 1 op: reduce 8 to 4. That's optimal. Could we reduce both to 2? That would require 8->2 (divide by 2 three times) and 4->2 (divide by 2 once) total 4 ops, worse.
Now, consider a=2^3, b=2^3*5? Actually b has extra factor 5. That will require removing that 5, but we cannot remove 5 from a because a doesn't have 5. So we must reduce both to something that has no 5? The target final number must have exponent of 5 = min(a5,b5). a5=0, b5=1, so min=0 => we must remove the 5 from b. So that's one op. That's necessary. So absolute differences sum captures it.
Now, is there any scenario where the core equality condition might be more subtle? Suppose a= 2*7, b= 7. core equal => 7. So answer = |1-0|=1. Works.
Thus solution is simple.
Now, confirm the problem statement: "If the mass of a certain piece is divisible by two, then I can eat exactly a half of the piece. If the mass of a certain piece is divisible by three, then I can eat exactly two-thirds, and if the mass is divisible by five, then I can eat four-fifths." So indeed it's division by 2,3,5.
One nuance: When dividing by 3, she eats exactly two-thirds, which leaves one-third. So the factor is 3, not 3/2. So yes.
Thus solution is to compute exponents.
Implementation:
For each number n:
cnt2 = 0; while n%2==0: n/=2; cnt2++
similarly for 3,5.
rest = n.
If rest_a != rest_b: print -1.
Else: ans = abs(cnt2a - cnt2b) + abs(cnt3a - cnt3b) + abs(cnt5a - cnt5b); print ans.
Edge Cases: n up to 1e9, fits in int32. Use long long? Actually 1e9 fits in 32-bit int, but dividing many times may be safe with int. Use long long for safety, but not necessary.
Potential pitfalls: If one number is 0? But constraints: 1 ≤ a,b ≤ 1e9, so positive.
Now test with more examples: a=1, b=1 => rest 1, diffs 0 => 0. Good.
a=1, b=2 => rest a=1, b after dividing 2: b rest=1? b=2: cnt2=1, rest=1. rest equal. diffs: |0-1|=1 => answer 1: divide 2 by 2 => 1. Yes.
Now consider a=2, b=3: a rest: a=2 => cnt2=1, rest=1. b=3 => cnt3=1, rest=1. rest equal. diffs: |1-0|+|0-1| = 1+1=2. Can we make them equal? Need final common largest number: min(1,0)=0 for 2, min(0,1)=0 for 3 => target 1. So we need to reduce 2 to 1 (divide by 2) and 3 to 1 (divide by 3). That's 2 ops. Yes.
Now, what about a=4, b=9: 4=2^2, rest=1; 9=3^2, rest=1. diffs=2+2=4. Target 1. Need 4->1: divide by 2 twice (2 ops). 9->1: divide by 3 twice (2 ops). Total 4. Could we do something else? Possibly reduce 4 to 2? But 9 cannot become 2 because 9's core is 1 and 9 only divisible by 3. 2 would be 2^1 *1, but 9 doesn't have factor 2. So no. So 4 is correct.
Now, what about a=6, b=18: 6=23, 18=23^2. rest both 1. exponents: a2=1,a3=1; b2=1,b3=2. diffs: |1-1|+|1-2|=1 => total 1. Target: 2^1*3^1=6 (min exponents: 2^1, 3^1). So b needs to lose one factor 3: 18/3=6. a remains 6. So 1 operation. Good.
Now consider a=12, b=18: a=2^23, b=23^2. exponents: a2=2,a3=1; b2=1,b3=2. diffs: |2-1|+|1-2| =1+1=2. target 2^13^1=6. So reduce a: 12->6 (divide by 2) one op? Actually 12/2=6, but we need to remove one 2 and keep 3? 12/2=6 has exponents 2^13^1, that's exactly target. So one op on a. Reduce b: 18->6 (divide by 3) one op. total 2. Works.
Now consider a=25, b=125: a=5^2, b=5^3. rest both 1. diffs: |0-0|+|0-0|+|2-3|=1. Target 5^2? Actually min exponents for 5 is 2, so target =5^2=25. So reduce b by dividing by 5 once: 125->25. total 1. Good.
Now, is there any scenario where the order of operations could be such that we need to perform operations in a specific order because dividing by 2 might make the number not divisible by 5 later? But if we need to remove a factor of 5, we must do it while the number is divisible by 5. But if we first remove some 2's, does that affect divisibility by 5? No, because 5 is prime and independent. Dividing by 2 doesn't change divisibility by 5. However, there is a subtle case: Suppose we need to remove a factor of 2 and a factor of 5. It doesn't matter in which order we do them; the number remains divisible by the other prime as long as the exponent for that prime is still >0. If we remove the 5 first, then number becomes maybe even? It stays divisible by 2 if exponent >0. So no problem.
What about dividing by 3 and then by 2? Similarly.
Thus independence.
Now, potential misinterpretation: Could the fox also combine operations? The problem says "If the mass of a certain piece is divisible by two, then I can eat exactly a half of the piece. If the mass of a certain piece is divisible by three, then I can eat exactly two-thirds, and if the mass is divisible by five, then I can eat four-fifths." It doesn't say she can only apply one operation at a time. She can apply multiple operations sequentially. So each division is an operation.
Thus answer stands.
But wait: The problem states "I'll eat a little here and there and make the pieces equal". This could imply she can eat from both pieces, not necessarily one at a time? But each eating action is on one piece. So operations are per piece.
Now, some might think that the fox could eat from both pieces simultaneously in one operation? That would be ambiguous, but typical interpretation is each action is on one piece.
Thus solution is correct.
Now let's check if any more complex case might break the simple formula, e.g., if we have a = 2^2 * 7, b = 3^2 * 7. Rest equal 7. exponents: a2=2,a3=0; b2=0,b3=2. diffs = 2+2=4. Target = 7. So we need to remove both 2's from a (2 divisions) and both 3's from b (2 divisions) -> 4. Could there be a way to reduce both to 7 by alternating? That's still 4 operations. No alternative.
Now consider a = 2^1 * 3^1 * 7, b = 5^1 * 7. diffs = 1+1+1=3. Target =7. Remove 2 and 3 from a (2 ops) and remove 5 from b (1 op) -> 3.
Now, could there be scenario where we could reduce both to a number larger than the one with min exponents? For instance, suppose a = 2^2 * 7, b = 2^1 * 7. Then min exponents for 2 =1, so target =2^1 *7 = 14. That's larger than 7. But could we instead reduce both to 7? That would be possible: a would need to remove both 2's (2 ops), b would need to remove its 2 (1 op) total 3 ops vs target 14 only need to remove one 2 from a (1 op) and nothing from b -> 1 op. So clearly the larger target yields fewer ops.
Thus maximizing each exponent is beneficial.
Therefore answer is absolute differences.
Thus solution is O(log a + log b) time.
Now, we should think about correctness regarding "minimum number of operations". Are there any alternative strategies where the fox could sometimes eat from a piece more than needed to enable a different reduction? But since the only operations are division by 2,3,5, any reduction only reduces exponents. There's no way to increase exponents or change other primes. So the only way to achieve equality is to match the exponents.
One nuance: Could there be a situation where the fox could achieve equality without making the pieces have exactly the same final mass, but by making them equal at some intermediate point before completing all divisions? The problem says "make the pieces equal" likely meaning after some number of operations, the masses become equal. That equal mass could be intermediate; but the process may have multiple steps. However, we are counting total operations until equality is achieved. It could be that we don't need to fully reduce to the largest common reachable number if we can make them equal earlier by reducing one piece more than the other? For example, suppose a=8 (2^3), b=4 (2^2). The largest common reachable number is 4 (since min exponent=2). That yields operations: reduce a from 8 to 4 (one division), total ops=1. But could we perhaps reduce both to 2 (8->2 via 3 divisions, 4->2 via 1 division) and achieve equality earlier? The moment we do the first division on a: 8->4, now both are 4? Actually b=4, after that operation they are equal (4,4) after 1 op. That's using the largest common reachable. So we stop at equality. If we instead aimed for 2, we would have to do at least 1 op on b (4->2) and 3 ops on a (8->2), but after the first op on b, b becomes 2 while a is still 8, not equal. After a's first op, a becomes 4, b=2 not equal. After a's second op, a=2, b=2, so equality achieved after 3 ops total? Actually sequence: start (8,4). Option 1: apply to a: /2 -> (4,4) equality after 1 op. Option 2: apply to b: /2 -> (8,2). Then apply to a: /2 -> (4,2) not equal; /2 again -> (2,2) after total 3 ops. So the minimal is 1. So larger target is better.
But could there be a scenario where making both pieces reduce to a smaller number results in fewer total operations? For example, suppose a=2^3 * 3^0, b=2^0 * 3^3. Rest=1. exponents: a2=3,a3=0; b2=0,b3=3. Differences sum = 3+3=6. Largest common reachable number is 2^0*3^0 =1, requiring removal of all 2's from a (3 ops) and all 3's from b (3 ops) =6. Could we achieve equality earlier? Suppose we reduce a by dividing by 2 twice to get 2 (2^1), and reduce b by dividing by 3 twice to get 3 (3^1). Those are not equal. To get equal, they must have same prime factors. Since rest=1, the only numbers reachable are powers of 2 and 3 separately. They can never have both 2 and 3 unless one of them reduces to 1 and the other reduces to 1? Actually, the numbers can only contain 2's from a and 3's from b. They cannot acquire new primes. So the only possible common numbers are those that are both of form 2^x and 3^y with same value, which forces the value to be 1 (since only 1 = 2^0 = 3^0). So they must both become 1. So the minimum ops is indeed 6. There's no alternative because until both are 1, they cannot be equal. So the formula works.
But consider case where rest not 1 and both contain some other prime factor c. Suppose a = 2^3 * c, b = 2^2 * c. Largest common = 2^2 * c. That's the only candidate? Actually, could we aim for 2^1 * c? That would require removing an extra 2 from both a and b (but b only has 2^2, so we can remove one more from b to get to 2^1*c). But then a would have to remove two more to get from 2^3 to 2^1, which is more ops. So minimal is indeed the one with max common exponent.
But is it always that the largest common reachable number yields minimal total ops? Let's formalize: total ops = (a2 - e2) + (a3 - e3) + (a5 - e5) + (b2 - e2) + (b3 - e3) + (b5 - e5) = (a2+b2) + (a3+b3) + (a5+b5) - 2(e2+e3+e5). Since the first sum is constant, minimizing total ops is equivalent to maximizing S = e2+e3+e5 subject to constraints: 0 ≤ e2 ≤ a2,b2; 0 ≤ e3 ≤ a3,b3; 0 ≤ e5 ≤ a5,b5. Since the constraints are independent, maximum S is sum of min(a_i,b_i). So it's optimal.
Thus solution is confirmed.
Now, could there be any scenario where we cannot achieve the target because we need to perform divisions in a specific order and some intermediate step might become not divisible? For example, suppose we want to reduce a = 2^2 * 3 (12) to 2^1 * 3^1 (6). We need to remove one factor of 2. We can do 12 / 2 = 6 directly. That's fine. Or we could do 12 / 3 = 4 then /2? But 4/2=2, that would overshoot. But we want to stop at 6. Actually, if we do 12/3 first, we get 4. Then we need to remove a factor of 2 to get 2, which is not 6. That's not the target. So we need to choose which divisions to apply. To achieve exactly e2=1, e3=1, we need to remove exactly (a2 - e2) = 1 factor of 2, and (a3 - e3) = 0 factor of 3. So we must not divide by 3. So we simply divide by 2 once. That's fine. In general, we can choose to only divide by primes we need to reduce. The only tricky part: If a number is, say, 2*5=10 and we want to remove the factor 5, we can do /5 directly. If we want to remove only 2, we can do /2. So we can always apply the required divisions individually, as long as the number remains divisible by the required prime at the moment we apply the division. Initially, the number is divisible by that prime because it has the exponent. After some divisions, the exponent may reduce but we only remove exactly the required number of that prime. So if we need to remove x factors of 2, we do x divisions by 2. It remains divisible by 2 until we have removed all of them; after removing some, the number still has remaining 2 factors if needed. So we can always do them.
Thus no issue.
Now, consider the possibility that the fox could perform operations on both pieces interleaved, but that doesn't affect count.
Thus answer is straightforward.
One might think about using BFS? But given constraints up to 1e9, exponential? No, we derived simple formula.
Thus solution code: read a,b. Compute function to get (cnt2,cnt3,cnt5,rest). Use loops.
Potential integer overflow? Use long long for rest, but after dividing out 2,3,5, the rest may be up to 1e9, still fits in 32-bit.
Now, about output: If impossible, -1, else ans.
Let's test with random cases.
Case: a=2^10 * 3^5 * 7, b=2^5 * 3^10 * 7. rest equal (7). diffs = |10-5|+|5-10|+|0-0| =5+5=10. answer =10.
Check: a reduce 2^5 to match b's 2 exponent: need remove 5 twos; b reduce 3^5? Actually a has 3^5, b has 3^10; min=5. So a remove 0 threes, b remove 5 threes. So total: a remove 5 twos (5 ops), b remove 5 threes (5 ops) =10. Could we aim for lower exponents? That would increase ops. So optimal.
Now, corner case: a=0? Not allowed.
Thus solution appears correct.
We should also consider that the problem may consider the fox can eat from both pieces in a single operation? The problem statement: "Find the minimum number of operations the fox needs to make pieces equal." It doesn't explicitly say that one operation is eating from one piece. Typically, an operation is an action. The fox can choose which piece to eat from each time. So each action is on one piece. So total number of actions = number of divisions performed.
One more nuance: The fox might be able to eat from a piece multiple times? Yes, that's multiple operations.
Thus answer stands.
Now, is there any possibility that the fox could combine eating half and two-thirds etc. in one operation? The description says "If the mass of a certain piece is divisible by two, then I can eat exactly a half of the piece." So that is one type of operation. It doesn't say she can eat both half and two-thirds at once. So each operation is one of these three on one piece.
Thus answer is correct.
Let's ensure that reading input and output matches examples.
Now, write solution in C++ (or Python). We'll do it.
Implementation steps:
- define a function that takes an integer n, and returns a tuple (cnt2, cnt3, cnt5, rest).
- For a and b, compute.
- if rest_a != rest_b: print -1.
- else: ans = abs(cnt2a-cnt2b)+abs(cnt3a-cnt3b)+abs(cnt5a-cnt5b); print ans.
Edge Cases: Since numbers up to 1e9, loops for dividing by 2,3,5 are at most log2(1e9) ~ 30 iterations each, so trivial.
Now, confirm with more complex test where rest is not 1 but some other number. Eg: a=277? Actually primes other than 2,3,5 must match exactly. So if a=14 (27), b=28 (47) -> a=27, b=2^27. rest both 7, diffs=1, answer 1. a=14->7 by dividing by 2, b=28->7 by dividing by 2 twice? Actually b need to go to 7: remove two twos ->2 ops. Wait careful: a: 14/2=7 (1 op). b: 28 ->? 28/2=14 (1 op) then 14/2=7 (2 ops) total 2. But our diffs: a2=1,b2=2 => |1-2|=1. But that says total ops=1? That seems off. Because we computed total ops as sum of absolute differences: |1-2|=1. But we need to reduce both to the common largest number. What is the largest common reachable number? a = 2^1 * 7^1, b = 2^2 * 7^1. min exponent for 2 is 1. So target = 2^1 * 7 = 14? Wait, a already is 14? Actually a is 14 (27), b is 28 (47). min exponent for 2 is min(1,2)=1, so target = 2^1 * 7 = 14. That means we want final equal mass = 14. Then a is already 14, so 0 ops on a. b needs to reduce from 28 to 14: divide by 2 once (28/2=14). So total ops = 1. That matches the answer 1. But earlier I mistakenly thought target was 7; that would be smaller but not optimal. So indeed answer=1. Good.
Thus formula works.
Now test: a=14, b=7. a=2*7, b=7. rest both 7, a2=1,b2=0 => diff=1, answer 1. a must reduce from 14 to 7 (one op). b unchanged. Works.
Thus all good.
Now, we should check if there's any scenario where the fox might not be able to achieve the target with exactly those number of operations because of ordering? For example, a=12 (2^23), b=18 (23^2). Target = 2^1*3^1=6. We can achieve by dividing a by 2 (12->6) and b by 3 (18->6). That's 2 ops. But could we achieve equality with fewer than 2 ops? Suppose we divide a by 3: 12->4, b by 2: 18->9. Then we have (4,9) not equal. Then we could do further ops. So not less.
Now, can we achieve equality with 1 op? Possibly if we reduce one to match the other exactly: 12->4 (divide by 3) gives 4, but b is 18, not equal. 12->6 (divide by 2) gives 6, b is 18. 18->6 (divide by 3) gives 6, a is 12. So no single op makes both equal. So 2 is minimal.
Thus answer is correct.
Thus solution is straightforward.
Now, double-check the problem statement: "The little bears realize that the fox's proposal contains a catch. But at the same time they realize that they can not make the two pieces equal themselves. So they agreed to her proposal, but on one condition: the fox should make the pieces equal as quickly as possible. Find the minimum number of operations the fox needs to make pieces equal." So we assume the fox will minimize operations. Good.
Now, implement.
Implementation details:
We'll read ints a,b.
Define function to compute counts:
void factor(int n, int &cnt2, int &cnt3, int &cnt5, int &rest) {
cnt2=0; while(n%2==0) { n/=2; cnt2++; }
cnt3=0; while(n%3==0) { n/=3; cnt3++; }
cnt5=0; while(n%5==0) { n/=5; cnt5++; }
rest = n;
}
Alternatively return a tuple.
Then compare rest.
If rest differ: cout << -1 << endl;
Else cout << abs(cnt2a-cnt2b) + abs(cnt3a-cnt3b) + abs(cnt5a-cnt5b) << endl;
Edge Cases: The numbers could be up to 1e9, factoring by 2,3,5 only is safe.
Now, is there any scenario where the fox could use a combination of operations to achieve equality faster by not necessarily reducing to the maximal common number, but by making them equal at an intermediate step when they are not both fully reduced? For example, if we have a = 6 (23), b = 4 (2^2). The largest common reachable number? Compute cores: 6/(23)=1, 4/(2^2)=1, rest=1. exponents: a2=1,a3=1; b2=2,b3=0. min exponents: e2=1, e3=0 => target =2^1=2. So we need to reduce a to 2 (divide by 3) and b to 2 (divide by 2) => total 2 ops. But could we achieve equality with 1 operation? Let's see: if we divide b by 2: b becomes 2. Now a=6, b=2, not equal. If we divide a by 2: a becomes 3, b=4. Not equal. If we divide a by 3: a becomes 2, b=4. Not equal. So 1 op insufficient. So 2 is minimal. Our formula gives |1-2|+|1-0|=1+1=2, correct.
What about a = 12 (2^23), b = 18 (23^2) we already did gives 2.
Now consider a = 30 (235), b = 6 (23). cores: 30/(235)=1, 6/(23)=1. exponents: a2=1,a3=1,a5=1; b2=1,b3=1,b5=0. min exponents: e2=1,e3=1,e5=0 => target = 2*3=6. So we need to reduce a: remove the factor 5 (one op), b unchanged => total 1. Formula: diffs: |1-1|+|1-1|+|1-0|=1. Works.
Now consider a=237=42, b=5*7=35, we did diffs=3.
Thus formula stands.
Now, there is a subtle point: The operation for dividing by 3 is "eat exactly two-thirds", meaning she eats 2/3, leaving 1/3. That is division by 3. Similarly for 5, division by 5. So our mapping to division by prime is correct.
One more nuance: Could the fox eat from a piece and then eat from the same piece again in the same operation? That would be two operations.
Thus answer.
Now, we must produce code. I'll implement in C++.
Potential pitfalls: When dividing by 2,3,5 we need to use integer division. Use while loops.
Now, after obtaining rest, we need to compare rest values. Since they may be large but within int.
All good.
Now, let's also consider whether there is any scenario where the fox could eat from both pieces simultaneously? The problem likely counts each eating action as an operation. So we are good.
Now, I think the solution is correct.
But before finalizing, let's read the problem again to ensure no hidden constraints: "If the mass of a certain piece is divisible by two, then I can eat exactly a half of the piece. If the mass of a certain piece is divisible by three, then I can eat exactly two-thirds, and if the mass is divisible by five, then I can eat four-fifths." This suggests that when divisible by 3, she can eat exactly two-thirds, leaving one-third. So we can treat as dividing by 3. Similarly for 5.
Now, the bears can't make pieces equal themselves. That's just story.
Now, output: required minimum number of operations.
Examples match.
Thus solution is done.
Now, let's write in Python for simplicity, but since we are using C++ environment? Usually Codeforces problem (this is from Codeforces? Possibly problem 385A? Actually it's Codeforces Round #... I recall a problem "Little Pony and Sort by Shift"? No. But it's likely from Codeforces: "Fox and Number Game"? Not exactly. But anyway.
I'll implement in C++.
Pseudo:
#include
#include
using namespace std;
int main() {
int a, b;
cin >> a >> b;
int a2=0,a3=0,a5=0,b2=0,b3=0,b5=0;
int ta = a, tb = b;
while(ta%2==0){ta/=2; a2++;}
while(ta%3==0){ta/=3; a3++;}
while(ta%5==0){ta/=5; a5++;}
while(tb%2==0){tb/=2; b2++;}
while(tb%3==0){tb/=3; b3++;}
while(tb%5==0){tb/=5; b5++;}
if(ta != tb) {
cout << -1 << endl;
} else {
int ans = abs(a2-b2) + abs(a3-b3) + abs(a5-b5);
cout << ans << endl;
}
return 0;
}
That's it.
Now, think about potential overflow or negative numbers? None.
Now, confirm with example:
Input:
15 20
15: 15%2? no