import std.stdio, std.string, std.array, std.algorithm, std.conv, std.typecons, std.numeric;

alias M = Tuple!(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(ref m; meds) {
        auto abc = readln.chomp.split(" ").map!(to!int).array;
        m = M(abc[0], abc[1], abc[2]);
    }

    int buy(M bought, M[] rest)
    {
        if (rest.empty) {
            immutable div = gcd(bought.a, bought.b);
            if (!div) return int.max;

            immutable a = bought.a / div;
            immutable b = bought.b / div;

            return ma == a && mb == b ? bought.c : int.max;
        }

        auto head = rest[0];
        auto tail = rest[1..$];
        auto updated = bought;

        updated.a += head.a;
        updated.b += head.b;
        updated.c += head.c;

        return min(buy(bought, tail), buy(updated, tail));
    }

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