Buggy python submission (verdict: Time Limit Exceeded):
import sys

def main():
    INF = float("inf")
    input = sys.stdin.readline
    N, M = map(int,input().split())
    G = [[] for k in range(N)]
    for k in range(M):
        u, v = map(int,input().split())
        u -= 1
        v -= 1
        G[u] += [v]
    S, T = map(int,input().split())
    S -= 1
    T -= 1
    D = [[INF,INF,INF] for k in range(N)]
    D[S][0] = 0
    Q = [[S,0]]
    while len(Q) > 0:
        ima = Q[0][0]
        k = Q[0][1]
        Q.pop(0)
        for tsugi in G[ima]:
            if D[tsugi][(k+1)%3] != INF:
                pass
            else:
                D[tsugi][(k+1)%3] = D[ima][k] + 1
                Q.append([tsugi,(k+1)%3])
    if D[T][0] == INF or D[T][0]%3 != 0:
        print(-1)
    else:
        print(D[T][0]//3)

if __name__ == '__main__':
    main()


Fixed submission:
import sys
from collections import deque
def main():
    INF = float("inf")
    input = sys.stdin.readline
    N, M = map(int,input().split())
    G = [[] for k in range(N)]
    for k in range(M):
        u, v = map(int,input().split())
        u -= 1
        v -= 1
        G[u] += [v]
    S, T = map(int,input().split())
    S -= 1
    T -= 1
    D = [[INF,INF,INF] for k in range(N)]
    D[S][0] = 0
    Q = deque([[S,0]])
    while len(Q) > 0:
        t = Q.popleft()
        ima = t[0]
        k = t[1]
        for tsugi in G[ima]:
            if D[tsugi][(k+1)%3] != INF:
                pass
            else:
                D[tsugi][(k+1)%3] = D[ima][k] + 1
                Q.append([tsugi,(k+1)%3])
    if D[T][0] == INF or D[T][0]%3 != 0:
        print(-1)
    else:
        print(D[T][0]//3)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(200000)

N, M = map(int,input().split())
G = [[[],[],[]] for k in range(N)]

for k in range(M):
    u, v = map(int,input().split())
    u -= 1
    v -= 1
    G[u][0] += [v]
    G[u][1] += [v]
    G[u][2] += [v]

S, T = map(int,input().split())
S -= 1
T -= 1
D1 = [[200000,200000,200000] for k in range(N)]
D1[S][0] = 0
def dfs1(now, dist):
    for tsugi in G[now][1]:
        if D1[tsugi][1] > dist + 1:
            D1[tsugi][1] = dist + 1
            dfs2(tsugi, dist+1)
def dfs2(now, dist):
    for tsugi in G[now][2]:
        if D1[tsugi][2] > dist + 1:
            D1[tsugi][2] = dist + 1
            dfs3(tsugi, dist+1)
def dfs3(now, dist):
    for tsugi in G[now][0]:
        if D1[tsugi][0] > dist + 1:
            D1[tsugi][0] = dist + 1
            dfs1(tsugi, dist+1)

dfs1(S,0)

if D1[T][0]%3 == 0:
    print(D1[T][0]//3)
else:
    print(-1)


Fixed submission:
import sys
from collections import deque
def main():
    INF = float("inf")
    input = sys.stdin.readline
    N, M = map(int,input().split())
    G = [[] for k in range(N)]
    for k in range(M):
        u, v = map(int,input().split())
        u -= 1
        v -= 1
        G[u] += [v]
    S, T = map(int,input().split())
    S -= 1
    T -= 1
    D = [[INF,INF,INF] for k in range(N)]
    D[S][0] = 0
    Q = deque([[S,0]])
    while len(Q) > 0:
        t = Q.popleft()
        ima = t[0]
        k = t[1]
        for tsugi in G[ima]:
            if D[tsugi][(k+1)%3] != INF:
                pass
            else:
                D[tsugi][(k+1)%3] = D[ima][k] + 1
                Q.append([tsugi,(k+1)%3])
    if D[T][0] == INF or D[T][0]%3 != 0:
        print(-1)
    else:
        print(D[T][0]//3)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(200000)
def main():
    INF = float("inf")
    input = sys.stdin.readline
    N, M = map(int,input().split())
    G = [[[],[],[]] for k in range(N)]

    for k in range(M):
        u, v = map(int,input().split())
        u -= 1
        v -= 1
        G[u][0] += [v]
        G[u][1] += [v]
        G[u][2] += [v]

    S, T = map(int,input().split())
    S -= 1
    T -= 1
    D1 = [[INF,INF,INF] for k in range(N)]
    D1[S][0] = 0

    def dfs1(now):
        for tsugi in G[now][1]:
            if D1[tsugi][1] > D1[now][0] + 1:
                D1[tsugi][1] = D1[now][0] + 1
                dfs2(tsugi)
    def dfs2(now):
        for tsugi in G[now][2]:
            if D1[tsugi][2] > D1[now][1] + 1:
                D1[tsugi][2] = D1[now][1] + 1
                dfs3(tsugi)
    def dfs3(now):
        for tsugi in G[now][2]:
            if D1[tsugi][0] > D1[now][2] + 1:
                D1[tsugi][0] = D1[now][2] + 1
                dfs1(tsugi)

    dfs1(S)
    if D1[T][0]%3 == 0:
        print(D1[T][0]//3)
    else:
        print(-1)
if __name__ == '__main__':
    main()


Fixed submission:
import sys
from collections import deque
def main():
    INF = float("inf")
    input = sys.stdin.readline
    N, M = map(int,input().split())
    G = [[] for k in range(N)]
    for k in range(M):
        u, v = map(int,input().split())
        u -= 1
        v -= 1
        G[u] += [v]
    S, T = map(int,input().split())
    S -= 1
    T -= 1
    D = [[INF,INF,INF] for k in range(N)]
    D[S][0] = 0
    Q = deque([[S,0]])
    while len(Q) > 0:
        t = Q.popleft()
        ima = t[0]
        k = t[1]
        for tsugi in G[ima]:
            if D[tsugi][(k+1)%3] != INF:
                pass
            else:
                D[tsugi][(k+1)%3] = D[ima][k] + 1
                Q.append([tsugi,(k+1)%3])
    if D[T][0] == INF or D[T][0]%3 != 0:
        print(-1)
    else:
        print(D[T][0]//3)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(200000)
def main():
    input = sys.stdin.readline
    N, M = map(int,input().split())
    G = [[[],[],[]] for k in range(N)]

    for k in range(M):
        u, v = map(int,input().split())
        u -= 1
        v -= 1
        G[u][0] += [v]
        G[u][1] += [v]
        G[u][2] += [v]

    S, T = map(int,input().split())
    S -= 1
    T -= 1
    D1 = [[200000,200000,200000] for k in range(N)]
    D1[S][0] = 0
    def dfs1(now, dist):
        for tsugi in G[now][1]:
            if D1[tsugi][1] > dist + 1:
                D1[tsugi][1] = dist + 1
                dfs2(tsugi, dist+1)
    def dfs2(now, dist):
        for tsugi in G[now][2]:
            if D1[tsugi][2] > dist + 1:
                D1[tsugi][2] = dist + 1
                dfs3(tsugi, dist+1)
    def dfs3(now, dist):
        for tsugi in G[now][0]:
            if D1[tsugi][0] > dist + 1:
                D1[tsugi][0] = dist + 1
                dfs1(tsugi, dist+1)

    dfs1(S,0)

    if D1[T][0]%3 == 0:
        print(D1[T][0]//3)
    else:
        print(-1)
if __name__ == '__main__':
    main()


Fixed submission:
import sys
from collections import deque
def main():
    INF = float("inf")
    input = sys.stdin.readline
    N, M = map(int,input().split())
    G = [[] for k in range(N)]
    for k in range(M):
        u, v = map(int,input().split())
        u -= 1
        v -= 1
        G[u] += [v]
    S, T = map(int,input().split())
    S -= 1
    T -= 1
    D = [[INF,INF,INF] for k in range(N)]
    D[S][0] = 0
    Q = deque([[S,0]])
    while len(Q) > 0:
        t = Q.popleft()
        ima = t[0]
        k = t[1]
        for tsugi in G[ima]:
            if D[tsugi][(k+1)%3] != INF:
                pass
            else:
                D[tsugi][(k+1)%3] = D[ima][k] + 1
                Q.append([tsugi,(k+1)%3])
    if D[T][0] == INF or D[T][0]%3 != 0:
        print(-1)
    else:
        print(D[T][0]//3)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(200000)

N, M = map(int,input().split())
G = [[[],[],[]] for k in range(N)]

for k in range(M):
    u, v = map(int,input().split())
    u -= 1
    v -= 1
    G[u][0] += [v]
    G[u][1] += [v]
    G[u][2] += [v]

S, T = map(int,input().split())
S -= 1
T -= 1
D1 = [[200000,200000,200000] for k in range(N)]
D1[S][0] = 0
def dfs1(now, dist):
    for tsugi in G[now][1]:
        if D1[tsugi][1] > dist + 1:
            D1[tsugi][1] = dist + 1
            dfs2(tsugi, dist+1)
def dfs2(now, dist):
    for tsugi in G[now][2]:
        if D1[tsugi][2] > dist + 1:
            D1[tsugi][2] = dist + 1
            dfs3(tsugi, dist+1)
def dfs3(now, dist):
    for tsugi in G[now][0]:
        if D1[tsugi][0] > dist + 1:
            D1[tsugi][0] = dist + 1
            dfs1(tsugi, dist+1)

dfs1(S,0)

if D1[T][0]%3 == 0:
    print(D1[T][0]//3)
else:
    print(-1)


Fixed submission:
import sys
from collections import deque
def main():
    INF = float("inf")
    input = sys.stdin.readline
    N, M = map(int,input().split())
    G = [[] for k in range(N)]
    for k in range(M):
        u, v = map(int,input().split())
        u -= 1
        v -= 1
        G[u] += [v]
    S, T = map(int,input().split())
    S -= 1
    T -= 1
    D = [[INF,INF,INF] for k in range(N)]
    D[S][0] = 0
    Q = deque([[S,0]])
    while len(Q) > 0:
        t = Q.popleft()
        ima = t[0]
        k = t[1]
        for tsugi in G[ima]:
            if D[tsugi][(k+1)%3] != INF:
                pass
            else:
                D[tsugi][(k+1)%3] = D[ima][k] + 1
                Q.append([tsugi,(k+1)%3])
    if D[T][0] == INF or D[T][0]%3 != 0:
        print(-1)
    else:
        print(D[T][0]//3)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Runtime Error):
N, M = map(int,input().split())
G = [[] for k in range(N)]

for k in range(M):
    u, v = map(int,input().split())
    u -= 1
    v -= 1
    G[u].append(v)
    G[v].append(u)

S, T = map(int,input().split())
S -= 1
T -= 1

D1 = [200000 for k in range(N)]
D1[S] = 0
def dfs1(now, dist):
    for tsugi in G[now]:
        if D1[tsugi] > dist + 1:
            D1[tsugi] = dist + 1
            dfs1(tsugi, dist+1)

D2 = [200000 for k in range(N)]
D2[T] = 0
def dfs2(now, dist):
    for tsugi in G[now]:
        if D2[tsugi] > dist + 1:
            D2[tsugi] = dist + 1
            dfs2(tsugi, dist+1)

dfs1(S,0)
dfs2(T,0)

if D1[T] == 200000:
    print(-1)
    exit(0)
if D1[T]%3 == 0:
    print(D1[T]//3)
elif D1[T]%3 == 1:
    ans = 100000
    for k in range(N):
        if D2[k]%3 == 2:
            ans = min(1+(D1[T]+D2[k])//3,ans)
    if ans == 100000:
        print(-1)
    else:
        print(ans)
elif D1[T]%3 == 2:
    ans = 100000
    for k in range(N):
        if D2[k]%3 == 1:
            ans = min(1+(D1[T]+D2[k])//3,ans)
    if ans == 100000:
        print(-1)
    else:
        print(ans)


Fixed submission:
import sys
from collections import deque
def main():
    INF = float("inf")
    input = sys.stdin.readline
    N, M = map(int,input().split())
    G = [[] for k in range(N)]
    for k in range(M):
        u, v = map(int,input().split())
        u -= 1
        v -= 1
        G[u] += [v]
    S, T = map(int,input().split())
    S -= 1
    T -= 1
    D = [[INF,INF,INF] for k in range(N)]
    D[S][0] = 0
    Q = deque([[S,0]])
    while len(Q) > 0:
        t = Q.popleft()
        ima = t[0]
        k = t[1]
        for tsugi in G[ima]:
            if D[tsugi][(k+1)%3] != INF:
                pass
            else:
                D[tsugi][(k+1)%3] = D[ima][k] + 1
                Q.append([tsugi,(k+1)%3])
    if D[T][0] == INF or D[T][0]%3 != 0:
        print(-1)
    else:
        print(D[T][0]//3)

if __name__ == '__main__':
    main()