def expo_search(ternary_search_function):
    """
    Find how much iterations does it take for `ternary_search`
    to find the answer.
    Iteratively increase the `max_iterations` parameter by a factor of 2
    until we find the ternary search starts to give a valid answer.
    After that, just binary search within that range.
    """
    num_iterations_lower_bound = 1
    num_iterations_upper_bound = num_iterations_lower_bound
    while True:
        search_result = ternary_search_function(
            max_iterations=num_iterations_upper_bound
        )
        if search_result != -1:
            break
        num_iterations_upper_bound *= 2
    # We can increment the lower bound here as well!
    num_iterations_lower_bound = num_iterations_upper_bound // 2
    # Now just do binary search
    while num_iterations_upper_bound >= num_iterations_lower_bound:
        mid = (num_iterations_lower_bound + num_iterations_upper_bound) // 2
        evalu = ternary_search_function(max_iterations=mid)
        if evalu == -1:
            num_iterations_lower_bound = mid + 1
        else:
            num_iterations_upper_bound = mid - 1
    return num_iterations_lower_bound