N,M = map(int,input().split()) G = [[] for _ in range(N+1)] rG = [[] for _ in range(N+1)] edge = [] for _ in range(M): a,b,c = map(int,input().split()) G[a].append(b) rG[b].append(a) if c == 1: G[b].append(a) rG[a].append(b) edge.append((a,b,c)) order = [] memo = [0] * (N + 1) import sys sys.setrecursionlimit(10 ** 8) """ def dfs(now): memo[now] = 1 for v in G[now]: if memo[v] == 0: dfs(v) order.append(now) for i in range(1,N+1): if memo[i] == 0: dfs(i) """ def dfs(now): stack = [now] memo[now] = 1 while stack: i = stack.pop() if memo[i] == 1: memo[i] = 2 stack.append(i) for v in G[i]: if memo[v] == 0: memo[v] = 1 stack.append(v) else: order.append(i) for i in range(1,N+1): if memo[i] == 0: dfs(i) l = [] memo = [0] * (N + 1) """ def dfs2(now,s): memo[i] = 1 s.append(i) for v in rG[now]: if memo[v] == 0: dfs2(v,s) for i in order[::-1]: if memo[i] == 0: l.append([]) dfs2(i,l[-1]) """ def dfs2(now,s): memo[now] = 1 stack = [now] while stack: i = stack.pop() s.append(i) for v in rG[i]: if memo[v] == 0: memo[v] = 1 stack.append(v) for i in order[::-1]: if memo[i] == 0: l.append([]) dfs2(i,l[-1]) parent = list(range(N+1)) def find(i): if parent[i] == i:return i parent[i] = find(parent[i]) return parent[i] def unite(i,j): I = find(i) J = find(j) if I == J:return False parent[I] = J parent[i] = J return True def isSame(i,j): return find(i) == find(j) from collections import defaultdict d = defaultdict(int) for s in l: v = s[0] for u in s: unite(v,u) for a,b,c in edge: if isSame(a,b): d[find(a)] += 1 for s in l: if len(s) <= d[find(s[0])]: print('Yes') exit() print('No')