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 rec calc a b c i =
    if a > 0 && b > 0 && ma * b = mb * a then
        c
    elif i >= n then
        -1
    else
        let ai = abcs.[i].[0]
        let bi = abcs.[i].[1]
        let ci = abcs.[i].[2]

        let r1 = calc a b c (i + 1)
        let r2 = calc (a + ai) (b + bi) (c + ci) (i + 1)

        if r1 > 0 && r2 > 0 then
            min r1 r2
        elif r1 > 0 then
            r1
        elif r2 > 0 then
            r2
        else
            -1

Console.WriteLine(calc 0 0 0 0)
