Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
graph = [[] for i in range(N)]
for i in range(M):
    u, v = map(int, input().split())
    u, v = u-1, v-1
    graph[u].append(v)
S, T = map(int, input().split())
S, T = S-1, T-1
new1 = [[] for i in range(N)]
new2 = [[] for i in range(N)]
new3 = [[] for i in range(N)]
for i in range(N):
    for v in graph[i]:
        new1[i].append(v)
for i in range(N):
    for v in graph[i]:
        new2[i].extend(new1[v])
for i in range(N):
    for v in graph[i]:
        new3[i].extend(new2[v])
#print(new1)
#print(new2)
#print(new3)



visit = [False for i in range(N)]
length = [0 for i in range(N)]
def bfs(v, T):
    Q = []
    Q.append(v)
    visit[v] = True
    while len(Q) > 0:
        v = Q.pop(0)
        #print(con)
        con = list(set(new3[v]))
        for u in con:
            if not visit[u]:
                visit[u] = True
                length[u] = length[v] + 1
                Q.append(u)
                #print(u, length[u])
            if u == T:
                return()
bfs(S,T)
#print(length)
if visit[T]:
    print(length[T])
else:
    print(-1)


Fixed submission:
N, M = map(int, input().split())
graph = [[] for i in range(3 * N)]
for i in range(M):
    u, v = map(int, input().split())
    u, v = u-1, v-1
    graph[u].append(v+N)
    graph[u+N].append(v+2*N)
    graph[u+2*N].append(v)
S, T = map(int,input().split())
S, T = S-1, T-1
visit = [False for i in range(3 * N)]
length = [0 for i in range(3 * N)]
def bfs(v):
    Q = []
    Q.append(v)
    visit[v] = True
    while Q:
        v = Q.pop(0)
        for u in graph[v]:
            if not visit[u]:
                visit[u] = True
                length[u] = length[v] + 1
                Q.append(u)
                #print(u, length[u])
bfs(S)
#print(length)
if visit[T]:
    print(int(length[T] //3))
else:
    print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
graph = [[] for i in range(N)]
for i in range(M):
    u, v = map(int, input().split())
    u, v = u-1, v-1
    graph[u].append(v)
S, T = map(int, input().split())
S, T = S-1, T-1
def bfs3(v, n, ans):
    if n == 0:
        ans.append(v)
    else:
        for u in graph[v]:
            bfs3(u, n-1,ans)

visit = [False for i in range(N)]
length = [0 for i in range(N)]
def bfs(v, T):
    Q = []
    Q.append(v)
    visit[v] = True
    while len(Q) > 0:
        v = Q.pop(0)
        ans = []
        bfs3(v, 3, ans)
        con = list(set(ans))
        #print(con)
        for u in con:
            if not visit[u]:
                visit[u] = True
                length[u] = length[v] + 1
                Q.append(u)
                #print(u, length[u])
            if u == T:
                return()
bfs(S,T)
#print(length)
if visit[T]:
    print(length[T])
else:
    print(-1)


Fixed submission:
N, M = map(int, input().split())
graph = [[] for i in range(3 * N)]
for i in range(M):
    u, v = map(int, input().split())
    u, v = u-1, v-1
    graph[u].append(v+N)
    graph[u+N].append(v+2*N)
    graph[u+2*N].append(v)
S, T = map(int,input().split())
S, T = S-1, T-1
visit = [False for i in range(3 * N)]
length = [0 for i in range(3 * N)]
def bfs(v):
    Q = []
    Q.append(v)
    visit[v] = True
    while Q:
        v = Q.pop(0)
        for u in graph[v]:
            if not visit[u]:
                visit[u] = True
                length[u] = length[v] + 1
                Q.append(u)
                #print(u, length[u])
bfs(S)
#print(length)
if visit[T]:
    print(int(length[T] //3))
else:
    print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
graph = [[] for i in range(N)]
for i in range(M):
    u, v = map(int, input().split())
    u, v = u-1, v-1
    graph[u].append(v)
S, T = map(int, input().split())
S, T = S-1, T-1
def bfs3(v, n, ans):
    if n == 0:
        ans.append(v)
    else:
        for u in graph[v]:
            bfs3(u, n-1,ans)

visit = [False for i in range(N)]
length = [0 for i in range(N)]
def bfs(v):
    Q = []
    Q.append(v)
    visit[v] = True
    while len(Q) > 0:
        v = Q.pop(0)
        ans = []
        bfs3(v, 3, ans)
        con = list(set(ans))
        #print(con)
        for u in con:
            if not visit[u]:
                visit[u] = True
                length[u] = length[v] + 1
                Q.append(u)
                #print(u, length[u])
bfs(S)
#print(length)
if visit[T]:
    print(length[T])
else:
    print(-1)


Fixed submission:
N, M = map(int, input().split())
graph = [[] for i in range(3 * N)]
for i in range(M):
    u, v = map(int, input().split())
    u, v = u-1, v-1
    graph[u].append(v+N)
    graph[u+N].append(v+2*N)
    graph[u+2*N].append(v)
S, T = map(int,input().split())
S, T = S-1, T-1
visit = [False for i in range(3 * N)]
length = [0 for i in range(3 * N)]
def bfs(v):
    Q = []
    Q.append(v)
    visit[v] = True
    while Q:
        v = Q.pop(0)
        for u in graph[v]:
            if not visit[u]:
                visit[u] = True
                length[u] = length[v] + 1
                Q.append(u)
                #print(u, length[u])
bfs(S)
#print(length)
if visit[T]:
    print(int(length[T] //3))
else:
    print(-1)