let input = readLine()!.split(separator: " ").map{ Int($0)! }
let N = input[0]
let Ma = input[1]
let Mb = input[2]

var dp = Array(repeating: Array(repeating: Optional<Int>.none, count: 40 * 10 + 1), count: 40 * 10 + 1)
dp[0][0] = 0

var sumOfA = 0
var sumOfB = 0
for _ in stride(from: 0, to: N, by: 1) {
    let chemical = readLine()!.split(separator: " ").map{ Int($0)! }
    let a = chemical[0]
    let b = chemical[1]
    let c = chemical[2]

    for j in stride(from: sumOfA + a, through: a, by: -1) {
        for k in stride(from: sumOfB + b, through: b, by: -1) {
            guard let previousValue = dp[j - a][k - b] else { continue }

            dp[j][k] = min(dp[j][k] ?? Int.max, previousValue + c)
        }
    }

    sumOfA += a
    sumOfB += b
}

var result: Int? = nil
for i in stride(from: Ma, through: sumOfA, by: Ma) {
    let j = i / Ma * Mb
    if let cost = dp[i][j] {
        result = min(result ?? Int.max, cost)
    }
}

print(result ?? -1)
