import sys
from collections import deque
def main():
sys.setrecursionlimit(1 << 25)
N, M = map(int, sys.stdin.readline().split())
edges = []
adj = [[] for _ in range(N+1)]
for _ in range(M):
a, b = map(int, sys.stdin.readline().split())
edges.append((a, b))
adj[a].append(b)
# Function to check if a graph is a DAG using Kahn's algorithm
def is_dag(adj_list):
in_degree = [0] * (N + 1)
for u in range(1, N+1):
for v in adj_list[u]:
in_degree[v] += 1
q = deque()
for i in range(1, N+1):
if in_degree[i] == 0:
q.append(i)
cnt = 0
while q:
u = q.popleft()
cnt += 1
for v in adj_list[u]:
in_degree[v] -= 1
if in_degree[v] == 0:
q.append(v)
return cnt == N
# Check if the original graph is a DAG
if is_dag(adj):
print("YES" if M >= 1 else "NO")
return
# Function to find a cycle and its edges using DFS
def find_cycle_edge():
visited = [False] * (N + 1)
stack = []
parent = {}
edge_stack = []
cycle_edges = None
def dfs(u):
nonlocal cycle_edges
visited[u] = True
stack.append(u)
for v in adj[u]:
if not visited[v]:
parent[v] = u
edge_stack.append((u, v))
if dfs(v):
return True
edge_stack.pop()
elif v in stack:
# Found a cycle
idx = stack.index(v)
cycle_nodes = stack[idx:]
cycle_nodes.append(v) # To complete the cycle
cycle_edges = []
for i in range(len(cycle_nodes)-1):
cycle_edges.append((cycle_nodes[i], cycle_nodes[i+1]))
# Also check if there's an edge from last to first
# Assuming the cycle is continuous
return True
stack.pop()
return False
for u in range(1, N+1):
if not visited[u]:
if dfs(u):
break
return cycle_edges
cycle_edges = find_cycle_edge()
if not cycle_edges:
print("NO")
return
# Check each edge in the cycle
for (u, v) in cycle_edges:
# Create adj without this edge
new_adj = [[] for _ in range(N+1)]
for a, b in edges:
if (a == u and b == v):
continue
new_adj[a].append(b)
if is_dag(new_adj):
# Check if there's a path from u to v in new_adj
visited = [False] * (N+1)
q = deque([u])
visited[u] = True
found = False
while q:
current = q.popleft()
if current == v:
found = True
break
for neighbor in new_adj[current]:
if not visited[neighbor]:
visited[neighbor] = True
q.append(neighbor)
if not found:
print("YES")
return
print("NO")
if __name__ == "__main__":
main()