import sys from random import shuffle data = open(0).read().strip() eqs = [] text = "" for line in data.splitlines(): if "=>" in line: lhs, rhs = line.split(" => ") eqs.append((lhs, rhs)) elif line: text = line molecules = set() for lhs, rhs in eqs: for i in range(len(text)): f, t = text[:i], text[i:] if t.startswith(lhs): n = f + t.replace(lhs, rhs, 1) molecules.add(n) print(len(molecules)) # m = float("inf") # for _ in range(100000): # getout = False # c = str(text) # for i in range(10**4): # if i % 1000 == 0: # shuffle(eqs) # for lhs, rhs in eqs: # if rhs in c: # c = c.replace(rhs, lhs, 1) # if c == "e": # if i < m: # m = i # print(i) # getout = True # if getout: # break # print(m)