import std.algorithm;
import std.array;
import std.ascii;
import std.container;
import std.conv;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void log(A...)(A arg) { stderr.writeln(arg); }
int size(T)(in T s) { return cast(int)s.length; }

void main() {
    int N, Ma, Mb; readf("%d %d %d\n", &N, &Ma, &Mb);
    auto A = new int[N];
    auto B = new int[N];
    auto C = new int[N];
    foreach (i; 0 .. N) {
        int a, b, c; readf("%d %d %d\n", &a, &b, &c);
        A[i] = a;
        B[i] = b;
        C[i] = c;
    }
    int L = min(N, 20);
    int ans = int.max;
    int[Tuple!(int, int)] map;
    map[tuple(0, 0)] = 0;
    for (int bit = 1; bit < (1 << L); bit++) {
        int a = 0, b = 0, c = 0;
        for (int i = 0; i < L; i++) {
            if (bit & (1 << i)) {
                a += A[i];
                b += B[i];
                c += C[i];
            }
        }
        if (a * Mb == b * Ma) {
            ans = min(ans, c);
        }
        auto key = tuple(a, b);
        map[key] = min(map.get(key, int.max), c);
    }
    if (N > 20) {
        auto sa = A.reduce!"a + b";
        auto sb = B.reduce!"a + b";
        int X = N - 20;
        for (int bit = 0; bit < (1 << X); bit++) {
            int a = 0, b = 0, c = 0;
            for (int i = 0; i < X; i++) {
                if (bit & (1 << i)) {
                    a += A[i + 20];
                    b += B[i + 20];
                    c += C[i + 20];
                }
            }
            for (int k = 1; k * Ma <= sa && k * Mb <= sb; k++) {
                int ma = k * Ma,
                    mb = k * Mb;
                auto rkey = tuple(ma - a, mb - b);
                if (rkey in map) {
                    ans = min(ans, map[rkey] + c);
                }
            }
        }
    }
    if (ans == int.max) {
        ans = -1;
    }
    writeln(ans);
}
