def cost(num_of_cities, distance_matrix, tour):
    """
    Given `distance_matrix` and `num_of_cities`, calculates cost of a tour
    """
    cost = 0
    for i in range(num_of_cities):
        cost += distance_matrix[tour[i]][tour[(i+1) % num_of_cities]]
    return cost