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

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

#1回ずつ数えて答えを3で割る
N, M = lr()
graph = [[] for _ in range(N+1)] # 1-indexed
for _ in range(M):
    u, v = lr()
    graph[u].append(v)

S, T = lr()
que = deque([(S, 0)])
time_set = [set() for _ in range(N+1)] # 1-indexed
while que:
    q, time = que.popleft()
    if time > M * 3:
        break
    for next in graph[q]:
        if time_set[next] == (time+1) % 3:
            continue
        if next == T and (time+1) % 3 == 0:
            print((time+1)//3); exit()
        que.append((next, time+1))
        time_set[next].add((time+1)%3)

print(-1); exit()
# 52

Fixed submission:
import sys
from heapq import heappop, heappush

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, M = lr()
# 3*N頂点あるとする
edges = [[] for _ in range(3*N + 4)] # 3-indexed
for _ in range(M):
    u, v = lr()
    u *= 3; v *= 3
    edges[u].append(v+1)
    edges[u+1].append(v+2)
    edges[u+2].append(v)

INF = 10 ** 18

def dijkstra(start):
    dist = [INF] * (3*N+4)
    dist[start] = 0
    que = [(0, start)]
    while que:
        d, v = heappop(que)
        if dist[v] < d:
            continue
        for next in edges[v]:
            d1 = d + 1
            if dist[next] > d1:
                dist[next] = d1
                heappush(que, (d1, next))
    return dist

S, T = lr()
dist = dijkstra(S*3)
if dist[T*3] == INF:
    print(-1)
else:
    print(dist[T*3] // 3)

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

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

#1回ずつ数えて答えを3で割る
N, M = lr()
graph = [[] for _ in range(N+1)] # 1-indexed
for _ in range(M):
    u, v = lr()
    graph[u].append(v)

S, T = lr()
que = deque([(S, 0)])
time_set = [set() for _ in range(N+1)] # 1-indexed
while que:
    q, time = que.popleft()
    if time > M * 3:
        break
    for next in graph[q]:
        if time_set[next] == (time+1) % 3:
            continue
        if next == T and (time+1) % 3 == 0:
            print((time+1)//3); exit()
        que.append((next, time+1))
        time_set[next].add((time+1)%3)

print(-1); exit()
# 52

Fixed submission:
import sys
from heapq import heappop, heappush

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, M = lr()
# 3*N頂点あるとする
edges = [[] for _ in range(3*N + 4)] # 3-indexed
for _ in range(M):
    u, v = lr()
    u *= 3; v *= 3
    edges[u].append(v+1)
    edges[u+1].append(v+2)
    edges[u+2].append(v)

INF = 10 ** 18

def dijkstra(start):
    dist = [INF] * (3*N+4)
    dist[start] = 0
    que = [(0, start)]
    while que:
        d, v = heappop(que)
        if dist[v] < d:
            continue
        for next in edges[v]:
            d1 = d + 1
            if dist[next] > d1:
                dist[next] = d1
                heappush(que, (d1, next))
    return dist

S, T = lr()
dist = dijkstra(S*3)
if dist[T*3] == INF:
    print(-1)
else:
    print(dist[T*3] // 3)