import sys from collections import deque dq = deque() for (i,c) in enumerate(sys.stdin.read()): equals = list(filter(lambda x: c == x[1], enumerate(dq))) if len(equals)>0: # remove left part until a char equal to c, included for _ in range(equals[-1][0]+1): dq.popleft() if len(dq) == 13: # if we have 13+1 distinct chars, we're good print(i+1) break dq.append(c)