import sys input = sys.stdin.readline class UnionFind(object): def __init__(self, n: int): self.nodes = [-1]*n def find(self, x: int) -> int: if self.nodes[x] < 0: return x else: self.nodes[x] = self.find(self.nodes[x]) return self.nodes[x] def unite(self, x: int, y: int) -> bool: root_x, root_y, nodes = self.find(x), self.find(y), self.nodes if root_x != root_y: if nodes[root_x] > nodes[root_y]: root_x, root_y = root_y, root_x nodes[root_x] += nodes[root_y] nodes[root_y] = root_x return root_x != root_y if __name__ == '__main__': N, M = map(int, input().split()) edges = [] uf = UnionFind(N) for u, v, c in (map(int, input().split()) for _ in range(M)): u -= 1 v -= 1 if c == 1: if not uf.unite(u, v): print('Yes') exit() else: edges.append((u, v)) adj = [set() for _ in range(N)] rev = [set() for _ in range(N)] for u, v in edges: par_u, par_v = uf.find(u), uf.find(v) if par_u == par_v: print('Yes') exit() adj[par_u].add(par_v) visited = [0]*N for i in range(N): if visited[i]: continue visited[i] = 1 stack = [i] while stack: v = stack.pop() for dest in adj[v]: if dest == i: print('Yes') exit() if visited[dest]: continue visited[dest] = 1 stack.append(dest) else: print('No')