from functools import reduce


def solve(k, lim):
    """Find the <lim>th term of a Van Eck's sequence with initial values
    <k>.  """

    seq = [*k]
    nums = reduce(lambda a, b: a | b, [{n: i} for i,n in enumerate(seq)])

    for i in range(len(k) - 1, lim):
        res = nums.get(seq[i], i)

        seq.append(i-res)
        nums[seq[i]] = i

    return seq[lim - 1]