module Main

open System

let nms = Console.ReadLine().Split(' ')
        |> Array.map int

let ma = nms.[1]
let mb = nms.[2]
let n = nms.[0]

let abcs = [| for _ in 1..n ->
                Console.ReadLine().Split(' ')
                    |> Array.map int
            |]

let z = 401
let m = 4001
let mutable xs = Array3D.init (n + 1) z z (fun _ _ _ -> m)
xs.[0, 0, 0] <- 0

for i in 0..(n - 1) do
    for a in 0..(z - 1) do
        for b in 0..(z - 1) do
            let ai = abcs.[i].[0]
            let bi = abcs.[i].[1]
            let ci = abcs.[i].[2]

            let x = xs.[i, a, b]
            if x <> m then
                if xs.[i + 1, a, b] > x then
                     xs.[i + 1, a, b] <- x

                if xs.[i + 1, a + ai, b + bi] > x + ci then
                    xs.[i + 1, a + ai, b + bi] <- x + ci

let mutable r = m
for i in 1..n do
    for a in 1..(z - 1) do
        for b in 1..(z - 1) do
            if a * mb = b * ma && r > xs.[i, a, b] then
                r <- xs.[i, a, b]

Console.WriteLine(
    if r = m then
        -1
    else
        r
)
