import sys P = [ tuple(map(eval, p.split("\n"))) for p in sys.stdin.read().split("\n\n") ] def C(l,r): T = (type(l),type(r)) if T == (int,int): if lr # 0 or 1 elif T == (int,list): return C([l],r) elif T == (list,int): return C(l,[r]) else: # list,list for q in zip(l,r): c = C(*q) if c: return c return C(len(l),len(r)) # part 1 S = 0 for i,p in enumerate(P): if C(*p) <= 0: S += i+1 print(S) # part 2 from functools import cmp_to_key Q = [ q for (l,r) in P for q in [l,r] ] Q.append([[2]]) Q.append([[6]]) Q.sort(key=cmp_to_key(C)) print((Q.index([[2]])+1)*(Q.index([[6]])+1))