object Main extends App {
  val sc = new java.util.Scanner(System.in)
  val n, ma, mb = sc.nextInt()
  val x = Array.fill(n)((sc.nextInt(), sc.nextInt(), sc.nextInt()))
  val (x1, x2) = x.splitAt(n/2)
  val h = 10 * ((n+1)/2)

  def compose(x1: Int, x2: Int): Int = {
    x1 * (h+1) + x2
  }

  def prepare(x: Array[(Int, Int, Int)]): Array[Int] = {
    val l = x.length
    val res = Array.fill((h+1) * (h+1))(Int.MaxValue)
    for (i <- 1 until 1 << l) {
      val (as, bs, cs) = x.zipWithIndex.filter(e => ((i >> e._2) & 1) == 1).map(_._1).unzip3
      val aSum = as.sum
      val bSum = bs.sum
      val cSum = cs.sum
      val d = (aSum / ma).min(bSum / mb)
      val ar = aSum - (ma * d)
      val br = bSum - (mb * d)
      res(compose(ar, br)) = res(compose(ar, br)).min(cSum)
    }
    res
  }

  val r1 = prepare(x1)
  val r2 = prepare(x2)

  var ans = Int.MaxValue
  if (r1(0) != Int.MaxValue) {
    ans = ans.min(r1(0))
  }
  if (r2(0) != Int.MaxValue) {
    ans = ans.min(r2(0))
  }
  for {
    a1 <- 0 to h
    b1 <- 0 to h
    if (a1 != 0 || b1 != 0) && r1(compose(a1, b1)) != Int.MaxValue
  } {
    val d = ((a1 + ma - 1) / ma).max((b1 + mb - 1) / mb)
    val a2 = (ma * d) - a1
    val b2 = (mb * d) - b1
    if (a2 <= h && b2 <= h && r2(compose(a2, b2)) != Int.MaxValue) {
      ans = ans.min(r1(compose(a1, b1)) + r2(compose(a2, b2)))
    }
  }

  println(if (ans == Int.MaxValue) -1 else ans)
}
