using System;
using System.Collections.Generic;
using static Assistant.Input;
using static Assistant.Debug;
using System.Linq;
using Assistant;

namespace ABC054D
{
    class Program
    {
        static void Main(string[] args)
        {
            int N = RInt;
            int Ma = RInt;
            int Mb = RInt;

            List<int> canda = new List<int>();
            List<int> candb = new List<int>();
            int sa = Ma;
            int sb = Mb;
            while (sa <= 200 && sb <= 200)
            {
                canda.Add(sa);
                candb.Add(sb);
                sa += Ma;
                sb += Mb;
            }

            Med[] meds = new Med[N];
            for (int i = 0; i < N; i++)
            {
                int a = RInt;
                int b = RInt;
                int c = RInt;
                meds[i] = new Med { a = a, b = b, c = c };
            }

            // 半分全列挙
            int count = N / 2;
            if (N > 30) count = N / 2;

            var first = meds.Take(count).ToArray();
            var last = meds.Skip(count).ToArray();

            int[,] min = new int[201, 201];
            for (int i = 0; i < 201; i++)
            {
                for (int j = 0; j < 201; j++)
                {
                    min[i, j] = int.MaxValue / 2;
                }
            }

            int n = first.Length;
            for (int bit = 0; bit < (1 << n); ++bit)
            {
                int suma = 0;
                int sumb = 0;
                int sumc = 0;
                for (int i = 0; i < n; i++)
                {
                    if ((bit & (1 << i)) != 0)
                    {
                        suma += first[i].a;
                        sumb += first[i].b;
                        sumc += first[i].c;
                    }
                }
                min[suma, sumb] = Math.Min(min[suma, sumb], sumc);
            }

            int ans = int.MaxValue / 2;
            n = last.Length;
            for (int bit = 0; bit < (1 << n); ++bit)
            {
                int suma = 0;
                int sumb = 0;
                int sumc = 0;
                for (int i = 0; i < n; i++)
                {
                    if ((bit & (1 << i)) != 0)
                    {
                        suma += last[i].a;
                        sumb += last[i].b;
                        sumc += last[i].c;
                    }
                }

                for (int i = 0; i < canda.Count; i++)
                {
                    int rema = canda[i] - suma;
                    int remb = candb[i] - sumb;
                    if (rema < 0 || remb < 0) continue;
                    if (min[rema, remb] == int.MaxValue / 2) continue;
                    int cand = sumc + min[rema, remb];
                    ans = Math.Min(ans, cand);
                }

            }

            if (ans == int.MaxValue / 2) ans = -1;
            Console.WriteLine(ans);

        }

        public static int[][] BitSearch(int n)
        {
            int[][] ret = new int[1 << n][];
            for (int bit = 0; bit < (1 << n); ++bit)
            {
                List<int> S = new List<int>();
                for (int i = 0; i < n; i++)
                {
                    if ((bit & (1 << i)) != 0)
                    {
                        S.Add(i);
                    }
                }

                ret[bit] = S.ToArray();
            }
            return ret;
        }

        struct Med
        {
            public int a;
            public int b;
            public int c;
        }
    }


}

namespace Assistant
{
    static class Input
    {
        static List<string> line = new List<string>();
        static int index = 0;
        static String RNext()
        {
            if (line.Count <= index) line.AddRange(Console.ReadLine().Split());
            return line[index++];
        }
        public static int RInt => int.Parse(RNext());
        public static long RLong => long.Parse(RNext());
        public static int[] RInts => Console.ReadLine().Split().Select(int.Parse).ToArray();
        public static long[] RLongs => Console.ReadLine().Split().Select(long.Parse).ToArray();
        public static string RString => RNext();
        //以下未テスト
        public static int[] RIntsC(int c) => Enumerable.Repeat(0, c).Select(x => int.Parse(RNext())).ToArray();
        public static long[] RLongsC(int c) => Enumerable.Repeat(0, c).Select(x => long.Parse(RNext())).ToArray();
        public static char[][] RMap(int h) => Enumerable.Repeat(0, h).Select(x => Console.ReadLine().ToCharArray()).ToArray();
    }

    public struct Mlong
    {
        long _v;
        const long mod = 1000000007;
        public Mlong(long n = 0) : this() { _v = n >= mod || n < 0 ? (n % mod + mod) % mod : n; }

        public static implicit operator Mlong(long _x) => new Mlong(_x);
        public static implicit operator long(Mlong _x) => _x._v;
        public static Mlong operator +(Mlong m1, Mlong m2) { long m = m1._v + m2._v; return m >= mod ? m - mod : m; }
        public static Mlong operator -(Mlong m1, Mlong m2) { long m = m1._v - m2._v; return m >= 0 ? m : m + mod; }
        public static Mlong operator *(Mlong m1, Mlong m2) => m1._v * m2._v % mod;
        public static Mlong operator /(Mlong m1, Mlong m2) => m1._v * ModPow(m2._v, mod - 2) % mod;

        public static long ModPow(long a, long n)
        {
            if (n == 0) return 1;
            else if (n % 2 == 1) return a * ModPow(a, n - 1) % mod;
            else return ModPow(a * a % mod, n / 2);
        }

        static Mlong[] fac, finv, inv;
        public static void nCkInit(int max)
        {
            fac = new Mlong[max]; finv = new Mlong[max]; inv = new Mlong[max];
            fac[0] = fac[1] = 1;
            finv[0] = finv[1] = 1;
            inv[1] = 1;
            for (int i = 2; i < max; i++)
            {
                fac[i] = fac[i - 1] * i;
                inv[i] = mod - inv[mod % i] * (mod / i);
                finv[i] = finv[i - 1] * inv[i];
            }
        }

        public static Mlong nCk(int n, int k)
        {
            if (n < k) return 0;
            if (n < 0 || k < 0) return 0;
            return fac[n] * finv[k] * finv[n - k];
        }
    }

    static class Debug
    {
        public static void Draw<T>(T[] array, int mode = 0)
        {
#if DEBUG
            Console.WriteLine(string.Join(" ", array));
#endif
        }
        static public void Draw<T>(T[,] map, int mode = 1)
        {
#if DEBUG
            int W = map.GetLength(0);
            int H = map.GetLength(1);

            string[,] map2 = new string[W + 1, H + 1];
            for (int i = 0; i < W + 1; i++)
            {
                for (int j = 0; j < H + 1; j++)
                {
                    if (i == 0 && j == 0) map2[i, j] = 0.ToString();
                    else if (i == 0) map2[i, j] = (j - 1).ToString();
                    else if (j == 0) map2[i, j] = (i - 1).ToString();
                    else map2[i, j] = map[i - 1, j - 1].ToString();
                }
            }

            for (int i = 0; i < W + 1; i++)
            {
                for (int j = 0; j < H + 1; j++)
                {
                    if (mode == 0) Console.Write(map2[i, j].Last());
                    if (mode == 1) Console.Write(map2[i, j] + " ");
                }
                Console.WriteLine();
            }
            Console.WriteLine();
#endif
        }

    }
}


Measured memory_bytes: 28444