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, 1 JNSP => 1 WFPSM
7 FJVMD => 9 CXKN
3 GDPKF, 10 QSNCW => 7 ZDGPL
7 LPXM, 11 VSPSC => 1 LXMLX
6 RWDSX, 2 NMZND, 1 MPKC => 1 KHCQ
6 RWDSX => 4 QMJK
15 MHBH, 28 DBKN, 12 CNBS => 4 PGHF
20 NMZND, 1 PGHF, 1 BXVJK => 2 LPXM
1 CDSP, 17 BXVJK => 5 NHSTB
12 HVJVQ => 3 VSFTG
2 PGHF, 3 VCMVN, 2 NHSTB => 1 DFNVL
5 FNPC => 9 HBXF
3 DPRL => 4 FJVMD
1 KWFD, 1 TVJPG => 8 VCMVN
1 FDWSG, 1 VCWNW, 4 BDKN, 14 FDVNC, 1 CLZT, 62 SGLGN, 5 QMJK, 26 ZDGPL, 60 KCMH, 32 FVQM, 15 SRWZ => 1 FUEL
3 XHLNT => 8 TVJPG
5 HBXF => 2 HVJVQ
3 CHDTJ, 15 KWFD => 9 WTPN
7 CNTFK => 7 CNBS
1 CNBS => 2 JPDF
5 JNSP => 8 DPRL
11 NTXHM => 8 GDPKF
10 JPDF => 9 BXVJK'''
data = data.split('\n')

from math import ceil

ore = 0
formulas = dict()
leftovers = dict()
for line in data:
	line = line.split(' => ')
	ingredients = line[0]
	result = line[1]
	ingredients = ingredients.split(', ')
	#7 A, 2 B => 1 C devient C: [1, 2, 7, A, 2, B]
	#qte resultat, nombre d'ingredients, qte + valeur pr chaque ingrédient.
	formula = [int(result.split(' ')[0]), len(ingredients)]
	for ingredient in ingredients:
		formula.append(int(ingredient.split(' ')[0]))
		formula.append(ingredient.split(' ')[1])
	formulas[result.split(' ')[1]] = formula

def getIngredients(name, qty):
	global leftovers, ore

	formula = formulas[name]
	if not name in leftovers:
		leftovers[name] = 0
	factor = ceil((qty-leftovers[name])/formula[0])
	leftovers[name] += factor*formula[0]-qty
	
	for i in range(formula[1]):
		if formula[3+2*i] == 'ORE':
			ore -= factor*formula[2+2*i]
		else:
			getIngredients(formula[3+2*i], factor*formula[2+2*i])
	return

fuel = 0
ore = 1000000000000
while True:
	getIngredients('FUEL',1)
	fuel += 1
	if fuel%1000 == 0:
		print(fuel, ore)
	if ore < 0:
		fuel -= 1
		break
		
print('FINI', fuel)
