import sys
[n, m] = [int(i) for i in input().split(' ')]
roads = []
for line in range(m):
    [u,v,w,p] = [int(i) for i in input().split(' ')]
    roads.append((u,v,w,p))

def prim_algo(n, roads):
    network = []
    added_cities = []
    for road in roads:
        u,v,w,p = road
        if p == 1:
            network.append(road)
            added_cities.append(u)
            added_cities.append(v)
    
    start_city = 1
    added_cities = set(added_cities)
    
    while len(added_cities) < n:
        min_cost = float('inf')
        new_road = None
        for road in roads:
            u,v,w,p = road
            if u in added_cities and v not in added_cities and w < min_cost and p == 0:
                min_cost = w
                new_road = road
        if new_road:
            network.append(new_road)
            added_cities.add(new_road[1])
    return network

result = prim_algo(n, roads)
print(len(result))
result_indexes = []
for i in result:
    result_indexes.append(str(roads.index(i) + 1))
print(' '.join(result_indexes))
    