Buggy python submission (verdict: Time Limit Exceeded):
# import bisect
from collections import Counter, deque
# import copy
# from heapq import heappush, heappop, heapify
# from fractions import gcd
# import itertools
# from operator import attrgetter, itemgetter
# import math

import sys

# import numpy as np

ipti = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    n, m = list(map(int, ipti().split()))
    node_next = [set() for _ in range(n)]

    for i in range(m):
        u, v = list(map(int, ipti().split()))
        node_next[u-1].add(v-1)

    s, t = list(map(int, ipti().split()))
    s, t = s - 1, t - 1

    seen = [[0] * n for _ in range(3)]

    que = deque()
    que.append((s, 0, 0))

    while que:
        cnode, cstate, dist = que.popleft()
        seen[cstate][cnode] = 1
        dist += 1
        nstate = (cstate + 1) % 3
        for nnode in node_next[cnode]:
            if seen[nstate][nnode] == 0:
                if nstate == 0 and nnode == t:
                    print(dist // 3)
                    sys.exit()
                else:
                    que.append((nnode, nstate, dist))

    print(-1)


if __name__ == '__main__':
    main()


Fixed submission:
# import bisect
from collections import Counter, deque
# import copy
# from heapq import heappush, heappop, heapify
# from fractions import gcd
# import itertools
# from operator import attrgetter, itemgetter
# import math

import sys

# import numpy as np

ipti = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    n, m = list(map(int, ipti().split()))
    node_next = [set() for _ in range(n)]

    for i in range(m):
        u, v = list(map(int, ipti().split()))
        node_next[u - 1].add(v - 1)

    s, t = list(map(int, ipti().split()))
    s, t = s - 1, t - 1

    dist = [[INF] * n for _ in range(3)]
    dist[0][s] = 0

    que = deque()
    que.append((0, s))

    while que:
        cstate, cnode = que.popleft()
        d = dist[cstate][cnode]
        nstate = (cstate + 1) % 3
        for nnode in node_next[cnode]:
            if dist[nstate][nnode] == INF:
                dist[nstate][nnode] = d + 1
                que.append((nstate, nnode))

    if dist[0][t] != INF:
        print(dist[0][t] // 3)
    else:
        print(-1)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
# import bisect
from collections import Counter, deque
# import copy
# from heapq import heappush, heappop, heapify
# from fractions import gcd
# import itertools
# from operator import attrgetter, itemgetter
# import math

import sys

# import numpy as np

ipti = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    n, m = list(map(int, ipti().split()))
    node_next = [set() for _ in range(n)]

    for i in range(m):
        u, v = list(map(int, ipti().split()))
        u, v = u - 1, v - 1
        node_next[u].add(v)

    s, t = list(map(int, ipti().split()))
    s, t = s - 1, t - 1

    seen = [[0] * 3 for _ in range(n)]

    que = deque()
    que.append((s, 0, 0))

    while que:
        cnode, cstate, dist = que.popleft()
        seen[cnode][cstate] = 1
        dist += 1
        nstate = (cstate + 1) % 3
        for nnode in node_next[cnode]:
            if nstate == 0 and nnode == t:
                print(dist // 3)
                sys.exit()
            else:
                if seen[nnode][nstate] == 0:
                    que.append((nnode, nstate, dist))

    print(-1)


if __name__ == '__main__':
    main()


Fixed submission:
# import bisect
from collections import Counter, deque
# import copy
# from heapq import heappush, heappop, heapify
# from fractions import gcd
# import itertools
# from operator import attrgetter, itemgetter
# import math

import sys

# import numpy as np

ipti = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    n, m = list(map(int, ipti().split()))
    node_next = [set() for _ in range(n)]

    for i in range(m):
        u, v = list(map(int, ipti().split()))
        node_next[u - 1].add(v - 1)

    s, t = list(map(int, ipti().split()))
    s, t = s - 1, t - 1

    dist = [[INF] * n for _ in range(3)]
    dist[0][s] = 0

    que = deque()
    que.append((0, s))

    while que:
        cstate, cnode = que.popleft()
        d = dist[cstate][cnode]
        nstate = (cstate + 1) % 3
        for nnode in node_next[cnode]:
            if dist[nstate][nnode] == INF:
                dist[nstate][nnode] = d + 1
                que.append((nstate, nnode))

    if dist[0][t] != INF:
        print(dist[0][t] // 3)
    else:
        print(-1)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
# import bisect
from collections import Counter, deque
# import copy
# from heapq import heappush, heappop, heapify
# from fractions import gcd
# import itertools
# from operator import attrgetter, itemgetter
# import math

import sys

# import numpy as np

ipti = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    n, m = list(map(int, ipti().split()))
    node_next = [[[] for __ in range(3)] for _ in range(n)]

    for i in range(m):
        u, v = list(map(int, ipti().split()))
        u, v = u - 1, v - 1
        node_next[u][0].append((v, 1))
        node_next[u][1].append((v, 2))
        node_next[u][2].append((v, 0))

    s, t = list(map(int, ipti().split()))
    s, t = s - 1, t - 1

    seen = [[0] * 3 for _ in range(n)]

    que = deque()
    que.append((s, 0, 0))

    while que:
        cnode, cstate, dist = que.popleft()
        seen[cnode][cstate] = 1
        dist += 1
        for nnode, nstate in node_next[cnode][cstate]:
            if nnode == t and nstate == 0:
                print(dist // 3)
                sys.exit()
            else:
                if seen[nnode][nstate] == 0:
                    que.append((nnode, nstate, dist))

    print(-1)


if __name__ == '__main__':
    main()


Fixed submission:
# import bisect
from collections import Counter, deque
# import copy
# from heapq import heappush, heappop, heapify
# from fractions import gcd
# import itertools
# from operator import attrgetter, itemgetter
# import math

import sys

# import numpy as np

ipti = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    n, m = list(map(int, ipti().split()))
    node_next = [set() for _ in range(n)]

    for i in range(m):
        u, v = list(map(int, ipti().split()))
        node_next[u - 1].add(v - 1)

    s, t = list(map(int, ipti().split()))
    s, t = s - 1, t - 1

    dist = [[INF] * n for _ in range(3)]
    dist[0][s] = 0

    que = deque()
    que.append((0, s))

    while que:
        cstate, cnode = que.popleft()
        d = dist[cstate][cnode]
        nstate = (cstate + 1) % 3
        for nnode in node_next[cnode]:
            if dist[nstate][nnode] == INF:
                dist[nstate][nnode] = d + 1
                que.append((nstate, nnode))

    if dist[0][t] != INF:
        print(dist[0][t] // 3)
    else:
        print(-1)


if __name__ == '__main__':
    main()