import Data.Array.IO
import Data.Array.Unboxed
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,0),(n*10,n*10,n)) (-1) :: IO (IOArray (Int,Int,Int) Int)

  writeArray arr (0,0,0) 0
  forM_ (zip [1..] dat) $ \(k, (a:b:c:_)) -> do
    forM_ [a..n*10] $ \i -> do
      forM_ [b..n*10] $ \j -> do
        c1 <- readArray arr (i-a,j-b,k-1)
        c2 <- readArray arr (i,j,k-1)
        if c1 == -1 && c2 >= 0 then
          writeArray arr (i,j,k) c2
          else if c1 >= 0 && c2 == -1 then
          writeArray arr (i,j,k) (c1+c)
          else if c1 >= 0 && c2 >= 0 then
          writeArray arr (i,j,k) (min (c1+c) c2)
          else
          return ()

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

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