import Data.Maybe
import qualified Data.ByteString.Char8 as B
main=B.interact$B.pack.f.map(fst.fromJust.B.readInt).B.words
f(_:as)=g 2 2.reverse$as
g x y(a:as)
  |z>y="-1"
  |otherwise=g z(y`div`a*a+a-1)as
  where z=(x+a-1)`div`a*a
g x y _=unwords.map show$[x,y]