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XNP => 6 JQRF
1 FTFK, 1 TVSF, 1 QBDX => 2 JBLM
1 TDHPM, 14 NHPR, 3 QPSF => 5 ZFVH
3 GDTPC, 1 ZKHLC => 8 ZFXNP
5 DWZS => 3 LQWJ
1 FTFK, 4 LBCZ, 13 NHPR => 1 FWFXZ
1 RCBQS, 12 SHBC => 9 FTFK
1 WSRVZ, 1 XKDJ => 5 LKRTN
2 BGTMN, 1 MDPN => 5 PLSM
2 BGXJV, 17 XKDJ, 4 FPTV => 9 LCKW
148 ORE => 2 QTBC
110 ORE => 2 VJWQR
42 ZFXNP, 15 RCKZ, 8 GDTPC => 3 QJLF
13 HRWS => 4 GDTPC
34 HRWS => 4 DFCVF
2 VKZFJ, 2 NHPR, 16 PLVN, 1 QPSF, 13 LBCZ, 4 DCRK, 10 LWHS => 7 NXZCH
3 CWHV, 1 THNDN => 7 LWHS
1 BGXJV, 2 QBDX => 5 DWZS
9 LQWJ => 8 QPSF
21 BCQN, 3 FMSZ, 1 RSKC => 5 THKF
118 ORE => 6 WXHJF
11 FMSZ => 9 TWLC
28 PLSM => 5 SHBC
1 ZKHLC, 23 SCJG => 7 LBCZ
17 DWZS, 16 THNDN => 9 PLVN
7 HDKX => 9 HRWS"""

d = dict()
for inp in s.split('\n'):
    a, b = inp.split('=>')
    n, r = b.strip().split(' ')
    d[r] = (int(n), a)

count = 10 ** 12
made = 0

extra = dict()

while count > 0:
    att = max(1, count // 1590844)
    req = dict([('FUEL', att)])

    while req:
        for mat in req.keys():
            break
        
        num = req[mat]
        if mat in extra:
            v = min(extra[mat], num)
            extra[mat] -= v
            num -= v

            
        del req[mat]
       
        if mat == 'ORE':
            if num <= count:
                count -= num
            else:
                count = 0
                break
        else:
            make, a = d[mat]
            need = (make + num - 1) // make
            extraV = need * make - num
            if mat in extra:
                pass
            else:
                extra[mat] = 0
            extra[mat] += extraV
            
            for s in a.split(','):
                val, matN = s.strip().split(' ')
                val = int(val)
                if matN in req:
                    req[matN] += val*need
                else:
                    req[matN] = val*need

    if count > 0:
        made += att

print(made)
