indexes = [0] * len(aa)
maxval = max(a[0] for a in aa)
best = None
for minval in allnums:
    if best is None or best[0] > maxval - minval:
        best = maxval - minval, minval, maxval
for i in ntoi[minval]:
    indexes[i] += 1
if indexes[i] >= len(aa[i]):
    return [min(x for x in a if x >= best[1]) for a in aa]
maxval = max(maxval, aa[i][indexes[i]])
aa = [[0, 4, 9], [2, 6, 11], [3, 8, 13], [7, 12]]
print(minspan(aa))
Finding N closest numbers