import Data.Array.IO
import Control.Monad

main = do
  (n:ma:mb:_) <- getLine >>= return . map read . words :: IO [Int]
  dat <- getContents >>= return . map (map read . words) . lines :: IO [[Int]]

  arr <- newArray ((0,0),(n*10,n*10)) (-1) :: IO (IOArray (Int,Int) Int)

  writeArray arr (0,0) 0
  forM_ dat $ \(a:b:c:_) -> do
    forM_ [0..n*10-a] $ \i -> do
      forM_ [0..n*10-b] $ \j -> do
        c1 <- readArray arr (i,j)
        c2 <- readArray arr (i+a,j+b)
        when (c1 >= 0 && (c2 == -1 || c1+c < c2)) $ writeArray arr (i+a,j+b) (c1+c)

  res <- forM [1..n] (\r -> readArray arr (ma*r, mb*r)) >>= return . filter (>=0)

  print $ if res == [] then -1 else minimum res
    
