from collections import deque n, m=map(int, input().split()) par=[i for i in range(n)] sz=[1]*n def find(x): if x==par[x]: return x par[x]=find(par[x]) return par[x] def unite(x, y): x=find(x) y=find(y) if x==y: return False if sz[x]>sz[y]: x, y=y, x sz[y]+=sz[x] par[x]=y return True e=[] for _ in range(m): a, b, c=map(int, input().split()) a-=1 b-=1 if c==1: if not unite(a, b): print("Yes") exit() else: e.append((a, b)) g=[[] for i in range(n)] deg=[0]*n for p in e: x=find(p[0]) y=find(p[1]) g[x].append(y) deg[y]+=1 que=deque() for x in range(n): if deg[x]==0: que.appendleft(x) while len(que)!=0: x=que.popleft() for y in g[x]: deg[y]-=1 if deg[y]==0: que.appendleft(y) for x in range(n): if deg[x]>0: print("Yes") exit() print("No")