import sys input = sys.stdin.buffer.readline sys.setrecursionlimit(10 ** 7) class UF_tree: def __init__(self, n): self.root = [-1] * (n + 1) self.rank = [0] * (n + 1) def find(self, x): if self.root[x] < 0: return x else: self.root[x] = self.find(self.root[x]) return self.root[x] def isSame(self, x, y): return self.find(x) == self.find(y) def unite(self, x, y): x = self.find(x) y = self.find(y) if x == y: return False elif self.rank[x] < self.rank[y]: self.root[y] += self.root[x] self.root[x] = y else: self.root[x] += self.root[y] self.root[y] = x if self.rank[x] == self.rank[y]: self.rank[x] += 1 return True def size(self, x): return -self.root[self.find(x)] def main(): N, M = map(int, input().split()) uf = UF_tree(N) edge = [] for _ in range(M): a, b, c = map(int, input().split()) a -= 1 b -= 1 if c == 1: if not uf.unite(a, b): return True else: edge.append((a, b)) G = [[] for _ in range(N)] for a, b in edge: a = uf.find(a) b = uf.find(b) if a == b: return True G[a].append(b) group = [-1] * N g = 0 for i in range(N): if group[i] != -1: continue group[i] = g que = [i] while que: s = que.pop() for t in G[s]: if group[t] == g: return True if group[t] == -1: group[t] = g que.append(t) g += 1 return False if main(): print("Yes") else: print("No")