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

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LMI(): return list(map(int, sys.stdin.readline().split()))
MOD = 10 ** 9 + 7
INF = float('inf')


N, M = MI()
adj = [list() for _ in range(N)]
for _ in range(M):
    u, v = map(lambda s: int(s) - 1, input().split())
    adj[u].append(v)
S, T = map(lambda s: int(s) - 1, input().split())

st = [(0, S, 0)]
seen = {(0, S)}
while len(st) > 0:
    dim, v, cnt = st.pop()
    if dim == 0 and v == T:
        print(cnt // 3)
        exit(0)
    ndim = (dim + 1) % 3
    for nxt in adj[v]:
        if (ndim, nxt) not in seen:
            st.append((ndim, nxt, cnt + 1))
print(-1)

Fixed submission:
import sys
sys.setrecursionlimit(300000)
from collections import deque

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LMI(): return list(map(int, sys.stdin.readline().split()))
MOD = 10 ** 9 + 7
INF = float('inf')


N, M = MI()
adj = [list() for _ in range(N)]
for _ in range(M):
    u, v = map(lambda s: int(s) - 1, sys.stdin.readline().split())
    adj[u].append(v)
S, T = map(lambda s: int(s) - 1, sys.stdin.readline().split())

q = deque([(0, S, 0)])
seen = {S}
while len(q) > 0:
    dim, v, cnt = q.popleft()
    if v == T and dim == 0:
        print(cnt // 3)
        exit(0)
    ndim = (dim + 1) % 3
    for nxt in adj[v]:
        if (ndim * N + nxt) not in seen:
            q.append((ndim, nxt, cnt + 1))
            seen.add(ndim * N + nxt)
print(-1)

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

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LMI(): return list(map(int, sys.stdin.readline().split()))
MOD = 10 ** 9 + 7
INF = float('inf')


N, M = MI()
adj = [list() for _ in range(3 * N)]
for _ in range(M):
    u, v = map(lambda s: int(s) - 1, input().split())
    adj[u].append((v + N) % (3 * N))
    adj[u + N].append((v + 2 * N) % (3 * N))
    adj[u + 2 * N].append((v + 3 * N) % (3 * N))
S, T = map(lambda s: int(s) - 1, input().split())

st = [(S, 0)]
seen = {S}
while len(st) > 0:
    v, cnt = st.pop()
    if v == T:
        print(cnt // 3)
        exit(0)
    for nxt in adj[v]:
        if nxt not in seen:
            st.append((nxt, cnt + 1))
print(-1)

Fixed submission:
import sys
sys.setrecursionlimit(300000)
from collections import deque

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LMI(): return list(map(int, sys.stdin.readline().split()))
MOD = 10 ** 9 + 7
INF = float('inf')


N, M = MI()
adj = [list() for _ in range(N)]
for _ in range(M):
    u, v = map(lambda s: int(s) - 1, sys.stdin.readline().split())
    adj[u].append(v)
S, T = map(lambda s: int(s) - 1, sys.stdin.readline().split())

q = deque([(0, S, 0)])
seen = {S}
while len(q) > 0:
    dim, v, cnt = q.popleft()
    if v == T and dim == 0:
        print(cnt // 3)
        exit(0)
    ndim = (dim + 1) % 3
    for nxt in adj[v]:
        if (ndim * N + nxt) not in seen:
            q.append((ndim, nxt, cnt + 1))
            seen.add(ndim * N + nxt)
print(-1)

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

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LMI(): return list(map(int, sys.stdin.readline().split()))
MOD = 10 ** 9 + 7
INF = float('inf')


N, M = MI()
adj = [list() for _ in range(3 * N)]
for _ in range(M):
    u, v = map(lambda s: int(s) - 1, input().split())
    adj[u].append((v + N) % (3 * N))
    adj[u + N].append((v + 2 * N) % (3 * N))
    adj[u + 2 * N].append((v + 3 * N) % (3 * N))
S, T = map(lambda s: int(s) - 1, input().split())

st = [(S, 0)]
seen = {S}
while len(st) > 0:
    v, cnt = st.pop()
    if v == T:
        print(cnt // 3)
        exit(0)
    for nxt in adj[v]:
        if nxt not in seen:
            st.append((nxt, cnt + 1))
print(-1)

Fixed submission:
import sys
sys.setrecursionlimit(300000)
from collections import deque

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LMI(): return list(map(int, sys.stdin.readline().split()))
MOD = 10 ** 9 + 7
INF = float('inf')


N, M = MI()
adj = [list() for _ in range(N)]
for _ in range(M):
    u, v = map(lambda s: int(s) - 1, sys.stdin.readline().split())
    adj[u].append(v)
S, T = map(lambda s: int(s) - 1, sys.stdin.readline().split())

q = deque([(0, S, 0)])
seen = {S}
while len(q) > 0:
    dim, v, cnt = q.popleft()
    if v == T and dim == 0:
        print(cnt // 3)
        exit(0)
    ndim = (dim + 1) % 3
    for nxt in adj[v]:
        if (ndim * N + nxt) not in seen:
            q.append((ndim, nxt, cnt + 1))
            seen.add(ndim * N + nxt)
print(-1)