import Data.Bool
import qualified Data.ByteString.Char8 as C
import Data.List
import qualified Data.Map.Strict as M

main = C.interact $ put . sol . get

get = fmap (unfoldr (C.readInt . C.dropWhile (==' '))) . C.lines

put = C.pack . show

sol :: [[Int]] -> Int
sol ([n,ma,mb]:abcs) = bool (minimum $ M.elems mp) (-1) (M.null mp)
  where
  mp = M.filterWithKey (\[a,b] _ -> a*mb==b*ma) . M.deleteMin $
    foldr f (M.singleton [0,0] 0) abcs
  f [a,b,c] = M.unionWith min <*>
    M.map ((+) c) . M.mapKeysMonotonic (zipWith (+) [a,b])