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

  def prepare(x: List[(Int, Int, Int)]): Map[(Int, Int), Int] = {
    var m = Map[(Int, Int), Int]()
    for ((a, b, c) <- x) {
      val d = (a / ma).min(b / mb)
      val ar = a - (ma * d)
      val br = b - (mb * d)
      if (m.isDefinedAt((ar, br))) {
        m += (ar, br) -> m((ar, br)).min(c)
      } else {
        m += (ar, br) -> c
      }
    }
    m
  }

  val m1 = prepare(x1)
  val m2 = prepare(x2)

  var ans = Int.MaxValue
  if (m1.isDefinedAt((0, 0))) {
    ans = ans.min(m1(0, 0))
  }
  if (m2.isDefinedAt((0, 0))) {
    ans = ans.min(m2(0, 0))
  }
  for (((a1, b1), c1) <- m1 if a1 != 0 || b1 != 0) {
    val d = ((a1 + ma - 1)/ ma).max((b1 + mb - 1)/ mb)
    val a2 = (ma * d) - a1
    val b2 = (mb * d) - b1
    if (m2.isDefinedAt((a2, b2))) {
      ans = ans.min(c1 + m2((a2, b2)))
    }
  }

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