Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(1000000)
n,m=map(int,input().split())
graph=[[] for i in range(3*n)]
for i in range(m):
    a,e=map(int,input().split())
    graph[a-1].append(e+n-1)
    graph[a+n-1].append(e+2*n-1)
    graph[a+2*n-1].append(e-1)
s,t=map(int,input().split())
s-=1;t-=1
visited=[False]*(3*n)
dfs_alpha_memory = [3*n]*(3*n)
dfs_skipped = [set() for _ in range(3*n)]
dfs_whether_precise = [False]*(3*n)

def dfs(checking, alpha=3*n):
    if dfs_alpha_memory[checking] < alpha:
        return dfs_alpha_memory[checking]
    elif dfs_whether_precise[checking]:
        moves = dfs_skipped[checking]
    else:
        moves = graph[checking]
    pr_flag = True
    looped = set()
    visited[checking]=True
    initial_alpha = alpha
    for next in moves:
        if t==next:
            alpha=1
            looped = set()
            dfs_whether_precise[checking] = True
            break
        if visited[next]:
            looped.add(next)
            continue
        alpha=min(alpha, dfs(next, alpha-1)+1)
        pr_flag &= dfs_whether_precise[next]
    else:
        if alpha >= initial_alpha:
            alpha = 3*n
    dfs_skipped[checking] &= looped
    dfs_alpha_memory[checking] = min(alpha, dfs_alpha_memory[checking])
    dfs_whether_precise[checking] &= pr_flag
    visited[checking] = False
    return alpha

ans = dfs(s)//3
if ans >= n:
    print(-1)
else:
    print(ans)

Fixed submission:
import sys
input = sys.stdin.readline
inpl = lambda: list(map(int,input().split()))

N, M = inpl()
uv = [ set() for _ in range(3*N) ]
vu = [ set() for _ in range(3*N) ]
for _ in range(M):
    u, v = inpl()
    for k in range(3):
        uv[u-1 + k*N].add(v-1 + ((k+1)%3)*N)
        vu[v-1 + ((k+1)%3)*N].add(u-1 + k*N)
S, T = inpl()
S -= 1
T -= 1

cur = set({S})
for i in range(3*N):
    if i % 3 == 0 and T in cur:
        print(i//3)
        break
    elif len(cur) == 0:
        print(-1)
        break
    prev = cur
    cur = set()
    for u in prev:
        for fr in vu[u]:
            uv[fr].remove(u)
        cur |= uv[u]
else:
    print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(1000000)
n,m=map(int,input().split())
graph=[[] for i in range(3*n)]
for i in range(m):
    a,e=map(int,input().split())
    graph[a-1].append(e+n-1)
    graph[a+n-1].append(e+2*n-1)
    graph[a+2*n-1].append(e-1)
s,t=map(int,input().split())
s-=1;t-=1
visited=[False]*(3*n)
dfs_alpha_memory = [3*n]*(3*n)
dfs_skipped = [set() for _ in range(3*n)]
dfs_whether_precise = [False]*(3*n)

def dfs(checking, alpha=3*n):
    if dfs_alpha_memory[checking] < alpha:
        return dfs_alpha_memory[checking]
    elif dfs_whether_precise[checking]:
        moves = dfs_skipped[checking]
    else:
        moves = graph[checking]
    pr_flag = True
    looped = set()
    visited[checking]=True
    initial_alpha = alpha
    for next in moves:
        if t==next:
            alpha=1
            looped = set()
            dfs_whether_precise[checking] = True
            break
        if visited[next]:
            looped.add(next)
            continue
        alpha=min(alpha, dfs(next, alpha-1)+1)
        pr_flag &= dfs_whether_precise[next]
    else:
        if alpha >= initial_alpha:
            alpha = 3*n
    dfs_skipped[checking] &= looped
    dfs_alpha_memory[checking] = min(alpha, dfs_alpha_memory[checking])
    dfs_whether_precise[checking] &= pr_flag
    visited[checking] = False
    return alpha

ans = dfs(s)//3
if ans >= n:
    print(-1)
else:
    print(ans)

Fixed submission:
import sys
input = sys.stdin.readline
inpl = lambda: list(map(int,input().split()))

N, M = inpl()
uv = [ set() for _ in range(3*N) ]
vu = [ set() for _ in range(3*N) ]
for _ in range(M):
    u, v = inpl()
    for k in range(3):
        uv[u-1 + k*N].add(v-1 + ((k+1)%3)*N)
        vu[v-1 + ((k+1)%3)*N].add(u-1 + k*N)
S, T = inpl()
S -= 1
T -= 1

cur = set({S})
for i in range(3*N):
    if i % 3 == 0 and T in cur:
        print(i//3)
        break
    elif len(cur) == 0:
        print(-1)
        break
    prev = cur
    cur = set()
    for u in prev:
        for fr in vu[u]:
            uv[fr].remove(u)
        cur |= uv[u]
else:
    print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(1000000)
n,m=map(int,input().split())
graph=[[] for i in range(3*n)]
for i in range(m):
    a,e=map(int,input().split())
    graph[a-1].append(e+n-1)
    graph[a+n-1].append(e+2*n-1)
    graph[a+2*n-1].append(e-1)
s,t=map(int,input().split())
s-=1;t-=1
visited=[False]*(3*n)
dfs_alpha_memory = [3*n]*(3*n)
skipped = [set() for _ in range(3*n) ]

def dfs(checking, alpha=3*n):
    if dfs_alpha_memory[checking] < alpha:
        return dfs_alpha_memory[checking]
    looped = set()
    visited[checking]=True
    initial_alpha = alpha
    for next in graph[checking]:
        if t==next:
            alpha=1
            break
        if visited[next]:
            looped.add(next)
            continue
        alpha=min(alpha, dfs(next, alpha-1)+1)
    else:
        if alpha >= initial_alpha:
            alpha = 3*n
    skipped[checking] &= looped
    dfs_alpha_memory[checking] = min(alpha, dfs_alpha_memory[checking])
    visited[checking] = False
    return alpha

ans = dfs(s)//3
if ans >= n:
    print(-1)
else:
    print(ans)

Fixed submission:
import sys
input = sys.stdin.readline
inpl = lambda: list(map(int,input().split()))

N, M = inpl()
uv = [ set() for _ in range(3*N) ]
vu = [ set() for _ in range(3*N) ]
for _ in range(M):
    u, v = inpl()
    for k in range(3):
        uv[u-1 + k*N].add(v-1 + ((k+1)%3)*N)
        vu[v-1 + ((k+1)%3)*N].add(u-1 + k*N)
S, T = inpl()
S -= 1
T -= 1

cur = set({S})
for i in range(3*N):
    if i % 3 == 0 and T in cur:
        print(i//3)
        break
    elif len(cur) == 0:
        print(-1)
        break
    prev = cur
    cur = set()
    for u in prev:
        for fr in vu[u]:
            uv[fr].remove(u)
        cur |= uv[u]
else:
    print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(1000000)
n,m=map(int,input().split())
graph=[[] for i in range(3*n)]
for i in range(m):
    a,e=map(int,input().split())
    graph[a-1].append(e+n-1)
    graph[a+n-1].append(e+2*n-1)
    graph[a+2*n-1].append(e-1)
s,t=map(int,input().split())
s-=1;t-=1
visited=[False]*(3*n)
dfs_alpha_memory = [3*n]*(3*n)
skipped = [set() for _ in range(3*n) ]

def dfs(checking, alpha=3*n):
    if dfs_alpha_memory[checking] < alpha:
        return dfs_alpha_memory[checking]
    looped = set()
    visited[checking]=True
    initial_alpha = alpha
    for next in graph[checking]:
        if t==next:
            alpha=1
            break
        if visited[next]:
            looped.add(next)
            continue
        alpha=min(alpha, dfs(next, alpha-1)+1)
    else:
        if alpha >= initial_alpha:
            alpha = 3*n
    skipped[checking] &= looped
    dfs_alpha_memory[checking] = min(alpha, dfs_alpha_memory[checking])
    visited[checking] = False
    return alpha

ans = dfs(s)//3
if ans >= n:
    print(-1)
else:
    print(ans)

Fixed submission:
import sys
input = sys.stdin.readline
inpl = lambda: list(map(int,input().split()))

N, M = inpl()
uv = [ set() for _ in range(3*N) ]
vu = [ set() for _ in range(3*N) ]
for _ in range(M):
    u, v = inpl()
    for k in range(3):
        uv[u-1 + k*N].add(v-1 + ((k+1)%3)*N)
        vu[v-1 + ((k+1)%3)*N].add(u-1 + k*N)
S, T = inpl()
S -= 1
T -= 1

cur = set({S})
for i in range(3*N):
    if i % 3 == 0 and T in cur:
        print(i//3)
        break
    elif len(cur) == 0:
        print(-1)
        break
    prev = cur
    cur = set()
    for u in prev:
        for fr in vu[u]:
            uv[fr].remove(u)
        cur |= uv[u]
else:
    print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(1000000)
n,m=map(int,input().split())
graph=[[] for i in range(3*n)]
for i in range(m):
    a,e=map(int,input().split())
    graph[a-1].append(e+n-1)
    graph[a+n-1].append(e+2*n-1)
    graph[a+2*n-1].append(e-1)
s,t=map(int,input().split())
s-=1;t-=1
visited=[False]*(3*n)

def dfs(checking,length=0):
    visited[checking]=True
    ret=3*n
    for next in graph[checking]:
        if t==next:
            ret=length+1
            break
        if visited[next]:
            continue
        ret=min(ret,dfs(next,length+1))
    visited[checking]=False
    return ret

ans = dfs(s,0)//3
if ans >= n:
    print(-1)
else:
    print(ans)

Fixed submission:
import sys
input = sys.stdin.readline
inpl = lambda: list(map(int,input().split()))

N, M = inpl()
uv = [ set() for _ in range(3*N) ]
vu = [ set() for _ in range(3*N) ]
for _ in range(M):
    u, v = inpl()
    for k in range(3):
        uv[u-1 + k*N].add(v-1 + ((k+1)%3)*N)
        vu[v-1 + ((k+1)%3)*N].add(u-1 + k*N)
S, T = inpl()
S -= 1
T -= 1

cur = set({S})
for i in range(3*N):
    if i % 3 == 0 and T in cur:
        print(i//3)
        break
    elif len(cur) == 0:
        print(-1)
        break
    prev = cur
    cur = set()
    for u in prev:
        for fr in vu[u]:
            uv[fr].remove(u)
        cur |= uv[u]
else:
    print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(1000000)
n,m=map(int,input().split())
graph=[[] for i in range(3*n)]
for i in range(m):
    a,e=map(int,input().split())
    graph[a-1].append(e+n-1)
    graph[a+n-1].append(e+2*n-1)
    graph[a+2*n-1].append(e-1)
s,t=map(int,input().split())
s-=1;t-=1
visited=[False]*(3*n)

def dfs(checking,length=0):
    visited[checking]=True
    ret=3*n
    for next in graph[checking]:
        if t==next:
            ret=length+1
            break
        if visited[next]:
            continue
        ret=min(ret,dfs(next,length+1))
    visited[checking]=False
    return ret

ans = dfs(s,0)//3
if ans >= n:
    print(-1)
else:
    print(ans)

Fixed submission:
import sys
input = sys.stdin.readline
inpl = lambda: list(map(int,input().split()))

N, M = inpl()
uv = [ set() for _ in range(3*N) ]
vu = [ set() for _ in range(3*N) ]
for _ in range(M):
    u, v = inpl()
    for k in range(3):
        uv[u-1 + k*N].add(v-1 + ((k+1)%3)*N)
        vu[v-1 + ((k+1)%3)*N].add(u-1 + k*N)
S, T = inpl()
S -= 1
T -= 1

cur = set({S})
for i in range(3*N):
    if i % 3 == 0 and T in cur:
        print(i//3)
        break
    elif len(cur) == 0:
        print(-1)
        break
    prev = cur
    cur = set()
    for u in prev:
        for fr in vu[u]:
            uv[fr].remove(u)
        cur |= uv[u]
else:
    print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
import sys
input = sys.stdin.readline
inpl = lambda: list(map(int,input().split()))

N, M = inpl()
uv = [ set() for _ in range(N) ]
for _ in range(M):
    u, v = inpl()
    uv[u-1].add(v-1)
S, T = inpl()
S -= 1
T -= 1

cur = set({S})
for i in range(3*N-2):
    if i % 3 == 0 and T in cur:
        print(i//3)
        break
    prev = cur
    cur = set()
    for p in prev:
        cur |= uv[p]
else:
    print(-1)

Fixed submission:
import sys
input = sys.stdin.readline
inpl = lambda: list(map(int,input().split()))

N, M = inpl()
uv = [ set() for _ in range(3*N) ]
vu = [ set() for _ in range(3*N) ]
for _ in range(M):
    u, v = inpl()
    for k in range(3):
        uv[u-1 + k*N].add(v-1 + ((k+1)%3)*N)
        vu[v-1 + ((k+1)%3)*N].add(u-1 + k*N)
S, T = inpl()
S -= 1
T -= 1

cur = set({S})
for i in range(3*N):
    if i % 3 == 0 and T in cur:
        print(i//3)
        break
    elif len(cur) == 0:
        print(-1)
        break
    prev = cur
    cur = set()
    for u in prev:
        for fr in vu[u]:
            uv[fr].remove(u)
        cur |= uv[u]
else:
    print(-1)