Dolphin is planning to generate a small amount of a certain chemical substance C.

In order to generate the substance C, he must prepare a solution which is a mixture of two substances A and B in the ratio of M_a:M_b.

He does not have any stock of chemicals, however, so he will purchase some chemicals at a local pharmacy.

The pharmacy sells N kinds of chemicals. For each kind of chemical, there is exactly one package of that chemical in stock.

The package of chemical i contains a_i grams of the substance A and b_i grams of the substance B, and is sold for c_i yen (the currency of Japan).

Dolphin will purchase some of these packages. For some reason, he must use all contents of the purchased packages to generate the substance C.

Find the minimum amount of money required to generate the substance C.

If it is not possible to generate the substance C by purchasing any combination of packages at the pharmacy, report that fact.  

-----Constraints-----
 - 1≦N≦40 
 - 1≦a_i,b_i≦10 
 - 1≦c_i≦100 
 - 1≦M_a,M_b≦10 
 - gcd(M_a,M_b)=1
 - a_i, b_i, c_i, M_a and M_b are integers.

-----Input-----
The input is given from Standard Input in the following format:
N M_a M_b  
a_1 b_1 c_1  
a_2 b_2 c_2
:  
a_N b_N c_N  

-----Output-----
Print the minimum amount of money required to generate the substance C. If it is not possible to generate the substance C, print -1 instead.

-----Sample Input-----
3 1 1
1 2 1
2 1 2
3 3 10

-----Sample Output-----
3

The amount of money spent will be minimized by purchasing the packages of chemicals 1 and 2.

In this case, the mixture of the purchased chemicals will contain 3 grams of the substance A and 3 grams of the substance B, which are in the desired ratio: 3:3=1:1.

The total price of these packages is 3 yen.

Difficulty: MEDIUM

Solutions:
(N, Ma, Mb) = map(int, input().split(' '))
med = []
for _ in range(N):
	med.append(tuple(map(int, input().split(' '))))
inf = 10 ** 9 + 7
dp = [[inf for _ in range(401)] for _ in range(401)]
dp[0][0] = 0
for k in range(N):
	(a, b, c) = med[k]
	for i in range(400, -1, -1):
		for j in range(400, -1, -1):
			if dp[i][j] != inf:
				dp[i + a][j + b] = min(dp[i + a][j + b], dp[i][j] + c)
ans = inf
for i in range(401):
	for j in range(401):
		if i == 0 or j == 0:
			continue
		if i * Mb == j * Ma:
			ans = min(ans, dp[i][j])
print(ans if ans != inf else -1)


import sys
sys.setrecursionlimit(10 ** 8)
(N, Ma, Mb) = map(int, input().split())
A = [[int(i) for i in input().split()] for i in range(N)]
memo = {i: {} for i in range(N)}

def dfs(i, a, b):
	if b > 0 and a / b == Ma / Mb:
		return 0
	elif i == N:
		return float('inf')
	elif (a, b) in memo[i]:
		return memo[i][a, b]
	else:
		ans = min(dfs(i + 1, a, b), dfs(i + 1, a + A[i][0], b + A[i][1]) + A[i][2])
	memo[i][a, b] = ans
	return ans
if dfs(0, 0, 0) == float('inf'):
	print(-1)
else:
	print(dfs(0, 0, 0))


(n, ma, mb) = map(int, input().split())
r = [0] * n
c = [0] * n
for i in range(n):
	(a, b, c[i]) = map(int, input().split())
	r[i] = a * mb - b * ma
dp = [[10 ** 9] * 4001 for i in range(n + 1)]
for i in range(n):
	for j in range(4001):
		dp[i + 1][j] = min(dp[i + 1][j], dp[i][j])
		if j == 2000:
			dp[i + 1][j + r[i]] = min(dp[i + 1][j + r[i]], c[i])
		if 0 <= j + r[i] <= 4000:
			dp[i + 1][j + r[i]] = min(dp[i + 1][j + r[i]], dp[i][j] + c[i])
if dp[n][2000] < 10 ** 9:
	print(dp[n][2000])
else:
	print(-1)


(N, Ma, Mb) = map(int, input().split())
kusuri = []
cusum = [0] * (N + 1)
for i in range(N):
	(a, b, c) = map(int, input().split())
	sa = a * Mb - b * Ma
	kusuri.append((sa, c))
	cusum[i + 1] = cusum[i] + c
call = cusum[-1]
dp = [[call + 1] * 8001 for _ in range(41)]
dp[0][4000] = 0
cmin = call + 1
for (i, j) in enumerate(kusuri):
	d = j[0]
	c = j[1]
	for k in range(4000 - 100 * i, 4000 + 100 * i + 1):
		dp[i + 1][k] = min(dp[i + 1][k], dp[i][k])
		if k + d == 4000:
			cmin = min(cmin, dp[i][k] + c)
		dp[i + 1][k + d] = min(dp[i + 1][k + d], dp[i][k] + c)
if cmin == call + 1:
	print(-1)
else:
	print(cmin)


(N, ma, mb) = list(map(int, input().split()))
a = [0] * N
b = [0] * N
c = [0] * N
NMAX = 40
ABMAX = 10
INF = 1000000
for i in range(N):
	(a[i], b[i], c[i]) = list(map(int, input().split()))
dp = [[[INF] * (NMAX * ABMAX + 1) for _ in range(NMAX * ABMAX + 1)] for _ in range(N + 1)]
dp[0][0][0] = 0
for i in range(N):
	for ca in range(NMAX * ABMAX + 1):
		for cb in range(NMAX * ABMAX + 1):
			if dp[i][ca][cb] == INF:
				continue
			dp[i + 1][ca][cb] = min(dp[i + 1][ca][cb], dp[i][ca][cb])
			dp[i + 1][ca + a[i]][cb + b[i]] = min(dp[i + 1][ca + a[i]][cb + b[i]], dp[i][ca][cb] + c[i])
ans = INF
for ca in range(1, NMAX * ABMAX + 1):
	for cb in range(1, NMAX * ABMAX + 1):
		if ca * mb == cb * ma:
			ans = min(ans, dp[N][ca][cb])
print(ans if ans != INF else -1)


(N, A, B) = map(int, input().split())
inf = float('inf')
dp = [[inf] * 401 for _ in range(401)]
dp[0][0] = 0
for i in range(N):
	(a, b, c) = map(int, input().split())
	for j in range(-1, -402, -1):
		for k in range(-1, -402, -1):
			if dp[j][k] != inf:
				dp[j + a][k + b] = min(dp[j + a][k + b], dp[j][k] + c)
ans = inf
for i in range(1, 401):
	for j in range(1, 401):
		if i / j == A / B:
			ans = min(ans, dp[i][j])
if ans == inf:
	ans = -1
print(ans)


import math
import heapq
import bisect
import numpy as np
from collections import Counter, deque
import itertools
(N, Ma, Mb) = map(int, input().split())
DP = [[10 ** 9 for _ in range(401)] for _ in range(401)]
DP[0][0] = 0
for _ in range(N):
	(a, b, c) = map(int, input().split())
	for i in range(401)[::-1]:
		for j in range(401)[::-1]:
			if DP[i][j] != 10 ** 9:
				DP[i + a][j + b] = min(DP[i + a][j + b], DP[i][j] + c)
ans = 10 ** 9
(A, B) = (Ma, Mb)
while max(A, B) <= 400:
	ans = min(ans, DP[A][B])
	A += Ma
	B += Mb
if ans == 10 ** 9:
	print(-1)
else:
	print(ans)


(n, ma, mb) = [int(i) for i in input().split()]
lst = [[int(i) for i in input().split()] for _ in range(n)]
lst.sort(key=lambda x: x[2])
dic = {}

def push(k, v):
	if k in dic:
		dic[k] = min(dic[k], v)
	else:
		dic[k] = v
for i in lst:
	(k, v) = (i[0] * mb - i[1] * ma, i[2])
	l = [[j, dic[j]] for j in dic]
	for item in l:
		push(item[0] + k, item[1] + v)
	push(k, v)
print(dic[0] if 0 in dic else '-1')


def main():
	(N, Ma, Mb) = list(map(int, input().split()))
	A = []
	B = []
	C = []
	for _ in range(N):
		(a, b, c) = list(map(int, input().split()))
		A.append(a)
		B.append(b)
		C.append(c)
	dp = [[10 ** 5] * 401 for _ in range(401)]
	dp[0][0] = 0
	for t in range(N):
		(a, b, c) = (A[t], B[t], C[t])
		for i in range(400, -1, -1):
			for j in range(400, -1, -1):
				if i - a >= 0 and j - b >= 0:
					if dp[i - a][j - b] + c < dp[i][j]:
						dp[i][j] = dp[i - a][j - b] + c
	ans = 10 ** 5
	for i in range(1, 401):
		if i * Ma > 400 or i * Mb > 400:
			break
		if dp[i * Ma][i * Mb] < ans:
			ans = dp[i * Ma][i * Mb]
	if ans == 10 ** 5:
		print(-1)
	else:
		print(ans)

def __starting_point():
	main()
__starting_point()


import copy
(n, ma, mb) = map(int, input().split())
abc = [list(map(int, input().split())) for i in range(n)]
l = 10 * n
inf = float('inf')
data = [[inf] * (l + 1) for i in range(l + 1)]
data[0][0] = 0
for u in abc:
	(a, b, c) = u
	h = copy.deepcopy(data)
	for i in range(l - a + 1):
		for j in range(l - b + 1):
			if data[i][j] != inf:
				h[i + a][j + b] = min(h[i + a][j + b], data[i][j] + c)
	data = h
k = 1
ans = inf
while ma * k <= l and mb * k <= l:
	if data[ma * k][mb * k] != inf:
		ans = min(ans, data[ma * k][mb * k])
	k += 1
if ans != inf:
	print(ans)
else:
	print(-1)


(N, Ma, Mb) = list(map(int, input().split()))
L = []
sum_a = 0
sum_b = 0
for _ in range(N):
	(a, b, c) = list(map(int, input().split()))
	sum_a += a
	sum_b += b
	L.append((a, b, c))
inf = 10 ** 9 + 7
dp = [[[inf] * (sum_b + 1) for _ in range(sum_a + 1)] for _ in range(N + 1)]
dp[0][0][0] = 0
for i in range(N):
	for a in range(sum_a + 1):
		for b in range(sum_b + 1):
			if 0 <= a - L[i][0] and 0 <= b - L[i][1]:
				dp[i + 1][a][b] = min(dp[i][a - L[i][0]][b - L[i][1]] + L[i][2], dp[i][a][b])
			else:
				dp[i + 1][a][b] = dp[i][a][b]
min_cost = inf
for a in range(sum_a + 1):
	for b in range(sum_b + 1):
		if a * Mb == b * Ma and dp[N][a][b] != 0:
			min_cost = min(dp[N][a][b], min_cost)
print(min_cost if min_cost != inf else -1)


NMAX = 40
ABMAX = 10
INF = 1000000
(N, MA, MB) = list(map(int, input().split()))
A = [0] * NMAX
B = [0] * NMAX
C = [0] * NMAX
dp = [[[INF for _ in range(NMAX * ABMAX + 1)] for _ in range(NMAX * ABMAX + 1)] for _ in range(NMAX + 1)]
for i in range(N):
	(A[i], B[i], C[i]) = list(map(int, input().split()))
dp[0][0][0] = 0
for i in range(N):
	for ca in range(NMAX * ABMAX + 1):
		for cb in range(NMAX * ABMAX + 1):
			if dp[i][ca][cb] == INF:
				continue
			dp[i + 1][ca][cb] = min(dp[i + 1][ca][cb], dp[i][ca][cb])
			dp[i + 1][ca + A[i]][cb + B[i]] = min(dp[i + 1][ca + A[i]][cb + B[i]], dp[i][ca][cb] + C[i])
ans = INF
for ca in range(1, NMAX * ABMAX + 1):
	for cb in range(1, NMAX * ABMAX + 1):
		if ca * MB == cb * MA:
			ans = min(ans, dp[N][ca][cb])
if ans == INF:
	ans = -1
print(ans)


def main():
	INF = 100 * 40 + 1
	MX = 4000
	(N, Ma, Mb) = map(int, input().split())
	dp = [[INF] * (MX * 2 + 1) for _ in range(2)]
	(i, j) = (0, 1)
	for _ in range(N):
		(ai, bi, ci) = map(int, input().split())
		x = Ma * bi - Mb * ai
		for k in range(-MX, MX + 1):
			dp[j][k] = dp[i][k]
		dp[j][x] = min(dp[j][x], ci)
		for k in range(-MX + x, MX + 1):
			dp[j][k] = min(dp[j][k], dp[i][k - x] + ci)
		(i, j) = (j, i)
	res = dp[i][0]
	print(-1 if res == INF else res)
main()


(N, Ma, Mb) = map(int, input().split())
INF = float('inf')
dp = [[INF for b in range(401)] for a in range(401)]
dp[0][0] = 0
sa = 0
sb = 0
for i in range(1, N + 1):
	(ai, bi, ci) = map(int, input().split())
	sa += ai
	sb += bi
	for a in range(sa + 1, -1, -1):
		for b in range(sb + 1, -1, -1):
			if a - ai >= 0 and b - bi >= 0:
				dp[a][b] = min(dp[a][b], dp[a - ai][b - bi] + ci)
ans = INF
for j in range(1, 401):
	if Ma * j > 400 or Mb * j > 400:
		break
	ans = min(ans, dp[Ma * j][Mb * j])
print(ans if ans != INF else -1)


(N, Ma, Mb) = map(int, input().split())
L = [[float('inf')] * 410 for i in range(410)]
P = [[0, 0]]
L[0][0] = 0
for i in range(N):
	(a, b, c) = map(int, input().split())
	OldP = P.copy()
	Lsub = [L[p[0]][p[1]] for p in OldP]
	for (xp, p) in enumerate(OldP):
		if L[p[0] + a][p[1] + b] == float('inf'):
			P.append([p[0] + a, p[1] + b])
		L[p[0] + a][p[1] + b] = min(L[p[0] + a][p[1] + b], Lsub[xp] + c)
g = [Ma, Mb]
ans = float('inf')
while g[0] < 410 and g[1] < 410:
	ans = min(ans, L[g[0]][g[1]])
	g[0] += Ma
	g[1] += Mb
print(ans if ans != float('inf') else -1)


(N, Ma, Mb) = list(map(int, input().split()))
INF = 10 ** 8
lst = [[INF] * 401 for i in range(401)]
lst[0][0] = 0
for _ in range(N):
	(a, b, c) = list(map(int, input().split()))
	for i in range(400, a - 1, -1):
		for j in range(400, b - 1, -1):
			if lst[i - a][j - b] == INF:
				pass
			else:
				lst[i][j] = min(lst[i][j], lst[i - a][j - b] + c)
ans = INF
n = 400 // max(Ma, Mb)
for i in range(1, n + 1):
	ans = min(ans, lst[Ma * i][Mb * i])
if ans == INF:
	print(-1)
else:
	print(ans)


(N, Ma, Mb) = map(int, input().split())
P = [list(map(int, input().split())) for k in range(N)]
INF = 10000
dp = [[[INF for m in range(401)] for k in range(401)] for l in range(N + 1)]
dp[0][0][0] = 0
for k in range(N):
	a = P[k][0]
	b = P[k][1]
	c = P[k][2]
	for l in range(401):
		for m in range(401):
			if dp[k][l][m] != INF:
				dp[k + 1][l][m] = min(dp[k + 1][l][m], dp[k][l][m])
				dp[k + 1][l + a][m + b] = min(dp[k + 1][l + a][m + b], dp[k][l][m] + c)
ans = INF
for l in range(1, 401):
	for m in range(1, 401):
		if Ma * m == Mb * l:
			ans = min(ans, dp[N][l][m])
print(ans if ans != INF else -1)


(N, A, B) = map(int, input().split())
X = [1 << 20] * 4040
for _ in range(N):
	(a, b, c) = 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])


import sys
stdin = sys.stdin

def na():
	return map(int, stdin.readline().split())
(n, ma, mb) = na()
abcs = []
for i in range(n):
	abcs.append(list(na()))
I = 9999999999
dp = [[I] * 420 for _ in range(420)]
dp[0][0] = 0
sa = 1
sb = 1
for abc in abcs:
	for i in range(sa - 1, -1, -1):
		for j in range(sb - 1, -1, -1):
			dp[i + abc[0]][j + abc[1]] = min(dp[i + abc[0]][j + abc[1]], dp[i][j] + abc[2])
	sa += abc[0]
	sb += abc[1]
x = ma
y = mb
ret = I
while x < 420 and y < 420:
	ret = min(ret, dp[x][y])
	x += ma
	y += mb
if ret == I:
	print(-1)
else:
	print(ret)


(N, Ma, Mb) = list(map(int, input().split()))
abc = [list(map(int, input().split())) for _ in range(N)]
an = 0
bn = 0
for (a, b, c) in abc:
	an += a
	bn += b
ma = max(an, bn) + 1
DP = [[10 ** 10] * ma for _ in range(ma)]
for i in range(N):
	(a, b, c) = abc[i]
	for j in range(ma - 1, -1, -1):
		for k in range(ma - 1, -1, -1):
			if j == 0 and k == 0:
				DP[a][b] = min(DP[a][b], c)
				continue
			if DP[j][k] == 10 ** 10:
				continue
			if j + a >= ma or k + b >= ma:
				continue
			DP[j + a][k + b] = min(DP[j + a][k + b], DP[j][k] + c)
ans = 10 ** 10
(a, b) = (Ma, Mb)
while a < ma and b < ma:
	ans = min(ans, DP[a][b])
	a += Ma
	b += Mb
if ans == 10 ** 10:
	print(-1)
else:
	print(ans)


(n, ma, mb) = map(int, input().split())
abc = []
for i in range(n):
	abc.append([int(j) for j in input().split()])
from operator import itemgetter
abc.sort(key=itemgetter(2))
dp = [[10 ** 18 for i in range(401)] for j in range(401)]
dp[0][0] = 0
(p, q) = (0, 0)
for (a, b, c) in abc:
	for j in reversed(range(q + 1)):
		for i in range(p + 1):
			if dp[i][j] == 10 ** 18:
				continue
			else:
				dp[i + a][j + b] = min(dp[i + a][j + b], dp[i][j] + c)
	p += a
	q += b
ans = 1145141919
i = 1
while ma * i < 401 and mb * i < 401:
	ans = min(ans, dp[ma * i][mb * i])
	i += 1
if ans == 1145141919:
	print(-1)
else:
	print(ans)


(N, Ma, Mb) = map(int, input().split())
dp = [{} for i in range(N + 1)]
dp[0][0, 0] = 0
for i in range(N):
	(a, b, c) = map(int, input().split())
	for (p, q) in dp[i]:
		if (p, q) not in dp[i + 1]:
			dp[i + 1][p, q] = 100000
		dp[i + 1][p, q] = min(dp[i + 1][p, q], dp[i][p, q])
		if (p + a, q + b) not in dp[i + 1]:
			dp[i + 1][p + a, q + b] = 100000
		dp[i + 1][p + a, q + b] = min(dp[i + 1][p + a, q + b], dp[i][p, q] + c)
rs = []
for k in range(1, 10000):
	if (k * Ma, k * Mb) in dp[N]:
		r = dp[N][k * Ma, k * Mb]
		rs.append(r)
if len(rs) == 0:
	print(-1)
else:
	r = min(rs)
	print(r)


(N, Ma, Mb) = map(int, input().split())
G = []
for i in range(N):
	(a, b, c) = map(int, input().split())
	G.append((a, b, c))
M = 1 + max(sum([a for (a, b, c) in G]), sum([b for (a, b, c) in G]))
INF = 1000000000
dp = [INF] * (M * M)
dp[0] = 0
for (a, b, c) in G:
	for i in reversed(range(0, M - a)):
		for j in reversed(range(0, M - b)):
			dp[(i + a) * M + (j + b)] = min(dp[(i + a) * M + (j + b)], dp[i * M + j] + c)
ans = INF
x = 1
while Ma * x < M and Mb * x < M:
	ans = min(ans, dp[Ma * x * M + Mb * x])
	x += 1
if ans >= INF:
	ans = -1
print(ans)


def main():
	(N, Ma, Mb) = map(int, input().split())
	yakuhin = []
	sa = 0
	sb = 0
	for i in range(N):
		yakuhin.append(list(map(int, input().split())))
		sa += yakuhin[i][0]
		sb += yakuhin[i][1]
	INF = 10 ** 15
	DP = [[[INF for _ in range(sb + 1)] for _ in range(sa + 1)] for _ in range(N + 1)]
	for i in range(N):
		DP[i][0][0] = 0
	for i in range(N):
		for j in range(sa + 1):
			for k in range(sb + 1):
				if j >= yakuhin[i][0] and k >= yakuhin[i][1]:
					DP[i + 1][j][k] = min(DP[i][j][k], DP[i][j - yakuhin[i][0]][k - yakuhin[i][1]] + yakuhin[i][2])
				else:
					DP[i + 1][j][k] = min(DP[i + 1][j][k], DP[i][j][k])
	ans = INF
	for i in range(1, N + 1):
		for j in range(1, sa + 1):
			for k in range(1, sb + 1):
				if j * Mb == k * Ma:
					ans = min(ans, DP[i][j][k])
	print(ans if ans != INF else -1)
main()


(N, Ma, Mb) = list(map(int, input().split()))
ABC = [tuple(map(int, input().split())) for i in range(N)]
inf = float('inf')
DP = [[inf] * 401 for i in range(401)]
DP[0][0] = 0
for (a, b, c) in ABC:
	for i in reversed(range(401)):
		for j in reversed(range(401)):
			if DP[i][j] != inf:
				DP[i + a][j + b] = min(DP[i][j] + c, DP[i + a][j + b])
ans = inf
for i in range(1, 401):
	for j in range(1, 401):
		if Ma * j == Mb * i:
			ans = min(ans, DP[i][j])
print(-1 if ans == inf else ans)


import sys
from itertools import combinations
input = sys.stdin.readline

def main():
	(n, ma, mb) = list(map(int, input().split()))
	dp = [[0] * 401 for _ in range(401)]
	for _ in range(n):
		(a, b, c) = list(map(int, input().split()))
		for i in range(400, -1, -1):
			for j in range(400, -1, -1):
				if i == 0 and j == 0:
					if dp[a][b]:
						dp[a][b] = min(dp[a][b], c)
					else:
						dp[a][b] = c
				if dp[i][j] and i + a <= 400 and (j + b <= 400):
					if dp[i + a][j + b]:
						dp[i + a][j + b] = min(dp[i + a][j + b], dp[i][j] + c)
					else:
						dp[i + a][j + b] = dp[i][j] + c
	ans = 1000000
	(x, y) = (ma, mb)
	while x <= 400 and y <= 400:
		if dp[x][y]:
			ans = min(dp[x][y], ans)
		x += ma
		y += mb
	if ans == 1000000:
		print(-1)
	else:
		print(ans)

def __starting_point():
	main()
__starting_point()


import collections
import copy
INF = 10000
(N, Ma, Mb) = map(int, input().split())
medichine = [tuple(map(int, input().split())) for _ in range(N)]
dp = {}
dp = collections.defaultdict(lambda : INF)
dp[0, 0] = 0
for (a, b, c) in medichine:
	dp_ = copy.copy(dp)
	for (ca, cb) in dp.keys():
		dp_[ca + a, cb + b] = min(dp_[ca + a, cb + b], dp[ca, cb] + c)
	dp = dp_
ans = INF
for (ca, cb) in dp.keys():
	if ca == cb == 0:
		continue
	if ca * Mb == cb * Ma:
		ans = min(ans, dp[ca, cb])
print(-1 if ans == INF else ans)


inf = float('inf')
(N, Ma, Mb) = map(int, input().split())
dp = [[inf] * (400 + 1) for _ in range(400 + 1)]
dp[0][0] = 0
max_a = 0
max_b = 0
for i in range(N):
	(a, b, c) = map(int, input().split())
	max_a += a
	max_b += b
	for j in reversed(range(max_a + 1 - a)):
		for k in range(max_b + 1 - b):
			if dp[j][k] == inf:
				continue
			dp[j + a][k + b] = min(dp[j][k] + c, dp[j + a][k + b])
j = Ma
k = Mb
ans = inf
while j < 400 + 1 and k < 400 + 1:
	ans = min(ans, dp[j][k])
	j += Ma
	k += Mb
print(-1 if ans == inf else ans)


import sys
readline = sys.stdin.readline
(N, Ma, Mb) = map(int, readline().split())
INF = 10 ** 10
dp = [[INF] * 401 for i in range(401)]
dp[0][0] = 0
for _ in range(N):
	(a, b, c) = map(int, readline().split())
	for i in range(len(dp) - 1, -1, -1):
		for j in range(len(dp[i]) - 1, -1, -1):
			if dp[i][j] == INF:
				continue
			if dp[i + a][j + b] > dp[i][j] + c:
				dp[i + a][j + b] = dp[i][j] + c
ans = INF
import math
for i in range(1, len(dp)):
	for j in range(1, len(dp)):
		g = math.gcd(i, j)
		if i // g == Ma and j // g == Mb:
			if ans > dp[i][j]:
				ans = dp[i][j]
if ans == INF:
	print(-1)
else:
	print(ans)


def main():
	INF = 10 ** 5
	(n, ma, mb) = map(int, input().split())
	dp = [[INF] * 401 for _ in range(401)]
	dp[0][0] = 0
	for _ in range(n):
		(a, b, c) = map(int, input().split())
		for i in range(400 - a, -1, -1):
			dpi = dp[i]
			dpia = dp[i + a]
			for j in range(400 - b, -1, -1):
				if dpia[j + b] > dpi[j] + c:
					dpia[j + b] = dpi[j] + c
	ans = INF
	for i in range(1, 400):
		if ma * i > 400 or mb * i > 400:
			break
		ans = min(ans, dp[ma * i][mb * i])
	if ans >= INF:
		print(-1)
	else:
		print(ans)
main()


(n, A, B) = map(int, input().split())
z = [tuple(map(int, input().split())) for _ in range(n)]
inf = 10 ** 18
d = {(0, 0): 0}
for (a, b, cost) in z:
	newd = []
	for ((x, y), c) in d.items():
		new = (a + x, b + y)
		if new not in d or d[new] > c + cost:
			newd.append((new, c + cost))
	for (new, newc) in newd:
		d[new] = newc
ans = inf
for i in range(1, 10 * n):
	t = (A * i, B * i)
	if t in d:
		ans = min(ans, d[t])
if ans == inf:
	print(-1)
else:
	print(ans)


(N, Ma, Mb) = list(map(int, input().split()))
D = [tuple(map(int, input().split())) for _ in range(N)]

def listup(L, D):
	for (a, b, c) in D:
		for ((ka, kb), kc) in L.copy().items():
			key = (ka + a, kb + b)
			L[key] = min(L[key], kc + c) if key in L else kc + c
L1 = {(0, 0): 0}
L2 = {(0, 0): 0}
listup(L1, D[:N // 2])
listup(L2, D[N // 2:])
ans = 10 ** 9
for m in range(1, 401):
	ma = Ma * m
	mb = Mb * m
	if ma > 400 or mb > 400:
		break
	for key1 in L1:
		(ta1, tb1) = key1
		ta2 = ma - ta1
		tb2 = mb - tb1
		key2 = (ta2, tb2)
		if key2 in L2:
			ans = min(ans, L1[key1] + L2[key2])
print(ans if ans != 10 ** 9 else -1)


import fractions, copy, collections
(n, ma, mb) = map(int, input().split())
rows = [map(int, input().split()) for _ in range(n)]
ht = {(0, 0): 0}
for (a, b, c) in rows:
	temp = copy.deepcopy(ht)
	for (x, y) in ht:
		if not (x + a, y + b) in ht or ht[x, y] + c < ht[x + a, y + b]:
			temp[x + a, y + b] = ht[x, y] + c
	ht = temp
ans = float('inf')
for (a, b) in ht:
	if b != 0 and fractions.Fraction(ma, mb) == fractions.Fraction(a, b):
		ans = min(ans, ht[a, b])
print(-1 if ans == float('inf') else ans)


import sys
sys.setrecursionlimit(2147483647)
INF = float('inf')
MOD = 10 ** 9 + 7
input = lambda : sys.stdin.readline().rstrip()
from itertools import product
from copy import deepcopy

def resolve():
	(n, ma, mb) = map(int, input().split())
	ABC = [tuple(map(int, input().split())) for _ in range(n)]
	M = 400
	dp = [[INF] * (M + 1) for _ in range(M + 1)]
	dp[0][0] = 0
	for (a, b, c) in ABC:
		for (x, y) in product(range(M, -1, -1), repeat=2):
			if x + a <= M and y + b <= M:
				dp[x + a][y + b] = min(dp[x + a][y + b], dp[x][y] + c)
	ans = INF
	for (x, y) in product(range(1, M + 1), repeat=2):
		if x * mb == y * ma:
			ans = min(ans, dp[x][y])
	if ans == INF:
		ans = -1
	print(ans)
resolve()


(N, Ma, Mb) = map(int, input().split())
arr = [list(map(int, input().split())) for _ in range(N)]
dp = [[[5000] * 401 for _ in range(401)] for _ in range(41)]
dp[0][0][0] = 0
for i in range(N):
	(a, b, c) = arr[i]
	for j in range(10 * N + 1):
		for k in range(10 * N + 1):
			if dp[i][j][k] == 5000:
				continue
			dp[i + 1][j][k] = min(dp[i + 1][j][k], dp[i][j][k])
			dp[i + 1][j + a][k + b] = min(dp[i + 1][j + a][k + b], dp[i][j][k] + c)
ans = 5000
for i in range(1, 401):
	if i * Ma > 400 or i * Mb > 400:
		break
	ans = min(ans, dp[N][i * Ma][i * Mb])
if ans == 5000:
	print(-1)
else:
	print(ans)


inf = float('inf')
(N, Ma, Mb) = map(int, input().split())
ABC = []
for _ in range(N):
	(a, b, c) = map(int, input().split())
	ABC.append((a, b, c))
dp = [[inf] * 420 for _ in range(420)]
dp[0][0] = 0
for (a, b, c) in ABC:
	for i in range(410, -1, -1):
		for j in range(410, -1, -1):
			if dp[i][j] < inf:
				dp[i + a][j + b] = min(dp[i + a][j + b], dp[i][j] + c)
ans = inf
n = 1
while max(Ma * n, Mb * n) < 420:
	ans = min(ans, dp[Ma * n][Mb * n])
	n += 1
print(ans if ans < inf else -1)


import numpy as np
(N, Ma, Mb) = map(int, input().split())
ls = []
dp = np.full((1001, 1001), float('inf'))
dp[0][0] = 0
for i in range(N):
	(a, b, c) = map(int, input().split())
	dp[a:, b:] = np.minimum(dp[:1001 - a, :1001 - b] + c, dp[a:, b:])
ans = float('inf')
for i in range(1, 1001):
	if i * Ma > 1000 or i * Mb > 1000:
		break
	if dp[i * Ma, i * Mb] < ans:
		ans = dp[i * Ma, i * Mb]
if ans == float('inf'):
	ans = -1
print(int(ans))


(N, Ma, Mb) = list(map(int, input().strip().split()))
abc = [list(map(int, input().strip().split())) for i in range(N)]
maxW = N * 10
dp = [{} for __ in range(N + 1)]
dp[0][0, 0] = 0
for i in range(N):
	(a, b, c) = abc[i]
	for (k, v) in dp[i].items():
		dp[i + 1][k] = min(dp[i][k], dp[i + 1][k] if k in dp[i + 1] else float('inf'))
		dp[i + 1][k[0] + a, k[1] + b] = min(dp[i][k] + c, dp[i + 1][k[0] + a, k[1] + b] if (k[0] + a, k[1] + b) in dp[i + 1] else float('inf'))
cost = float('inf')
Mad = Ma
Mbd = Mb
while Ma <= maxW and Mb <= maxW:
	if (Ma, Mb) in dp[-1]:
		cost = min(dp[-1][Ma, Mb], cost)
	Ma += Mad
	Mb += Mbd
print(-1 if cost == float('inf') else cost)


(N, A, B) = list(map(int, input().split()))
item = []
for i in range(N):
	(a, b, c) = list(map(int, input().split()))
	item.append([a, b, c])
D = [[[10 ** 27 for i in range(401)] for j in range(401)] for k in range(N + 1)]
D[0][0][0] = 0
for i in range(N):
	for j in range(401):
		for k in range(401):
			if D[i][j][k] < 10 ** 26:
				D[i + 1][j][k] = min(D[i + 1][j][k], D[i][j][k])
				D[i + 1][j + item[i][0]][k + item[i][1]] = min(D[i + 1][j + item[i][0]][k + item[i][1]], D[i][j][k] + item[i][2])
i = 1
min_cost = 10 ** 27
while A * i <= 400 and B * i <= 400:
	min_cost = min(D[N][A * i][B * i], min_cost)
	i += 1
if min_cost == 10 ** 27:
	print(-1)
else:
	print(min_cost)


(nmax, abmax, inf) = (40, 10, 1000000)
dp = [[[inf] * (nmax * abmax + 1) for _ in range(nmax * abmax + 1)] for _ in range(nmax + 1)]
(n, ma, mb) = list(map(int, input().split()))
dp[0][0][0] = 0
for i in range(n):
	(a, b, c) = list(map(int, input().split()))
	dpi = dp[i]
	dpi1 = dp[i + 1]
	for ca in range(0, 401):
		for cb in range(0, 401):
			if dpi[ca][cb] == inf:
				continue
			dpi1[ca][cb] = min(dpi1[ca][cb], dpi[ca][cb])
			dpi1[ca + a][cb + b] = min(dpi1[ca + a][cb + b], dpi[ca][cb] + c)
ans = inf
dpn = dp[n]
for ca in range(1, 401):
	for cb in range(1, 401):
		if ca * mb == cb * ma:
			ans = min(ans, dpn[ca][cb])
if ans == inf:
	ans = -1
print(ans)


import copy
(N, Ma, Mb) = [int(n) for n in input().split()]
pd = {(0, 0): 0}
for i in range(N):
	(a, b, c) = [int(n) for n in input().split()]
	pdcopy = copy.deepcopy(pd)
	for (k, v) in pdcopy.items():
		nk = (k[0] + a, k[1] + b)
		if nk in pd:
			if pd[nk] > v + c:
				pd[nk] = v + c
		else:
			pd[nk] = v + c
mp = 1000000
for i in range(1, 401):
	if (Ma * i, Mb * i) in pd:
		mp = min(pd[Ma * i, Mb * i], mp)
if mp == 1000000:
	print(-1)
else:
	print(mp)


import sys
read = sys.stdin.readline
(N, Ma, Mb) = map(int, read().split())
abc = [tuple(map(int, read().split())) for _ in range(N)]
d = [(Mb * a - Ma * b, c) for (a, b, c) in abc]
dic = dict()
for (i, j) in d:
	for (key, value) in tuple(dic.items()):
		n = key + i
		if dic.get(n):
			dic[n] = min(value + j, dic[n])
		else:
			dic[n] = value + j
	if dic.get(i):
		dic[i] = min(j, dic[i])
	else:
		dic[i] = j
if dic.get(0):
	print(dic[0])
else:
	print(-1)


(N, Ma, Mb) = list(map(int, input().split()))
INF = 10000
dp = [[INF for b in range(401)] for a in range(401)]
dp[0][0] = 0
sum_a = 0
sum_b = 0
for i in range(1, N + 1):
	(ai, bi, ci) = list(map(int, input().split()))
	sum_a += ai
	sum_b += bi
	for a in range(sum_a + 1, -1, -1):
		for b in range(sum_b + 1, -1, -1):
			if a - ai >= 0 and b - bi >= 0:
				dp[a][b] = min(dp[a][b], dp[a - ai][b - bi] + ci)
ans = INF
for j in range(1, 401):
	if Ma * j > 400 or Mb * j > 400:
		break
	ans = min(ans, dp[Ma * j][Mb * j])
print(ans if ans != INF else -1)


(n, ma, mb) = list(map(int, input().split()))
item = []
for i in range(n):
	item.append(tuple(map(int, input().split())))
d = {(0, 0): 0}
for i in range(n):
	nd = {}
	for j in d:
		if j in nd:
			nd[j] = min(nd[j], d[j])
		else:
			nd[j] = d[j]
		s = (j[0] + item[i][0], j[1] + item[i][1])
		if s in nd:
			nd[s] = min(nd[s], d[j] + item[i][2])
		else:
			nd[s] = d[j] + item[i][2]
	d = nd
m = 10 ** 9
for i in d:
	if i == (0, 0):
		continue
	if i[0] * mb == i[1] * ma:
		m = min(m, d[i])
if m == 10 ** 9:
	m = -1
print(m)


(n, ma, mb) = map(int, input().split())
x = []
amax = 0
bmax = 0
for i in range(n):
	(x1, y1, z1) = [int(i) for i in input().split()]
	x.append([x1, y1, z1])
	amax += x1
	bmax += y1
inf = float('inf')
dp = [[inf] * (amax + 1) for i in range(bmax + 1)]
dp[0][0] = 0
aru = {(0, 0)}
for i in x:
	a = i[0]
	b = i[1]
	c = i[2]
	aru2 = list(aru)
	aru2 = sorted(aru2, key=lambda x: -x[0])
	for j in aru2:
		dp[j[1] + b][j[0] + a] = min(dp[j[1] + b][j[0] + a], dp[j[1]][j[0]] + c)
		aru.add((j[0] + a, j[1] + b))
ans = inf
mam = ma + 0
mbm = mb + 0
while amax >= ma and bmax >= mb:
	suu = dp[mb][ma]
	ans = min(ans, suu)
	mb += mbm
	ma += mam
if ans == inf:
	print(-1)
else:
	print(ans)


def main():
	(n, ma, mb) = map(int, input().split())
	arr = [list(map(int, input().split())) for _ in range(n)]
	dp = [{} for _ in range(n + 1)]
	dp[0][0, 0] = 0
	for i in range(n):
		for ky in dp[i].keys():
			if ky in dp[i + 1]:
				if dp[i + 1][ky] > dp[i][ky]:
					dp[i + 1][ky] = dp[i][ky]
			else:
				dp[i + 1][ky] = dp[i][ky]
			t = (ky[0] + arr[i][0], ky[1] + arr[i][1])
			if t in dp[i + 1]:
				if dp[i + 1][t] > dp[i][ky] + arr[i][2]:
					dp[i + 1][t] = dp[i][ky] + arr[i][2]
			else:
				dp[i + 1][t] = dp[i][ky] + arr[i][2]
	ans = float('inf')
	for i in range(1, 41):
		t = (ma * i, mb * i)
		if t in dp[n]:
			if ans > dp[n][t]:
				ans = dp[n][t]
	if ans == float('inf'):
		print('-1')
	else:
		print(ans)

def __starting_point():
	main()
__starting_point()


from collections import defaultdict
(N, Ma, Mb) = list(map(int, input().split()))
M = [tuple(map(int, input().split())) for _ in range(N)]
d = defaultdict(lambda : float('inf'))
d[0, 0] = 0
for (a, b, c) in M:
	new_d = d.copy()
	for (key_a, key_b) in d:
		new_d[a + key_a, b + key_b] = min(new_d[a + key_a, b + key_b], d[key_a, key_b] + c)
	d = new_d
ans = float('inf')
for i in range(1, 401 // max(Ma, Mb)):
	ans = min(ans, d[Ma * i, Mb * i])
print(ans if ans != float('inf') else -1)


(N, Ma, Mb) = map(int, input().split())
sz = 401
table = [[99999999] * (sz + 1) for _ in range(sz + 1)]
table[0][0] = 0
for n in range(N):
	(a, b, c) = map(int, input().split())
	for i in range(sz, -1, -1):
		for j in range(sz, -1, -1):
			if table[i][j] != 99999999:
				table[i + a][j + b] = min(table[i + a][j + b], table[i][j] + c)
ans = 99999999
(i, j) = (Ma, Mb)
while i <= sz and j <= sz:
	ans = min(ans, table[i][j])
	i += Ma
	j += Mb
print(ans if ans != 99999999 else -1)


(N, MA, MB) = map(int, input().split())
ABC = [tuple(map(int, input().split())) for i in range(N)]
from collections import defaultdict
from copy import copy
d = defaultdict(lambda : float('inf'))
d[0, 0] = 0
for (a, b, c) in ABC:
	d2 = copy(d)
	for ((pa, pb), pc) in d.items():
		d2[pa + a, pb + b] = min(d2[pa + a, pb + b], pc + c)
	d = d2
ans = float('inf')
for ((a, b), c) in d.items():
	if a == b == 0:
		continue
	if a * MB == b * MA:
		ans = min(ans, c)
print(-1 if ans == float('inf') else ans)


(N, Ma, Mb) = [int(_) for _ in input().split()]
T = [[int(_) for _ in input().split()] for i in range(N)]
dp = [[float('inf') for i in range(401)] for i in range(401)]
dp[0][0] = 0
sum_a = 0
sum_b = 0
for x in range(N):
	(a, b, c) = T[x]
	sum_a += a
	sum_b += b
	for i in list(range(a, sum_a + 1))[::-1]:
		for j in list(range(b, sum_b + 1))[::-1]:
			dp[i][j] = min(dp[i][j], dp[i - a][j - b] + c)
ans = float('inf')
for i in range(1, sum_a + 1):
	for j in range(1, sum_b + 1):
		if i * Mb == j * Ma:
			ans = min(ans, dp[i][j])
print(ans if ans != float('inf') else -1)


from itertools import permutations
(n, ma, mb) = map(int, input().split())
abc = []
inf = float('INF')
hw = 400 + 1
dp = [[inf for i in range(hw)] for j in range(hw)]
dp[0][0] = 0
for _ in range(n):
	(a, b, c) = map(int, input().split())
	abc.append((a, b, c))
abc.sort(key=lambda x: (-x[0], x[1]))
for (a, b, c) in abc:
	for i in range(hw - a)[::-1]:
		for j in range(hw - b)[::-1]:
			if dp[i + a][j + b] > dp[i][j] + c:
				dp[i + a][j + b] = dp[i][j] + c
(a, b) = (ma, mb)
ans = [dp[a * i][b * i] for i in range(1, hw // max(a, b)) if dp[a * i][b * i] < inf]
print(min(ans) if len(ans) else -1)


(n, ma, mb) = map(int, input().split())
INF = 10 ** 9
dp = {(0, 0): 0}
for _ in range(n):
	(a, b, c) = map(int, input().split())
	for ((ea, eb), ec) in dp.copy().items():
		dp[ea + a, eb + b] = min(dp.get((ea + a, eb + b), INF), ec + c)
ans = min((dp.get((i * ma, i * mb), INF) for i in range(1, 401)))
print(ans if ans != INF else -1)


(N, Ma, Mb) = map(int, input().split())
medic = [tuple(map(int, input().split())) for _ in range(N)]
from collections import defaultdict
inf = float('inf')
dp = defaultdict(lambda : inf)
dp[0, 0] = 0
for n in range(N):
	dpc = dp.copy()
	(a, b, c) = medic[n]
	for (d, cost) in dpc.items():
		(da, db) = d
		dp[da + a, db + b] = min(dp[da + a, db + b], c + cost)
ans = inf
(nowa, nowb) = (Ma, Mb)
while nowa <= 400 and nowb <= 400:
	ans = min(ans, dp[nowa, nowb])
	nowa += Ma
	nowb += Mb
if ans > 4000:
	ans = -1
print(ans)


import sys
input = sys.stdin.readline

def main():
	(N, Ma, Mb) = list(map(int, input().split()))
	abc = [list(map(int, input().split())) for i in range(N)]
	INF = N * 100 + 1
	dp = [[[INF for cb in range(411)] for ca in range(411)] for i in range(N + 1)]
	dp[0][0][0] = 0
	for i in range(N):
		for ca in range(N * 10):
			for cb in range(N * 10):
				if dp[i][ca][cb] == INF:
					continue
				dp[i + 1][ca][cb] = min(dp[i + 1][ca][cb], dp[i][ca][cb])
				d = abc[i]
				dp[i + 1][ca + d[0]][cb + d[1]] = min(dp[i + 1][ca + d[0]][cb + d[1]], dp[i][ca][cb] + d[2])
	res = INF
	for ca in range(1, 401):
		for cb in range(1, 401):
			if ca * Mb == cb * Ma:
				res = min(res, dp[N][ca][cb])
	if res == INF:
		res = -1
	print(res)
main()


ab = [[float('inf') for j in range(401)] for i in range(401)]
(n, ma, mb) = map(int, input().split())
(a, b, c) = map(int, input().split())
ab[a][b] = c
ab[0][0] = 0
for i in range(n - 1):
	(a, b, c) = map(int, input().split())
	for j in range(400, -1, -1):
		for k in range(400, -1, -1):
			if ab[j][k] != float('inf'):
				ab[j + a][k + b] = min(ab[j][k] + c, ab[j + a][k + b])
ans = float('inf')
(a, b) = (ma, mb)
while a < 401 and b < 401:
	ans = min(ans, ab[a][b])
	a += ma
	b += mb
if ans == float('inf'):
	ans = -1
print(ans)


import sys
input = sys.stdin.buffer.readline
(N, Ma, Mb) = map(int, input().split())
ABC = [list(map(int, input().split())) for _ in range(N)]
INF = 10 ** 15
dp = [[INF for j in range(401)] for i in range(401)]
dp[0][0] = 0
for (a, b, c) in ABC:
	for i in range(400, -1, -1):
		for j in range(400, -1, -1):
			if dp[i][j] != INF:
				dp[i + a][j + b] = min(dp[i + a][j + b], dp[i][j] + c)
answer = INF
for i in range(1, 401):
	for j in range(1, 401):
		if dp[i][j] != INF and i / j == Ma / Mb:
			answer = min(answer, dp[i][j])
print(answer if answer != INF else -1)


(N, MA, MB) = map(int, input().split())
src = [tuple(map(int, input().split())) for i in range(N)]
INF = 10 ** 9
dp = [[INF for j in range(401)] for i in range(401)]
dp[0][0] = 0
maxa = maxb = 0
for (a, b, c) in src:
	for ai in range(maxa, -1, -1):
		for bi in range(maxb, -1, -1):
			dp[ai + a][bi + b] = min(dp[ai + a][bi + b], dp[ai][bi] + c)
	maxa += a
	maxb += b
ans = INF
for i in range(1, 401):
	if MA * i > 400 or MB * i > 400:
		break
	ans = min(ans, dp[MA * i][MB * i])
print(-1 if ans == INF else ans)


import numpy as np
(n, ma, mb) = map(int, input().split())
r = 10 * n + 6
inf = 10 ** 5
dp = np.full((n + 1, r, r), inf)
dp[0][0][0] = 0
for i in range(n):
	(a, b, c) = map(int, input().split())
	dp[i + 1][a:, b:] = np.minimum(dp[i][:r - a, :r - b] + c, dp[i][a:, b:])
	dp[i + 1] = np.minimum(dp[i + 1], dp[i])
ans = inf
(x, y) = (0, 0)
while True:
	x += ma
	y += mb
	if x < r and y < r:
		ans = min(ans, dp[n][x][y])
	else:
		break
if ans == inf:
	print(-1)
else:
	print(int(ans))


def inpl():
	return map(int, input().split())
(N, Ma, Mb) = inpl()
INF = 4001
DP = [[INF] * 411 for _ in range(411)]
DP[0][0] = 0
for n in range(N):
	DP2 = [[INF] * 411 for _ in range(411)]
	(a, b, c) = inpl()
	for i in range(10 * (n + 1) + 1):
		for j in range(10 * (n + 1) + 1):
			if DP[i][j] == INF:
				continue
			DP2[i][j] = min(DP[i][j], DP2[i][j])
			DP2[i + a][j + b] = min(DP[i + a][j + b], DP[i][j] + c)
	DP = DP2
x = 1
ans = INF
while Ma * x <= 400 and Mb * x <= 400:
	ans = min(ans, DP[Ma * x][Mb * x])
	x += 1
if ans < INF:
	print(ans)
else:
	print(-1)


(N, Ma, Mb) = list(map(int, input().split()))
mix = set([(0, 0)])
ans = 10 ** 9
cost = {(0, 0): 0}
for i in range(N):
	(a, b, c) = list(map(int, input().split()))
	tmp = mix.copy()
	tmp2 = cost.copy()
	for x in tmp:
		d = x[0] + a
		e = x[1] + b
		f = min(tmp2.get((d, e), 10 ** 9), tmp2[x] + c)
		cost[d, e] = f
		mix.add((d, e))
		if d * Mb == e * Ma and f < ans:
			ans = f
if ans == 10 ** 9:
	ans = -1
print(ans)


(N, Ma, Mb) = map(int, input().split())
abc = []
INF = float('inf')
for i in range(N):
	(a, b, c) = map(int, input().split())
	abc.append((a, b, c))
stack = set([(0, 0)])
dp = [[[INF] * 401 for j in range(401)] for i in range(N + 1)]
dp[0][0][0] = 0
for i in range(N):
	dp[i + 1][0][0] = 0
	(a, b, c) = abc[i]
	stack_ = set()
	for (x, y) in stack:
		dp[i + 1][x][y] = min(dp[i][x][y], dp[i + 1][x][y])
		dp[i + 1][x + a][y + b] = min(dp[i + 1][x + a][y + b], dp[i][x + a][y + b], dp[i][x][y] + c)
		stack_.add((x + a, y + b))
		stack_.add((x, y))
	stack = stack_
k = 1
ans = INF
while Ma * k <= 400 and Mb * k <= 400:
	ans = min(dp[N][Ma * k][Mb * k], ans)
	k += 1
if ans == INF:
	print(-1)
else:
	print(ans)


(n, ma, mb) = map(int, input().split())
abc = []
for _ in range(n):
	(a, b, c) = map(int, input().split())
	abc.append((a, b, c))
dp = {(0, 0): 0}
for (a, b, c) in abc:
	newdp = dp.copy()
	for ((ka, kb), p) in dp.items():
		k = (a + ka, b + kb)
		if k in newdp:
			newdp[k] = min(newdp[k], p + c)
		else:
			newdp[k] = p + c
	dp = newdp
INF = 10 ** 10
ans = INF
for ((a, b), c) in dp.items():
	if ma * b != mb * a or a == b == 0:
		continue
	ans = min(ans, c)
if ans == INF:
	print(-1)
else:
	print(ans)


INF = float('inf')
(n, ma, mb) = list(map(int, input().split()))
t = [[INF] * 401 for _ in range(401)]
t[0][0] = 0
for _ in range(n):
	(a, b, c) = list(map(int, input().split()))
	for aa in range(400, -1, -1):
		for bb in range(400, -1, -1):
			if t[aa][bb] == INF:
				continue
			if t[a + aa][b + bb] > t[aa][bb] + c:
				t[a + aa][b + bb] = t[aa][bb] + c
result = INF
for a in range(400, 0, -1):
	for b in range(400, 0, -1):
		if a * mb == b * ma and t[a][b] < result:
			result = t[a][b]
if result == INF:
	result = -1
print(result)


def inpl():
	return [int(i) for i in input().split()]
(N, Ma, Mb) = inpl()
inf = float('inf')
H = {(0, 0): 0}
for i in range(N):
	(a, b, c) = inpl()
	for ((ia, ib), ic) in H.copy().items():
		H[ia + a, ib + b] = min(H.get((ia + a, ib + b), inf), ic + c)
ans = min((H.get((i * Ma, i * Mb), inf) for i in range(1, 401)))
print(-1 if ans == inf else ans)


(N, Ma, Mb) = map(int, input().split())
INF = 10 ** 9
dp = {(0, 0): 0}
for _ in range(N):
	(a, b, c) = map(int, input().split())
	for ((pa, pb), pc) in dp.copy().items():
		dp[pa + a, pb + b] = min(dp.get((pa + a, pb + b), INF), pc + c)
ans = min((dp.get((i * Ma, i * Mb), INF) for i in range(1, 401)))
print(ans if ans != INF else -1)


(N, Ma, Mb) = map(int, input().split())
INF = 10 ** 9 + 7
dp = [[[INF] * 401 for _ in range(401)] for __ in range(N + 1)]
dp[0][0][0] = 0
for n in range(N):
	(x, y, c) = map(int, input().split())
	for a in range(401):
		for b in range(401):
			if dp[n][a][b] == INF:
				continue
			dp[n + 1][a][b] = min(dp[n + 1][a][b], dp[n][a][b])
			if a + x <= 400 and b + y <= 400:
				dp[n + 1][a + x][b + y] = min(dp[n + 1][a + x][b + y], dp[n][a][b] + c)
ans = INF
(a, b) = (0, 0)
while True:
	a += Ma
	b += Mb
	if a > 400 or b > 400:
		break
	ans = min(ans, dp[N][a][b])
if ans == INF:
	print(-1)
else:
	print(ans)


(N, Ma, Mb) = map(int, input().split())
dp = [[10 ** 18 for _ in range(401)] for _ in range(401)]
dp[0][0] = 0
for i in range(N):
	(a, b, c) = map(int, input().split())
	for ca in range(i * 10, -1, -1):
		for cb in range(i * 10, -1, -1):
			dp[ca + a][cb + b] = min(dp[ca + a][cb + b], dp[ca][cb] + c)
ans = 10 ** 18
for ca in range(401):
	for cb in range(401):
		if not ca * Mb == cb * Ma or ca == 0:
			continue
		if ans > dp[ca][cb]:
			ans = dp[ca][cb]
if ans == 10 ** 18:
	print(-1)
else:
	print(ans)


(N, MA, MB) = map(int, input().split())
ABC = [tuple(map(int, input().split())) for i in range(N)]
INF = float('inf')
from collections import defaultdict
dp = defaultdict(lambda : INF)
dp[0, 0] = 0
for (a, b, c) in ABC:
	dp2 = defaultdict(lambda : INF)
	for ((pa, pb), pc) in dp.items():
		dp2[pa, pb] = min(dp2[pa, pb], pc)
		dp2[pa + a, pb + b] = min(dp2[pa + a, pb + b], pc + c)
	dp = dp2
ans = INF
for i in range(1, 401):
	ans = min(ans, dp[MA * i, MB * i])
print(-1 if ans == INF else ans)


(N, MA, MB) = map(int, input().split())
S = [list(map(int, input().split())) for i in range(N)]
DPLIST = [[10 ** 6 for i in range(401)] for j in range(401)]
DPLIST[0][0] = 0
for (a, b, c) in S:
	for i in range(400, a - 1, -1):
		for j in range(400, b - 1, -1):
			if DPLIST[i][j] > DPLIST[i - a][j - b] + c:
				DPLIST[i][j] = DPLIST[i - a][j - b] + c
ANS = 10 ** 6
for k in range(1, 400 // max(MA, MB)):
	if DPLIST[MA * k][MB * k] < ANS:
		ANS = DPLIST[MA * k][MB * k]
if ANS == 10 ** 6:
	print(-1)
else:
	print(ANS)


def main():
	(N, Ma, Mb) = map(int, input().split())
	drug = []
	for _ in range(N):
		(a, b, c) = map(int, input().split())
		drug.append([a, b, c])
	cur = dict()
	cur[0, 0] = 0
	for i in range(N):
		temp = cur.copy()
		for p in cur:
			(a, b) = (p[0] + drug[i][0], p[1] + drug[i][1])
			if (a, b) not in temp:
				temp[a, b] = cur[p] + drug[i][2]
			else:
				temp[a, b] = min(temp[a, b], cur[p] + drug[i][2])
		cur = temp
	min_c = float('inf')
	for p in cur:
		if p != (0, 0) and p[0] * Mb == p[1] * Ma:
			min_c = min(min_c, cur[p])
	if min_c == float('inf'):
		return -1
	else:
		return min_c

def __starting_point():
	print(main())
__starting_point()


(n, ma, mb) = map(int, input().split())
p = []
for i in range(n):
	(a, b, c) = map(int, input().split())
	p.append((a, b, c))
r = [[40001 for j in range(401)] for i in range(401)]
r[0][0] = 0
for i in range(n):
	for j in range(400 - 10 * i, -1, -1):
		for k in range(400 - 10 * i, -1, -1):
			if 40001 > r[j][k]:
				r[j + p[i][0]][k + p[i][1]] = min(r[j + p[i][0]][k + p[i][1]], r[j][k] + p[i][2])
min_v = 40001
ia = ma
ib = mb
flag = False
while ia <= 400 and ib <= 400:
	if 40001 > r[ia][ib] > 0:
		min_v = min(min_v, r[ia][ib])
		flag = True
	ia += ma
	ib += mb
if not flag:
	print(-1)
else:
	print(min_v)


def main():
	from collections import defaultdict
	INF = 40 * 100 + 1
	(N, Ma, Mb) = map(int, input().split())
	memo = defaultdict(lambda : INF)
	for _ in range(N):
		(ai, bi, ci) = map(int, input().split())
		x = Ma * bi - Mb * ai
		for (key, value) in tuple(memo.items()):
			memo[key + x] = min(memo[key + x], value + ci)
		memo[x] = min(memo[x], ci)
	print(memo[0] if 0 in memo else -1)
main()


inf = 9999999999
(N, Ma, Mb) = map(int, input().split())
d = [{} for i in range(N + 1)]
for i in range(N):
	(a, b, c) = map(int, input().split())
	for (j, k) in d[i]:
		d[i + 1][j, k] = d[i][j, k]
	d[i + 1][a, b] = min(c, d[i].get((a, b), inf))
	for (j, k) in d[i]:
		d[i + 1][j + a, k + b] = min(d[i][j, k] + c, d[i].get((j + a, k + b), inf))
ans = inf
for (j, k) in d[N]:
	if j * Mb == k * Ma:
		ans = min(ans, d[N][j, k])
if ans == inf:
	ans = -1
print(ans)


dp_n = 410
(n, ma, mb) = map(int, input().split())
dp = [[float('inf')] * dp_n for _ in range(dp_n)]
dp[0][0] = 0
for i in range(n):
	(a, b, c) = map(int, input().split())
	for j in reversed(range(dp_n)):
		for i in reversed(range(dp_n)):
			if dp[i][j] != float('inf'):
				try:
					dp[i + a][j + b] = min(dp[i + a][j + b], dp[i][j] + c)
				except:
					pass
res = float('inf')
for j in range(1, dp_n):
	for i in range(1, dp_n):
		if i * mb == j * ma:
			res = min(res, dp[i][j])
print(res if res != float('inf') else -1)


def half_enumeration(a, *, func):
	n = len(a)
	h = n // 2

	def enum(h, *, second_half: bool=False) -> set:
		p = set()
		if second_half:
			b = a[h:]
		else:
			b = a[:h]
		for x in b:
			q = set()
			for e in p:
				q.add(func(e, x))
			p.add(x)
			p.update(q)
		return p
	return (enum(h), enum(h, second_half=True))

def main():
	INF = 40 * 100 + 1
	(N, Ma, Mb) = list(map(int, input().split()))

	def update(e, x):
		return (e[0] + x[0], e[1] + x[1])
	xcs = []
	for _ in range(N):
		(ai, bi, ci) = list(map(int, input().split()))
		x = Ma * bi - Mb * ai
		xcs.append((x, ci))
	(p1, p2) = half_enumeration(xcs, func=update)
	t1 = [INF] * (INF * 2 + 1)
	t2 = [INF] * (INF * 2 + 1)
	for (x, c) in p1:
		t1[x] = min(t1[x], c)
	for (x, c) in p2:
		t2[x] = min(t2[x], c)
	ans = min(t1[0], t2[0])
	for x in range(-INF, INF + 1):
		ans = min(ans, t1[x] + t2[-x])
	print(-1 if ans == INF else ans)

def __starting_point():
	main()
__starting_point()


(n, ma, mb) = map(int, input().split())
dp = [[[4001] * 401 for _ in range(401)] for j in range(n + 1)]
dp[0][0][0] = 0
a = [0] * n
b = [0] * n
c = [0] * n
for i in range(n):
	(a[i], b[i], c[i]) = map(int, input().split())
for i in range(n):
	for j in range(401):
		for k in range(401):
			if dp[i][j][k] == 4001:
				continue
			dp[i + 1][j][k] = min(dp[i + 1][j][k], dp[i][j][k])
			dp[i + 1][j + a[i]][k + b[i]] = min(dp[i + 1][j + a[i]][k + b[i]], dp[i][j][k] + c[i])
ans = 4001
for i in range(1, 401):
	for j in range(1, 401):
		if i * mb == j * ma:
			ans = min(ans, dp[n][i][j])
print(ans if ans != 4001 else -1)


def calc():
	(n, ma, mb) = map(int, input().split())
	dp = [[[5000] * 401 for _ in range(401)] for _ in range(41)]
	arr = [list(map(int, input().split())) for _ in range(n)]
	dp[0][0][0] = 0
	for t in range(n):
		(a, b, c) = arr[t]
		for i in range(401):
			for j in range(401):
				if dp[t][i][j] == 5000:
					continue
				dp[t + 1][i][j] = min(dp[t + 1][i][j], dp[t][i][j])
				dp[t + 1][i + a][j + b] = min(dp[t + 1][i + a][j + b], dp[t][i][j] + c)
	ans = 5000
	for i in range(1, 401):
		if i * ma > 400 or i * mb > 400:
			break
		ans = min(ans, dp[n][i * ma][i * mb])
	if ans == 5000:
		print(-1)
	else:
		print(ans)
calc()


(N, Ma, Mb) = map(int, input().split())
L = []
for _ in range(N):
	L.append(list(map(int, input().split())))
dp = [[[10 ** 10 for _ in range(601)] for _ in range(601)] for _ in range(N + 10)]
dp[0][0][0] = 0
for l in range(N):
	for i in range(200):
		for j in range(200):
			dp[l + 1][i][j] = min(dp[l + 1][i][j], dp[l][i][j])
			dp[l + 1][i + L[l][0]][j + L[l][1]] = min(dp[l + 1][i + L[l][0]][j + L[l][1]], dp[l][i][j] + L[l][2])
ans = 10 ** 10
for i in range(1, 10000):
	try:
		ans = min(ans, dp[N][Ma * i][Mb * i])
	except:
		pass
if ans == 10 ** 10:
	print(-1)
else:
	print(ans)


import sys
input = sys.stdin.readline
(N, Ma, Mb) = map(int, input().split())
ABC = [tuple(map(int, input().split())) for _ in range(N)]
inf = float('inf')
dp = [[inf for _ in range(401)] for _ in range(401)]
dp[0][0] = 0
for (a, b, c) in ABC:
	for i in range(400, -1, -1):
		for j in range(400, -1, -1):
			if dp[i][j] != inf:
				dp[i + a][j + b] = min(dp[i + a][j + b], dp[i][j] + c)
ans = inf
for i in range(1, 401):
	for j in range(1, 401):
		if i * Mb == j * Ma:
			ans = min(ans, dp[i][j])
print(ans if ans != inf else -1)


def push(k, v):
	if k in dic:
		dic[k] = min(dic[k], v)
	else:
		dic[k] = v
(n, x, y) = map(int, str(input()).split(' '))
dic = {}
for i in range(0, n):
	a = list(map(int, str(input()).split(' ')))
	(k, v) = (a[0] * y - a[1] * x, a[2])
	l = []
	for j in dic:
		l.append([j, dic[j]])
	for jj in l:
		push(jj[0] + k, jj[1] + v)
	push(k, v)
if 0 in dic:
	print(dic[0])
else:
	print('-1')


(N, Ma, Mb) = map(int, input().split())
INF = 10 ** 18
dp = [[[INF] * (10 * N + 1) for _ in range(10 * N + 1)] for _ in range(N + 1)]
dp[0][0][0] = 0
for i in range(N):
	(a, b, c) = map(int, input().split())
	for j in range(10 * N + 1 - a):
		for k in range(10 * N + 1 - b):
			if dp[i][j][k] == INF:
				continue
			dp[i + 1][j][k] = min(dp[i + 1][j][k], dp[i][j][k])
			dp[i + 1][j + a][k + b] = min(dp[i + 1][j + a][k + b], dp[i][j][k] + c)
ans = INF
for j in range(1, 10 * N + 1):
	if j * Ma > 10 * N or j * Mb > 10 * N:
		break
	ans = min(ans, dp[N][j * Ma][j * Mb])
if ans == INF:
	ans = -1
print(ans)


def getlist():
	return list(map(int, input().split()))
INF = float('inf')
(N, Ma, Mb) = getlist()
L = [[INF] * 401 for i in range(401)]
L[0][0] = 0
for i in range(N):
	(a, b, c) = getlist()
	for j in range(401):
		for k in range(401):
			if L[400 - j][400 - k] != INF:
				L[400 - j + b][400 - k + a] = min(L[400 - j][400 - k] + c, L[400 - j + b][400 - k + a])
ans = INF
x = Ma
y = Mb
while y <= 400 and x <= 400:
	ans = min(ans, L[y][x])
	x += Ma
	y += Mb
if ans == INF:
	print(-1)
else:
	print(ans)


(n, MA, MB) = map(int, input().split())
INF = 10 ** 10
A = []
B = []
C = []
for i in range(n):
	(a, b, c) = map(int, input().split())
	if MA * b - MB * a > 0:
		A.append([MA * b - MB * a, c])
	elif MA * b - MB * a < 0:
		B.append([MB * a - MA * b, c])
	else:
		C.append(c)
dp1 = [[INF] * 10010 for i in range(len(A) + 1)]
for i in range(1, len(A) + 1):
	for j in range(1, 10010):
		if j < A[i - 1][0]:
			dp1[i][j] = dp1[i - 1][j]
		elif j == A[i - 1][0]:
			dp1[i][j] = min(A[i - 1][1], dp1[i - 1][j])
		else:
			dp1[i][j] = min(dp1[i - 1][j], dp1[i - 1][j - A[i - 1][0]] + A[i - 1][1])
dp2 = [[INF] * 10010 for i in range(len(B) + 1)]
for i in range(1, len(B) + 1):
	for j in range(1, 10010):
		if j < B[i - 1][0]:
			dp2[i][j] = dp2[i - 1][j]
		elif j == B[i - 1][0]:
			dp2[i][j] = min(B[i - 1][1], dp2[i - 1][j])
		else:
			dp2[i][j] = min(dp2[i - 1][j], dp2[i - 1][j - B[i - 1][0]] + B[i - 1][1])
ans = INF
for i in range(10010):
	a = dp1[-1][i] + dp2[-1][i]
	if a < ans:
		ans = a
for i in range(len(C)):
	if C[i] < ans:
		ans = C[i]
if ans < INF:
	print(ans)
else:
	print(-1)


from collections import defaultdict
(n, ma, mb) = map(int, input().split())
f = defaultdict(lambda : float('inf'))
for _ in range(n):
	(a, b, c) = map(int, input().split())
	t = f.copy()
	t[a, b] = min(t[a, b], c)
	for (k, v) in f.items():
		(ka, kb) = (k[0] + a, k[1] + b)
		t[ka, kb] = min(t[ka, kb], v + c)
	f = t
costs = [f[a, b] for (a, b) in f if a * mb == b * ma]
print(min(costs) if costs else -1)


(N, MA, MB) = map(int, input().split())
src = [tuple(map(int, input().split())) for i in range(N)]
INF = 10 ** 9
MAX = 401
dp = [[INF] * MAX for i in range(MAX)]
dp[0][0] = 0
amax = bmax = 0
for (a, b, n) in src:
	for i in reversed(range(bmax + 1)):
		for j in range(amax + 1):
			if dp[i][j] == INF:
				continue
			dp[i + b][j + a] = min(dp[i + b][j + a], dp[i][j] + n)
	amax += a
	bmax += b
ans = INF
for i in range(1, MAX):
	if i * MA >= MAX or i * MB >= MAX:
		break
	ans = min(ans, dp[i * MB][i * MA])
print(-1 if ans == INF else ans)