Fractional tricks
Boy Sergei really wants to learn how to juggle and do tricks. Recently, his friend showed him a trick - asked Sergei think a certain positive fraction, and then call it the denominator and the two fractions p[1]
/ q[1]
and p[2]
/ q[2]
, such that p[1]
/ q[1]
< a / b < p[2]
/ q[2]
, while among smaller fractions with the same denominator as that of the fractions, conceived Sergei. After that Seryozhin each guessed numerator ends, the hidden Sergei fractions.
Serge now he wants to learn to show this trick. Help him in this - write a program that finds the numerator ends, the hidden fraction.
Serge now he wants to learn to show this trick. Help him in this - write a program that finds the numerator ends, the hidden fraction.
Input
In separate lines three fractions are given in format numerator / denominator (p[1]
/ q[1]
, a / b, p[2]
/ q[2]
, where instead of a the question mark "?" is given. All numerators and denominators are positive integers and do not exceed 1000.
Output
Print the minimum number a / b, such that p[1]
/ q[1]
< a / b < p[2]
/ q[2]
. If such fraction does not exist, output "-1" (without the quotes).