{-# OPTIONS_GHC -O #-}

import Control.Monad
import Control.Monad.ST
import Data.List
import Control.Monad.Trans.State.Strict
import Data.Char (isSpace)
import Data.Maybe (fromJust,fromMaybe)
import qualified Data.ByteString.Lazy.Char8 as B

import Data.Array.ST
import Data.Array.Unboxed
type Dist s = STUArray s (Int,Int) Int

type Parser a = StateT B.ByteString Maybe a

parseInt :: Parser Int
parseInt = StateT (B.readInt . B.dropWhile isSpace)

parseInt3 :: Parser (Int,Int,Int)
parseInt3 = liftM3 (,,) parseInt parseInt parseInt

parse :: Parser ((Int,Int,Int), [(Int,Int,Int)])
parse = do
    (n,ma,mb)<-parseInt3
    abcs<-replicateM n parseInt3
    return ((n,ma,mb),abcs)

main=do
    ((n,ma,mb),abcs)<-fromJust.evalStateT parse <$> B.getContents
    let dp = fdp n abcs :: UArray (Int,Int,Int) Int
    print $ solve n ma mb dp
--
fdp n abcs = runSTUArray $ do
    dp <- newArray ((0,0,0),(n,10*n,10*n)) 5000 :: ST s (STUArray s (Int,Int,Int) Int)
    writeArray dp (0,0,0) 0
    forM_ [(ii,u,v)|ii<-zip abcs [1..n],u<-[0..10*n],v<-[0..10*n]] $ \(((a,b,c),i),u,v) ->
        if u-a>=0 && v-b>=0 then
            writeArray dp (i,u,v) =<< min <$> ((+c) <$> readArray dp (i-1,u-a,v-b)) <*> readArray dp (i-1,u,v)
        else
            writeArray dp (i,u,v) =<< readArray dp (i-1,u,v)
    return dp
--
solve n ma mb dp
    | ar < 5000 = ar
    | otherwise = -1
    where
        ca = takeWhile (\c->c*ma<=10*n && c*mb<=10*n) [1..]
        ar = minimum $ map (\c -> dp ! (n,c*ma,c*mb)) ca
