#include <iostream>
#include <sstream>
#include <algorithm>
#include <functional>
#include <iterator>
#include <bitset>
#include <string>
#include <list>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>

typedef   signed long long int slli;
typedef unsigned long long int ulli;
const slli MAX_SLLI = 0x7FFFFFFFFFFFFFFF;
const slli MIN_SLLI = 0x8000000000000000;
const ulli MAX_ULLI = 0xFFFFFFFFFFFFFFFF;
const ulli MIN_ULLI = 0x0000000000000000;

#define N_TIMES(i, n)     for (ulli i = 0;     i <  n; ++i)
#define N_TIMES_REV(i, n) for (slli i = n - 1; i >= 0; --i)

#ifdef __DEBUG__
template<typename T>
std::ostream& operator<<(std::ostream &os, const std::list<T> &list)
{
  const std::string delim = ", ";
  typename std::list<T>::const_iterator itr = list.begin();
  while (itr != list.end()) {
    os << *itr;
    ++itr;
    if(itr != list.end()) os << delim;
  }
  return os;
}

template<typename T>
std::ostream& operator<<(std::ostream &os, const std::vector<T> &v)
{
  const std::string delim = ", ";
  for (unsigned n = 0; n < v.size(); ++n) {
    os << v[n];
    if((n + 1) < v.size()) os << delim;
  }
  return os;
}
#endif

struct Material
{
  unsigned a, b, c;
  Material(unsigned a = 0, unsigned b = 0, unsigned c = 0)
    : a(a), b(b), c(c) {}
  /*bool operator==(const Material &m) const {
    return a == m.a && b == m.b && c == m.c;
  }*/
  bool operator==(const Material &m) const {
    return a == m.a && b == m.b;
  }
  /*bool operator<(const Material &m) const {
    return (c == m.c) ? (
      (b == m.b) ? a < m.a : b < m.b
    ) : c < m.c;
  }*/
  bool operator<(const Material &m) const {
    return (b == m.b) ? a < m.a : b < m.b;
  }
  
  Material operator+(const Material &m) const {
    return Material(a + m.a, b + m.b, c + m.c);
  }
};

std::istream& operator>>(std::istream &is, Material &m)
{
  is >> m.a >> m.b >> m.c;
  return is;
}

#ifdef __DEBUG__
std::ostream& operator<<(std::ostream &os, const Material &m)
{
  os << '(' << m.a << ',' << m.b << ',' << m.c << ')';
  return os;
}
#endif

class MaterialSet : public std::set<Material>
{
public:
  void update(const Material &m)
  {
    auto itr = find(m);
    if (itr != end()) {
      if (m.c < itr->c) {
        erase(itr);
        insert(m);
      }
    } else {
      insert(m);
    }
  }
};

inline bool satisfy(unsigned Ma, unsigned Mb, unsigned a, unsigned b)
{
  if(a == 0 || b == 0) return false;

  unsigned Ta = a / Ma, Sa = a % Ma;
  unsigned Tb = b / Mb, Sb = b % Mb;
  return (Ta == Tb) && (Sa == 0) && (Sb == 0);
}

inline bool satisfy(unsigned Ma, unsigned Mb, const Material& m)
{
  return satisfy(Ma, Mb, m.a, m.b);
}

long search(
  unsigned N, unsigned Ma, unsigned Mb,
  const std::vector<Material> &m
)
{
  std::vector<MaterialSet> dp(N + 1);
  dp[0].emplace(0, 0, 0);

  N_TIMES(n, N) {
    for (Material p : dp[n]) {
      dp[n + 1].update(p);
      dp[n + 1].update(p + m[n]);
    }
  }

  long ret = -1;

  for (Material p : dp[N]) {
    if (!satisfy(Ma, Mb, p)) continue;

    if (ret > 0) {
      ret = std::min(static_cast<long>(p.c), ret);
    } else {
      ret = static_cast<long>(p.c);
    }
  }

  return ret;
}

int main()
{
  unsigned N, Ma, Mb;
  std::cin >> N >> Ma >> Mb;

  std::vector<Material> m(N);
  N_TIMES(n, N) std::cin >> m[n];

  std::cout << search(N, Ma, Mb, m) << std::endl;

  return 0;
}

Measured memory_bytes: 19200