import Control.Applicative
import Control.Monad
import qualified Data.ByteString.Char8 as B
import Data.Maybe (fromJust)
import qualified Data.Map as M
import Data.Ratio
import Text.Printf
import Debug.Trace

getInts :: IO [Int]
getInts = map (fst . fromJust . B.readInt) . B.words <$> B.getLine

calc xs = M.mapKeysWith min (\(n,d) -> n % (max 1 d)) $ foldl upd zero xs
  where
    zero = M.singleton (0,0) 0
    upd s [a,b,c] = M.unionWith min
                    s
                    (M.map (+c) $ M.mapKeysWith min (\(n,d) -> (n+a,d+b)) s)

main = do
  [n,ma,mb] <- getInts
  xs <- replicateM n getInts
  let s = calc xs
  print $ M.findWithDefault (-1) (ma%mb) s
