#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; ++i)
#define ALL(a) (a).begin(), (a).end()
#define rALL(a) (a).rbegin(), (a).rend()
#define SZ(x) ((int)(x).size())
using ll = long long;
using ull = unsigned long long;
using P = pair<int, int>;
using llP = pair<ll, ll>;
using DoP = pair<double, double>;
const int di[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int dj[] = {1, 0, -1, 0, 1, -1, 1, -1};
const int INF = 1 << 30;
const ll INF64 = 1ll << 55;
const int mod = 1000000007;
//const int mod = 998244353;

template <class T>inline bool chmin(T &a, const T b)
{
  if (a > b)
  {
    a = b;
    return true;
  }
  return false;
}
template <class T>inline bool chmax(T &a, const T b)
{
  if (a < b)
  {
    a = b;
    return true;
  }
  return false;
}
template <class T>inline void line_out(const vector<T> vec) {
  int n = SZ(vec);
  rep(i, n) {
    cout << vec[i];
    if(i < n-1) cout << " ";
  }
  cout << endl;
  return;
}

struct mint
{
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a)
  {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a)
  {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a)
  {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const
  {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const
  {
    mint res(*this);
    return res -= a;
  }
  mint &operator++()
  {
    if ((x += 1) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator--()
  {
    if ((x += mod - 1) >= mod)
      x -= mod;
    return *this;
  }
  mint operator*(const mint a) const
  {
    mint res(*this);
    return res *= a;
  }
  mint pow(ll t) const
  {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const
  {
    return pow(mod - 2);
  }
  mint &operator/=(const mint a)
  {
    return (*this) *= a.inv();
  }
  mint operator/(const mint a) const
  {
    mint res(*this);
    return res /= a;
  }
};



int main()
{
  int n, ma, mb;
  cin >> n >> ma >> mb;
  int n1, n2;
  n1 = n/2;
  n2 = n-n1;
  int max_a = 0, max_b = 0;
  vector<int> a1(n1), b1(n1), c1(n1);
  vector<int> a2(n2), b2(n2), c2(n2);
  rep(i, n1) cin >> a1[i] >> b1[i] >> c1[i];
  rep(i, n2) cin >> a2[i] >> b2[i] >> c2[i];

  vector<tuple<int, int, int>> X, Y;
  int ans = INF;
  for(int bit = 1; bit < 1<<n1; ++bit) {
    int a = 0, b = 0, c = 0;
    rep(i, n1) if(bit & 1<<i) {
      a+=a1[i];
      b+=b1[i];
      c+=c1[i];
    }
    if(a*mb==b*ma) chmin(ans, c);
    X.emplace_back(a, b, c);
  }
  for(int bit = 1; bit < 1<<n2; ++bit) {
    int a = 0, b = 0, c = 0;
    rep(i, n2) if(bit & 1<<i) {
      a+=a2[i];
      b+=b2[i];
      c+=c2[i];
    }
    if(a*mb==b*ma) chmin(ans, c);
    Y.emplace_back(a, b, c);
  }


  sort(ALL(X));
  sort(ALL(Y));

  for(auto x : X) {
    int a = get<0>(x), b = get<1>(x), c = get<2>(x);
    if(a*mb==b*ma) continue;

    int k = 1;
    while(ma*k < a || mb*k < b) ++k;
    for(k;;++k) {
      if(ma*k > 400 || mb*k > 400) break;
      auto it = lower_bound(ALL(Y), make_tuple(ma*k-a, mb*k-b, 0));
      if(it == Y.end()) break;
      int na = get<0>(*it), nb = get<1>(*it), nc = get<2>(*it);
      if(a+na == ma*k && b+nb == mb*k) {
        chmin(ans, c+nc);
        break;
      }
    }
  }
  if(ans != INF) cout << ans << endl;
  else cout << -1 << endl;
}

Measured memory_bytes: 33856