def append_step_to_route(route_with_distance, distances, next_step):
    """Append step to route_with_distance and update distance."""
    distance, route = route_with_distance
    return (distance + distances[route[-1]][next_step],
            route + [next_step])