Broken Calculator Keys

The keys on a calculator is bad broken. Only the 5 keys sin, cos, tan, asin, atan are still functional. Respectively, they stand for sine, cosine, tangent, arc-sine, and arc-tangent. Initially the calculator’s display shows ‘0’. Compute the minimum number of key presses, such that the decimal equivalent of the fraction p/q will appear on the calculator! Please assume that the calculator has infinite precision, and that it uses radians for trig functions. Input A number of test cases (≤ 40000), one per line, each with p and q (0 ≤ p ≤ 1000 and 1 ≤ q ≤ 1000). Output Output the answer for each test case, one on each line. Sample Input 01 11 12 Sample Output 0 1 7