import sys input = sys.stdin.readline sys.setrecursionlimit(10**7) class DirectedGraph(): def __init__(self, N): self.N = N self.G = [[] for i in range(N)] self.rG = [[] for i in range(N)] self.order = [] self.used1 = [0] * N self.used2 = [0] * N self.group = [-1] * N self.label = 0 def add_edge(self, u, v): self.G[u].append(v) self.rG[v].append(u) def dfs(self, s): self.used1[s] = 1 for u in self.G[s]: if self.used1[u]: continue self.dfs(u) self.order.append(s) def rdfs(self, s, num): self.group[s] = num self.used2[s] = 1 for u in self.rG[s]: if self.used2[u]: continue self.rdfs(u, num) def scc(self): for i in range(self.N): if self.used1[i]: continue self.dfs(i) for s in reversed(self.order): if self.used2[s]: continue self.rdfs(s, self.label) self.label += 1 return self.label, self.group def construct(self): nG = [set() for _ in range(self.label)] mem = [[] for i in range(self.label)] for s in range(self.N): now = self.group[s] for u in self.G[s]: if now == self.group[u]: continue nG[now].add(self.group[u]) mem[now].append(s) return nG, mem class UnionFind(object): def __init__(self, n=1): self.par = [i for i in range(n)] self.rank = [0 for _ in range(n)] self.size = [1 for _ in range(n)] def find(self, x): if self.par[x] == x: return x else: self.par[x] = self.find(self.par[x]) return self.par[x] def union(self, x, y): x = self.find(x) y = self.find(y) if x != y: if self.rank[x] < self.rank[y]: x, y = y, x if self.rank[x] == self.rank[y]: self.rank[x] += 1 self.par[y] = x self.size[x] += self.size[y] def is_same(self, x, y): return self.find(x) == self.find(y) def get_size(self, x): x = self.find(x) return self.size[x] N, M = map(int, input().split()) G = DirectedGraph(N) A, B, C = [-1] * M, [-1] * M, [-1] * M U = UnionFind(N) for i in range(M): A[i], B[i], C[i] = map(int, input().split()) A[i], B[i] = A[i] - 1, B[i] - 1 if C[i] == 1: if U.is_same(A[i], B[i]): print("Yes") exit() U.union(A[i], B[i]) for i in range(M): if C[i] == 1: continue a, b = U.find(A[i]), U.find(B[i]) if a == b: print("Yes") exit() G.add_edge(a, b) G.scc() nG, mem = G.construct() for m in mem: if len(m) >= 2: print("Yes") exit() print("No")