from itertools import cycle
from multiprocessing.managers import ListProxy

cN, cE = (int(i) for i in input().split())

node_ids = [-1] * cN
list_cycles = []

cID = 0

class Cycle:
    def __init__(self):
        global cID
        self.id = cID
        cID += 1
    id = -1
    rc = 0
    bc = 0
    connections = []
    is_cycle = False

def find_cycle(id):
    for i in range(len(list_cycles)):
        if list_cycles[i].id == id:
            return i

for i in range(cE):
    x1, x2, col = (int(i) for i in input().split())
    if node_ids[x1] == -1 and node_ids[x2] == -1:
        c = Cycle()
        c.connections.extend([x1, x2])
        node_ids[x1] = c.id
        node_ids[x2] = c.id
        #print("new",c.id)
        if col == 1:
            c.rc += 1
        else:
            c.bc += 1
        list_cycles.append(c)
    elif node_ids[x2] == -1:
        c = list_cycles[find_cycle(node_ids[x1])]
        #print("if1",c.id)
        c.connections.append(x2)
        node_ids[x2] = c.id
        if col == 1:
            c.rc += 1
        else:
            c.bc += 1
    elif node_ids[x1] == -1:
        c = list_cycles[find_cycle(node_ids[x2])]
        #print("if2",c.id)
        c.connections.append(x1)
        node_ids[x1] = c.id
        if col == 1:
            c.rc += 1
        else:
            c.bc += 1
    else:
        if node_ids[x1] == node_ids[x2]:
            c = list_cycles[find_cycle(node_ids[x1])]
            #print("if3",c.id)
            c.is_cycle = True
            if col == 1:
                c.rc += 1
            else:
                c.bc += 1
        else:
            c = list_cycles[find_cycle(node_ids[x1])]
            c2 = list_cycles[find_cycle(node_ids[x2])]
            #print("if4",c.id, c2.id)
            for index in c2.connections:
                node_ids[index] = c.id
            c.rc += c2.rc
            c.bc += c2.bc
            if col == 1:
                c.rc += 1
            else:
                c.bc += 1
            c.connections.extend(c2.connections)

def isOK():
    for c in list_cycles:
        if not c.is_cycle:
            continue
        if c.rc != 0 and c.bc != 0:
            if c.rc == 1 or c.bc == 1:
                return False
    return True

if isOK():
    print("YES")
else:
    print("NO")

'''
6 6
0 1 0
0 2 0
1 2 0
3 4 1
4 5 1
5 3 1

6 6
0 1 0
0 2 1
1 2 0
3 4 1
4 5 1
5 3 1


7 7
0 1 0
0 2 0
1 2 0
3 4 0
4 5 1
5 6 1
6 3 1
'''