import sys from math import prod part = int(sys.argv[1]) T = sys.stdin.read().split('\n\n') M = [] # monkey: 0:items, 1:func, 2:divider, 3:iftrue, 4:iffalse, 5:count D = 1 for t in T: u = t.split('\n') m = [] m.append([int(i) for i in u[1].split(':')[1].split(',')]) m.append(u[2].split("=")[1]) d = int(u[3].split(" ")[-1]) m.append(d) # devider D *= d m.append(int(u[4].split(" ")[-1])) m.append(int(u[5].split(" ")[-1])) m.append(0) M.append(m) for _ in range(20 if part==1 else 10000): for k,m in enumerate(M): for i in m[0]: m[5] += 1 ni = eval(m[1].replace("old", str(i))) % D if part == 1: ni = ni // 3 nm = m[4] if ni % m[2] else m[3] M[nm][0].append(ni) m[0] = [] L=list(m[5] for m in M) print(L) LS=list(sorted(L)) print(LS[-1]*LS[-2])