N, A, B = list(map(int, input().split()))

X = [1<<20] * 8080

for _ in range(N):

    a, b, c = list(map(int, input().split()))

    t = A*b-B*a

    X = [min(X[i], (0 if (i-t)%8080 == 0 else X[(i-t)%8080]) + c) for i in range(8080)]

print((-1 if X[0] > 1<<18 else X[0]))
N, A, B = list(map(int, input().split()))

X = [1<<20] * 4040

for _ in range(N):

    a, b, c = list(map(int, input().split()))

    t = A*b-B*a

    X = [min(X[i], (0 if (i-t)%4040 == 0 else X[(i-t)%4040]) + c) for i in range(4040)]

print((-1 if X[0] > 1<<18 else X[0]))
N, A, B = list(map(int, input().split()))
X = [1 << 20] * 8080
for _ in range(N):
    a, b, c = list(map(int, input().split()))
    t = A * b - B * a
    X = [
        min(X[i], (0 if (i - t) % 8080 == 0 else X[(i - t) % 8080]) + c)
        for i in range(8080)
    ]
print((-1 if X[0] > 1 << 18 else X[0]))

N, A, B = list(map(int, input().split()))
X = [1 << 20] * 4040
for _ in range(N):
    a, b, c = list(map(int, input().split()))
    t = A * b - B * a
    X = [
        min(X[i], (0 if (i - t) % 4040 == 0 else X[(i - t) % 4040]) + c)
        for i in range(4040)
    ]
print((-1 if X[0] > 1 << 18 else X[0]))

-X = [1 << 20] * 8080 +X = [1 << 20] * 4040 -        min(X[i], (0 if (i - t) % 8080 == 0 else X[(i - t) % 8080]) + c) -        for i in range(8080) +        min(X[i], (0 if (i - t) % 4040 == 0 else X[(i - t) % 4040]) + c) +        for i in range(4040)