Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)

N,M=map(int,input().split())
EDGE=[list(map(int,input().split())) for i in range(M)]

#REVEDGE=[[] for i in range(N+1)]
EDGELIST=[[] for i in range(N+1)]
LEAF=[]
for x,y in EDGE:
    #REVEDGE[y].append(x)
    EDGELIST[x].append(y)

for i in range(1,N+1):
    if EDGELIST[i]==[]:
        LEAF.append(i)

DIST=[-1]*(N+1)
for l in LEAF:
    DIST[l]=0

def distance(x):
    if DIST[x]!=-1:
        return DIST[x]
    ANS=0
    for to in EDGELIST[x]:
        if ANS<distance(to)+1:
            ANS=distance(to)+1

    return ANS

for i in range(1,N+1):
    DIST[i]=distance(i)

print(max(DIST))


Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)

N,M=map(int,input().split())
EDGE=[list(map(int,input().split())) for i in range(M)]

#REVEDGE=[[] for i in range(N+1)]
EDGELIST=[[] for i in range(N+1)]
LEAF=[]
for x,y in EDGE:
    #REVEDGE[y].append(x)
    EDGELIST[x].append(y)

for i in range(1,N+1):
    if EDGELIST[i]==[]:
        LEAF.append(i)

DIST=[-1]*(N+1)
for l in LEAF:
    DIST[l]=0

def distance(x):
    if DIST[x]!=-1:
        return DIST[x]
    ANS=0
    for to in EDGELIST[x]:
        if ANS<distance(to)+1:
            ANS=distance(to)+1

    DIST[x]=ANS
    return ANS

for i in range(1,N+1):
    DIST[i]=distance(i)

print(max(DIST))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

N,M=map(int,input().split())
EDGE=[list(map(int,input().split())) for i in range(M)]

REVEDGE=[[] for i in range(N+1)]
EDGELIST=[[] for i in range(N+1)]
LEAF=[]
for x,y in EDGE:
    REVEDGE[y].append(x)
    EDGELIST[x].append(y)

for i in range(1,N+1):
    if EDGELIST[i]==[]:
        LEAF.append(i)

from collections import deque
QUE = deque(LEAF)

DIST=[0]*(N+1)

while QUE:
    x=QUE.pop()

    for to in REVEDGE[x]:
        if DIST[to]<DIST[x]+1:
            DIST[to]=DIST[x]+1
            QUE.appendleft(to)

print(max(DIST))
    




Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)

N,M=map(int,input().split())
EDGE=[list(map(int,input().split())) for i in range(M)]

#REVEDGE=[[] for i in range(N+1)]
EDGELIST=[[] for i in range(N+1)]
LEAF=[]
for x,y in EDGE:
    #REVEDGE[y].append(x)
    EDGELIST[x].append(y)

for i in range(1,N+1):
    if EDGELIST[i]==[]:
        LEAF.append(i)

DIST=[-1]*(N+1)
for l in LEAF:
    DIST[l]=0

def distance(x):
    if DIST[x]!=-1:
        return DIST[x]
    ANS=0
    for to in EDGELIST[x]:
        if ANS<distance(to)+1:
            ANS=distance(to)+1

    DIST[x]=ANS
    return ANS

for i in range(1,N+1):
    DIST[i]=distance(i)

print(max(DIST))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

N,M=map(int,input().split())
EDGE=[list(map(int,input().split())) for i in range(M)]

REVEDGE=[[] for i in range(N+1)]
EDGELIST=[[] for i in range(N+1)]
LEAF=[]
for x,y in EDGE:
    REVEDGE[y].append(x)
    EDGELIST[x].append(y)

for i in range(1,N+1):
    if EDGELIST[i]==[]:
        LEAF.append(i)

from collections import deque
QUE = deque(LEAF)

DIST=[0]*(N+1)

while QUE:
    x=QUE.pop()

    for to in REVEDGE[x]:
        if DIST[to]<DIST[x]+1:
            DIST[to]=DIST[x]+1
            QUE.append(to)

print(max(DIST))
    




Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)

N,M=map(int,input().split())
EDGE=[list(map(int,input().split())) for i in range(M)]

#REVEDGE=[[] for i in range(N+1)]
EDGELIST=[[] for i in range(N+1)]
LEAF=[]
for x,y in EDGE:
    #REVEDGE[y].append(x)
    EDGELIST[x].append(y)

for i in range(1,N+1):
    if EDGELIST[i]==[]:
        LEAF.append(i)

DIST=[-1]*(N+1)
for l in LEAF:
    DIST[l]=0

def distance(x):
    if DIST[x]!=-1:
        return DIST[x]
    ANS=0
    for to in EDGELIST[x]:
        if ANS<distance(to)+1:
            ANS=distance(to)+1

    DIST[x]=ANS
    return ANS

for i in range(1,N+1):
    DIST[i]=distance(i)

print(max(DIST))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

N,M=map(int,input().split())
EDGE=[list(map(int,input().split())) for i in range(M)]

REVEDGE=[[] for i in range(N+1)]

LEAF=[]
for x,y in EDGE:
    REVEDGE[y].append(x)
    LEAF.append(x)

LEAF=list(set(range(1,N+1)) - set(LEAF))

from collections import deque
QUE = deque(LEAF)

DIST=[0]*(N+1)

while QUE:
    x=QUE.pop()

    for to in REVEDGE[x]:
        if DIST[to]<DIST[x]+1:
            DIST[to]=DIST[x]+1
            QUE.append(to)

print(max(DIST))
    

Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)

N,M=map(int,input().split())
EDGE=[list(map(int,input().split())) for i in range(M)]

#REVEDGE=[[] for i in range(N+1)]
EDGELIST=[[] for i in range(N+1)]
LEAF=[]
for x,y in EDGE:
    #REVEDGE[y].append(x)
    EDGELIST[x].append(y)

for i in range(1,N+1):
    if EDGELIST[i]==[]:
        LEAF.append(i)

DIST=[-1]*(N+1)
for l in LEAF:
    DIST[l]=0

def distance(x):
    if DIST[x]!=-1:
        return DIST[x]
    ANS=0
    for to in EDGELIST[x]:
        if ANS<distance(to)+1:
            ANS=distance(to)+1

    DIST[x]=ANS
    return ANS

for i in range(1,N+1):
    DIST[i]=distance(i)

print(max(DIST))