Time limit
2s
Memory limit
128 MB
You have many resistors whose resistance is either 1 or 2.
When several resistors are connected in series, their equivalent resistance is the sum of their resistances. For example, resistors with values R1, R2, R3, ..., Rn connected in series have equivalent resistance R1 + R2 + R3 + ... + Rn.
When several resistors are connected in parallel, their equivalent resistance is the reciprocal of the sum of reciprocals: 1 / ((1 / R1) + (1 / R2) + (1 / R3) + ... + (1 / Rn)).
Given two positive integers a and b, connect resistors in series and parallel so that the equivalent resistance is exactly a / b. Find the minimum number of resistors needed.
The first line contains two positive integers a and b. Both are at most 50,000.
Print the minimum number of resistors needed on the first line. If more than 16 resistors are required, print -1.
To make 6 / 5, first connect resistors of values 1 and 2 in series to make value 3. Then connect that result in parallel with a resistor of value 2.