from collections import defaultdict

N, M = map(int, input().split())

red_graph = defaultdict(list)
blue_graph = defaultdict(list)

RED = 0
BLUE = 1

blue_edges = []
red_edges = []

for _ in range(M):
    a, b, color = map(int, input().split())

    if color == RED:
        red_graph[a].append(b)
        red_graph[b].append(a)
        red_edges.append((a,b))
    else:
        blue_graph[a].append(b)
        blue_graph[b].append(a)
        blue_edges.append((a,b))


def find_components(graph):
    vert_components = {v: N + v for v in range(N)}
    count = 0
    visited = set()

    for vertex in list(graph.keys()):
        if vertex in visited:
            continue

        component = set()
        stack = [vertex]

        while stack:
            current = stack.pop()

            if current in visited:
                continue

            visited.add(current)
            component.add(current)

            # print(current, graph[current])

            stack.extend(
                v for v in graph[current] if  v not in visited
            )

        # print(count, component)
        for v in component:
            vert_components[v] = count

        count += 1

    # print(vert_components)
    return vert_components

red_vert_components = find_components(red_graph)

for a, b in blue_edges:
    if red_vert_components[a] == red_vert_components[b]:
        print("NO")
        exit(0)

blue_vert_components = find_components(blue_graph)

for a,b in red_edges:
    if blue_vert_components[a] == blue_vert_components[b]:
        print("NO")
        exit(0)

print("YES")
