import sys

readlines = sys.stdin.buffer.readlines

input = sys.stdin.buffer.readline

N, Ma, Mb = list(map(int, input().split()))

X = [tuple(map(int, line.split())) for line in readlines()]



INF = 1 << 30

x = [[INF]*201 for _ in [0]*201]

y = [[INF]*201 for _ in [0]*201]



n = N//2

X, Y = X[:n], X[n:]

for bit in range(1 << n):

    totA = totB = cost = 0

    for i in range(n):

        if bit >> i & 1:

            a, b, c = X[i]

            totA += a

            totB += b

            cost += c



    x[totA][totB] = min(x[totA][totB], cost)



for bit in range(1 << (N-n)):

    totA = totB = cost = 0

    for i in range(N-n):

        if bit >> i & 1:

            a, b, c = Y[i]

            totA += a

            totB += b

            cost += c



    y[totA][totB] = min(y[totA][totB], cost)



ans = INF

M = 405//max(Ma, Mb)

for i, t in enumerate(x):

    for j, s in enumerate(t):

        if s == INF:

            continue

        for k in range(1, M+1):

            I = k*Ma-i

            J = k*Mb-j

            if 0 <= I <= 200 and 0 <= J <= 200:

                ans = min(ans, s + y[I][J])



print((ans if ans < INF else -1))

import sys

readlines = sys.stdin.buffer.readlines

input = sys.stdin.buffer.readline

N, Ma, Mb = list(map(int, input().split()))

X = [list(map(int, line.split())) for line in readlines()]

X = [(a*Mb-b*Ma, c) for a, b, c in X]



INF = 1 << 30

MAX = 10000

MID = MAX//2

dp = [INF]*(MAX+1)

ans = INF

for i, (x, c) in enumerate(X):

    if x == 0:

        ans = min(ans, c)

        continue



    if x > 0:

        for j in range(MAX, x-1, -1):

            dp[j] = min(dp[j], dp[j-x]+c)

    else:

        for j in range(MAX+x):

            dp[j] = min(dp[j], dp[j-x]+c)



    dp[MID+x] = min(dp[MID+x], c)



ans = min(ans, dp[MID])

print((ans if ans < INF else -1))

import sys

readlines = sys.stdin.buffer.readlines
input = sys.stdin.buffer.readline
N, Ma, Mb = list(map(int, input().split()))
X = [tuple(map(int, line.split())) for line in readlines()]
INF = 1 << 30
x = [[INF] * 201 for _ in [0] * 201]
y = [[INF] * 201 for _ in [0] * 201]
n = N // 2
X, Y = X[:n], X[n:]
for bit in range(1 << n):
    totA = totB = cost = 0
    for i in range(n):
        if bit >> i & 1:
            a, b, c = X[i]
            totA += a
            totB += b
            cost += c
    x[totA][totB] = min(x[totA][totB], cost)
for bit in range(1 << (N - n)):
    totA = totB = cost = 0
    for i in range(N - n):
        if bit >> i & 1:
            a, b, c = Y[i]
            totA += a
            totB += b
            cost += c
    y[totA][totB] = min(y[totA][totB], cost)
ans = INF
M = 405 // max(Ma, Mb)
for i, t in enumerate(x):
    for j, s in enumerate(t):
        if s == INF:
            continue
        for k in range(1, M + 1):
            I = k * Ma - i
            J = k * Mb - j
            if 0 <= I <= 200 and 0 <= J <= 200:
                ans = min(ans, s + y[I][J])
print((ans if ans < INF else -1))

import sys

readlines = sys.stdin.buffer.readlines
input = sys.stdin.buffer.readline
N, Ma, Mb = list(map(int, input().split()))
X = [list(map(int, line.split())) for line in readlines()]
X = [(a * Mb - b * Ma, c) for a, b, c in X]
INF = 1 << 30
MAX = 10000
MID = MAX // 2
dp = [INF] * (MAX + 1)
ans = INF
for i, (x, c) in enumerate(X):
    if x == 0:
        ans = min(ans, c)
        continue
    if x > 0:
        for j in range(MAX, x - 1, -1):
            dp[j] = min(dp[j], dp[j - x] + c)
    else:
        for j in range(MAX + x):
            dp[j] = min(dp[j], dp[j - x] + c)
    dp[MID + x] = min(dp[MID + x], c)
ans = min(ans, dp[MID])
print((ans if ans < INF else -1))

-X = [tuple(map(int, line.split())) for line in readlines()] +X = [list(map(int, line.split())) for line in readlines()] +X = [(a * Mb - b * Ma, c) for a, b, c in X] -x = [[INF] * 201 for _ in [0] * 201] -y = [[INF] * 201 for _ in [0] * 201] -n = N // 2 -X, Y = X[:n], X[n:] -for bit in range(1 << n): -    totA = totB = cost = 0 -    for i in range(n): -        if bit >> i & 1: -            a, b, c = X[i] -            totA += a -            totB += b -            cost += c -    x[totA][totB] = min(x[totA][totB], cost) -for bit in range(1 << (N - n)): -    totA = totB = cost = 0 -    for i in range(N - n): -        if bit >> i & 1: -            a, b, c = Y[i] -            totA += a -            totB += b -            cost += c -    y[totA][totB] = min(y[totA][totB], cost) +MAX = 10000 +MID = MAX // 2 +dp = [INF] * (MAX + 1) -M = 405 // max(Ma, Mb) -for i, t in enumerate(x): -    for j, s in enumerate(t): -        if s == INF: -            continue -        for k in range(1, M + 1): -            I = k * Ma - i -            J = k * Mb - j -            if 0 <= I <= 200 and 0 <= J <= 200: -                ans = min(ans, s + y[I][J]) +for i, (x, c) in enumerate(X): +    if x == 0: +        ans = min(ans, c) +        continue +    if x > 0: +        for j in range(MAX, x - 1, -1): +            dp[j] = min(dp[j], dp[j - x] + c) +    else: +        for j in range(MAX + x): +            dp[j] = min(dp[j], dp[j - x] + c) +    dp[MID + x] = min(dp[MID + x], c) +ans = min(ans, dp[MID])