Scanf.scanf "%d %d %d" (fun n ma mb ->
    let module M = Map.Make (struct type t = int * int let compare = compare end) in
    let map =
        let rec loop i map =
            if i = n then map else
                let map =
                    Scanf.scanf " %d %d %d" (fun a b c ->
                        M.fold (fun (ka, kb) v next ->
                            let key = (ka + a, kb + b) in
                            M.add key (min (v + c) (try M.find key next with _ -> max_int)) next
                        ) map map
                    )
                in
                loop (i + 1) map
        in
        loop 0 (M.add (0, 0) 0 M.empty)
    in
    let rec loop i mi =
        if i = 500 then (if mi = max_int then -1 else mi) else
            let key = (ma * i, mb * i) in
            let mi = min mi (try M.find key map with _ -> max_int) in
            loop (i + 1) mi
    in
    loop 1 max_int |> Printf.printf "%d\n"
)