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

N, M = map(int, input().split())

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

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

visited = [False] * N
visited[S] = True
queue1 = deque([(0, S)])

while queue1 :
    cost, i = queue1.popleft()
    queue2 = deque([(0, i)])
    
    while queue2 :
        step, q = queue2.popleft()
        if step == 2 : 
            for j in vec[q] :
                if j == T :
                    print(cost + 1)
                    exit()
                if not visited[j] :
                    visited[j] = True
                    queue1.append((cost + 1, j))
        else :
            for j in vec[q] :
                queue2.append((step + 1, j))

print(-1)
    

Fixed submission:
from collections import deque

N, M = map(int, input().split())

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

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

visited = [[False] * N for _ in range(3)]
visited[0][S] = True
queue = deque([(S, 0)])

while queue :
    q, cost = queue.popleft()
    p = (cost + 1) % 3
    for i in vec[q] :
        if p == 0 and i == T :
            print((cost + 1) // 3)
            exit()
        if not visited[p][i] :
            visited[p][i] = True
            queue.append((i, cost + 1))

print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
from heapq import heappush, heappop

N, M = map(int, input().split())
INF = 10 ** 12

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

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

visited = [False] * N
visited[S] = True
queue_ = [(0, S)]

while queue_ :
    cost, i = heappop(queue_)
    
    queue = deque([(0, i)])
    while queue :
        step, q = queue.popleft()
        if step == 2 : 
            for j in vec[q] :
                if j == T :
                    print(cost + 1)
                    exit()
                if not visited[j] :
                    visited[j] = True
                    heappush(queue_, (cost + 1, j))
        else :
            for j in vec[q] :
                queue.append((step + 1, j))

print(-1)
    

Fixed submission:
from collections import deque

N, M = map(int, input().split())

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

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

visited = [[False] * N for _ in range(3)]
visited[0][S] = True
queue = deque([(S, 0)])

while queue :
    q, cost = queue.popleft()
    p = (cost + 1) % 3
    for i in vec[q] :
        if p == 0 and i == T :
            print((cost + 1) // 3)
            exit()
        if not visited[p][i] :
            visited[p][i] = True
            queue.append((i, cost + 1))

print(-1)

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

N, M = map(int, input().split())
INF = 10 ** 12

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

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

vec_ = [[] for _ in range(N)]
visited = [False] * N
visited[S] = True
queue_ = [S]

while queue_ :
    i = queue_.pop()
    queue = deque([(0, i)])
    while queue :
        step, q = queue.popleft()
        if step == 2 : 
            for j in vec[q] :
                vec_[i].append(j)
                if not visited[j] :
                    visited[j] = True
                    queue_.append(j)
        else :
            for j in vec[q] :
                queue.append((step + 1, j))
                if not visited[j] :
                    visited[j] = True
                    queue_.append(j)
      
visited = [False] * N
visited[S] = True
queue = deque([(0, S)])
while queue :
    step, q = queue.popleft()
    for i in vec_[q] :
        if i == T :
            print(step+1)
            exit()
        
        if not visited[i] :
            queue.append((step + 1, i))
            visited[i] = True

print(-1)
    

Fixed submission:
from collections import deque

N, M = map(int, input().split())

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

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

visited = [[False] * N for _ in range(3)]
visited[0][S] = True
queue = deque([(S, 0)])

while queue :
    q, cost = queue.popleft()
    p = (cost + 1) % 3
    for i in vec[q] :
        if p == 0 and i == T :
            print((cost + 1) // 3)
            exit()
        if not visited[p][i] :
            visited[p][i] = True
            queue.append((i, cost + 1))

print(-1)

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

N, M = map(int, input().split())
INF = 10 ** 12

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

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

vec_ = [[] for _ in range(N)]
for i in range(N) :
    queue = deque([(0, i)])
    while queue :
        step, q = queue.popleft()
        if step == 2 : 
            for j in vec[q] :
                vec_[i].append(j)
        else :
            for j in vec[q] :
                queue.append((step + 1, j))
      
visited = [False] * N
visited[S] = True
queue = deque([(0, S)])
while queue :
    step, q = queue.popleft()
    for i in vec_[q] :
        if i == T :
            print(step+1)
            exit()
        
        if not visited[i] :
            queue.append((step + 1, i))
            visited[i] = True

print(-1)
    

Fixed submission:
from collections import deque

N, M = map(int, input().split())

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

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

visited = [[False] * N for _ in range(3)]
visited[0][S] = True
queue = deque([(S, 0)])

while queue :
    q, cost = queue.popleft()
    p = (cost + 1) % 3
    for i in vec[q] :
        if p == 0 and i == T :
            print((cost + 1) // 3)
            exit()
        if not visited[p][i] :
            visited[p][i] = True
            queue.append((i, cost + 1))

print(-1)

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

N, M = map(int, input().split())
INF = 10 ** 12

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

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

vec_ = [[] for _ in range(N)]
for i in range(N) :
    queue = deque([(0, i)])
    while queue :
        step, q = queue.popleft()
        if step == 3 : 
            vec_[i].append(q)
            continue
            
        for j in vec[q] :
            queue.append((step + 1, j))
      
visited = [False] * N
visited[S] = True
queue = deque([(0, S)])
while queue :
    step, q = queue.popleft()
    if q == T : 
        break
    for i in vec_[q] :
        if not visited[i] :
            queue.append((step + 1, i))
            visited[i] = True

if visited[T] :
    print(step)
else :
    print(-1)
    

Fixed submission:
from collections import deque

N, M = map(int, input().split())

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

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

visited = [[False] * N for _ in range(3)]
visited[0][S] = True
queue = deque([(S, 0)])

while queue :
    q, cost = queue.popleft()
    p = (cost + 1) % 3
    for i in vec[q] :
        if p == 0 and i == T :
            print((cost + 1) // 3)
            exit()
        if not visited[p][i] :
            visited[p][i] = True
            queue.append((i, cost + 1))

print(-1)