import std.stdio, std.string, std.array, std.algorithm, std.conv, std.typecons, std.numeric;
 
alias M = Tuple!(string, "i", int, "a", int, "b", int, "c");
 
void main()
{
    immutable nm = readln.chomp.split(" ").map!(to!int).array;
    immutable n = nm[0];
    immutable ma = nm[1];
    immutable mb = nm[2];
 
    auto meds = new M[n];
    foreach(i, ref m; meds) {
        auto abc = readln.chomp.split(" ").map!(to!int).array;
        m = M(i.to!string, abc[0], abc[1], abc[2]);
    }
 
    int[M][] memo;
    memo.length = n;
    int buy(M bought, size_t idx)
    {
        if (idx >= n)
            return ma == bought.a && mb == bought.b ? bought.c : int.max;
            
        if (bought in memo[idx])
            return memo[idx][bought];
 
        auto head = meds[idx];
        auto u = bought;
 
        immutable a = u.a + head.a;
        immutable b = u.b + head.b;
 
        immutable div = gcd(a, b);
        if (!div) {
            u.a = a;
            u.b = b;
        } else {
            u.a = a / div;
            u.b = b / div;
        }
 
        u.c += head.c;
 
        immutable ret = min(buy(bought, idx+1), buy(u, idx+1));
        memo[idx][bought] = ret;
        return ret;
    }
 
    immutable min_c = buy(M("", 0, 0, 0), 0);
    writeln(min_c == int.max ? -1 : min_c);
}