import Control.Arrow
import Data.Ratio
import qualified Data.Map as M

main = do
  [_,ma,mb] <- map read . words <$> getLine :: IO [Int]
  abcs <- map (map read . words) . lines <$> getContents :: IO [[Int]]
  print $ maybe (-1) id (mixing ma mb abcs)

mixing ma mb = M.lookup (ma % mb) . M.mapKeysWith min (uncurry (%)) . M.deleteMin . foldl (dp ma mb) (M.singleton (0,0) 0)

dp ma mb acc [a,b,c] = M.unionWith min acc $ M.mapKeysMonotonic ((+a) *** (+b)) $ M.map (+c) acc