#!/usr/bin/env python3 from sys import argv from itertools import product f = open(argv[1]).read().split("\n\n") i = {int(k):v[1:-1] if v[0] == v[-1] == "\"" else [[int(y) for y in x.split()] for x in v.split("|")] for k,v in map(lambda x: x.strip().split(": "), f[0].splitlines())} m = f[1].splitlines() def f(x): return [i[x]] if isinstance(i[x], str) else (y for c in i[x] for y in map(lambda x: "".join(x), product(*(f(r) for r in c)))) r = set(f(0)) print(sum(1 for x in m if x in r)) _42 = set(f(42)) _31 = set(f(31)) l = len(next(iter(_42))) # len(_42[0]) == len(_31[0]) s = 0 for x in m: if len(x) % l != 0: continue c = 0 r = len(x) while x[c:c+l] in _42: c += l while x[r-l:r] in _31: r -= l if c != 0 and r != len(x) and len(x) - r < c and r == c: s += 1 print(s)