import sys en=enumerate L = open(0).read().split('\n\n') L = [l.split(':') for l in L] L = [l[1].strip().split('\n') for l in L] L = [list(map(lambda x: tuple(map(int,x.split())),l)) for l in L] S = list(v for s in L[0] for v in s) for i,l in en(L[1:]): print(i, l) # map all seads R = [] for s in S: ns = None for dst, src, rg in l: if src <= s < src+rg: ns = dst+(s-src) break R.append(ns if ns else s) S = R print(S) print(min(S))