Label the nodes of an infinite rooted binary tree with rational numbers by the following rule.
- The root is labeled 1/1.
- If a node is labeled p/q, then its left child is labeled p/(p+q) and its right child is labeled (p+q)/q.

Traverse this tree in breadth first order, visiting the nodes of the same depth from left to right, and read off the rational sequence a1,a2,a3,…. This gives a1=1/1, a2=1/2, a3=2/1, a4=1/3, a5=3/2.
Given p and q, write a program that computes the integer n with an=p/q.