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

n, m = map(int, input().split())
uv  =[[] for _ in range(n)]
for _ in range(m):
    u, v = map(int, input().split())
    uv[u - 1].append(v - 1)
s, t = map(int, input().split())
s -= 1
t -= 1
uv2 = [[] for _ in range(n)]
uv3 = [[] for _ in range(n)]

for i, lst in enumerate(uv):
    for j in lst:
        lst2 = uv[j]
        for k in lst2:
            uv2[i].append(k)
            
for i, lst in enumerate(uv):
    for j in lst:
        lst2 = uv2[j]
        for k in lst2:
            uv3[i].append(k)

queue = deque()
queue.append(s)
queue.append(1)
check = [False for _ in range(n)]
check[s] = True

while queue:
    pos = queue.popleft()
    ans = queue.popleft()
    for next_pos in uv3[pos]:
        if next_pos == t:
            print(ans)
            exit()
        if check[next_pos]:
            continue
        check[next_pos] = True
        queue.append(next_pos)
        queue.append(ans + 1)
print(-1)
    

Fixed submission:
from collections import deque

n, m = map(int, input().split())
uv  =[[] for _ in range(n)]
for _ in range(m):
    u, v = map(int, input().split())
    uv[u - 1].append(v - 1)
s, t = map(int, input().split())
s -= 1
t -= 1

queue = deque()
queue.append(s)
queue.append(1)
queue.append(0)
check = [[False for _ in range(3)] for _ in range(n)]
check[s][0] = True

while queue:
    pos = queue.popleft()
    ans = queue.popleft()
    kkp = queue.popleft()
    for next_pos in uv[pos]:
        if next_pos == t and kkp == 2:
            print(ans // 3)
            exit()
        if check[next_pos][(kkp + 1) % 3]:
            continue
        check[next_pos][(kkp + 1) % 3] = True
        queue.append(next_pos)
        queue.append(ans + 1)
        queue.append((kkp + 1) % 3)
print(-1)

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

n, m = map(int, input().split())
uv  =[[] for _ in range(n)]
for _ in range(m):
    u, v = map(int, input().split())
    uv[u - 1].append(v - 1)
s, t = map(int, input().split())
s -= 1
t -= 1
uv3 = [[] for _ in range(n)]

for i, lst in enumerate(uv):
    for j in lst:
        lst2 = uv[j]
        for k in lst2:
            lst3 = uv[k]
            for l in lst3:
                uv3[i].append(l)
queue = deque()
queue.append(s)
queue.append(1)
check = [False for _ in range(n)]
check[s] = True

while queue:
    pos = queue.popleft()
    ans = queue.popleft()
    for next_pos in uv3[pos]:
        if next_pos == t:
            print(ans)
            exit()
        if check[next_pos]:
            continue
        check[next_pos] = True
        queue.append(next_pos)
        queue.append(ans + 1)
print(-1)
    

Fixed submission:
from collections import deque

n, m = map(int, input().split())
uv  =[[] for _ in range(n)]
for _ in range(m):
    u, v = map(int, input().split())
    uv[u - 1].append(v - 1)
s, t = map(int, input().split())
s -= 1
t -= 1

queue = deque()
queue.append(s)
queue.append(1)
queue.append(0)
check = [[False for _ in range(3)] for _ in range(n)]
check[s][0] = True

while queue:
    pos = queue.popleft()
    ans = queue.popleft()
    kkp = queue.popleft()
    for next_pos in uv[pos]:
        if next_pos == t and kkp == 2:
            print(ans // 3)
            exit()
        if check[next_pos][(kkp + 1) % 3]:
            continue
        check[next_pos][(kkp + 1) % 3] = True
        queue.append(next_pos)
        queue.append(ans + 1)
        queue.append((kkp + 1) % 3)
print(-1)

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

n, m = map(int, input().split())
uv  =[[] for _ in range(n)]
for _ in range(m):
    u, v = map(int, input().split())
    uv[u - 1].append(v - 1)
s, t = map(int, input().split())
s -= 1
t -= 1
uv3 = [[] for _ in range(n)]

for i, lst in enumerate(uv):
    for j in lst:
        lst2 = uv[j]
        for k in lst2:
            lst3 = uv[k]
            for l in lst3:
                uv3[i].append(l)
queue = deque()
queue.append(s)
queue.append(1)
check = [False for _ in range(n)]
check[s] = True

while queue:
    pos = queue.popleft()
    ans = queue.popleft()
    for next_pos in uv3[pos]:
        if next_pos == t:
            print(ans)
            exit()
        if check[next_pos]:
            continue
        check[next_pos] = True
        queue.append(next_pos)
        queue.append(ans + 1)
print(-1)
    

Fixed submission:
from collections import deque

n, m = map(int, input().split())
uv  =[[] for _ in range(n)]
for _ in range(m):
    u, v = map(int, input().split())
    uv[u - 1].append(v - 1)
s, t = map(int, input().split())
s -= 1
t -= 1

queue = deque()
queue.append(s)
queue.append(1)
queue.append(0)
check = [[False for _ in range(3)] for _ in range(n)]
check[s][0] = True

while queue:
    pos = queue.popleft()
    ans = queue.popleft()
    kkp = queue.popleft()
    for next_pos in uv[pos]:
        if next_pos == t and kkp == 2:
            print(ans // 3)
            exit()
        if check[next_pos][(kkp + 1) % 3]:
            continue
        check[next_pos][(kkp + 1) % 3] = True
        queue.append(next_pos)
        queue.append(ans + 1)
        queue.append((kkp + 1) % 3)
print(-1)