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[string] memo;
    int buy(M bought, M[] rest)
    {
        immutable key = rest.map!(b => b.i).join(":") ~ bought.to!string;

        if (key in memo)
            return memo[key];

        if (rest.empty)
            return ma == bought.a && mb == bought.b ? bought.c : int.max;

        auto head = rest[0];
        auto tail = rest[1..$];
        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, tail), buy(u, tail));
        memo[key] = ret;
        return ret;
    }

    immutable min_c = buy(M("", 0, 0, 0), meds);
    writeln(min_c == int.max ? -1 : min_c);
}