n, m = [int(x) for x in input().strip().split(" ")]

node_neighbors = [[] for _ in range(n)]
node_col_count = [(None, None) for _ in range(n)]
node_state = [0 for _ in range(n)]

for i in range(m):
    u, v, c = [int(x) for x in input().split(" ")]
    node_neighbors[u].append((v, c))
    node_neighbors[v].append((u, c))

def dfs(v, c1, c2):
    # assert(node_state[v] == 0)
    node_state[v] = 1
    node_col_count[v] = c1, c2

    for n, c in node_neighbors[v]:
        if c == 0:
            c1 += 1
        else:
            c2 += 1

        if (node_state[n] == 1):
            d1 = c1 - node_col_count[n][0]
            d2 = c2 - node_col_count[n][1]

            if d1 == 1 or d2 == 1:
                print("NO")
                exit()

        elif node_state[n] == 0:
            dfs(n, c1, c2)

        if c == 0:
            c1 -= 1
        else:
            c2 -= 1
    
    node_state[v] = 2
            
for v in range(n):
    if node_state[v] == 0:
        dfs(v, 0 , 0)
print("YES")