Buggy python submission (verdict: Time Limit Exceeded):
import sys
N, M = map(int, input().split())

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

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


par = [i for i in range(3*N)]
par2 = [-1 for i in range(3*N)]
rank = [0 for i in range(3*N)]

def find(x):
    if par[x] == x:
        return x
    else:
        return find(par[x])

def same(x, y):
    return find(x) == find(y)

def unite(x, y):
    x = find(x)
    y = find(y)
    if x == y:
        return
    if rank[x] < rank[y]:
        par[x] = y
    else:
        par[y] = x
        if rank[x] == rank[y]:
            rank[x] += 1

ans = 0
start = [S-1]
frag = 1
g = -1
while frag:
    frag = 0
    ans += 1
    g += 1
    g %= 3
    l = len(start)
    for i in range(l):
        s = start.pop(0)
        if par2[s + g * N] == 1:
            continue
        par2[s + g * N] == 1
        for j in range(len(G[s])):
            start.append(G[s][j])
            if g == 2:
                if same(s + 2 * N, G[s][j]):
                    continue
                else:
                    frag = 1
                    unite(s + 2 * N, G[s][j])

            else:
                if same(s + g * N, G[s][j] + (g+1) * N):
                    continue
                else:
                    frag = 1
                unite(s+ g * N, G[s][j] + (g+1) * N)

        if same(S-1, T-1) and par[S-1] != -1:
            print(ans//3)
            sys.exit()
print(-1)

Fixed submission:
import sys
import heapq
N, M = map(int, input().split())

G = [[] for i in range(3*N)]
for i in range(M):
    u, v = map(int, input().split())
    G[u-1].append([v-1 + N, 1])
    G[u-1 + N].append([v-1 + 2*N, 1])
    G[u-1 + 2*N].append([v-1, 1])

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

INF = 10**10
d = [INF for i in range(3*N)]

que = []
d[S-1] = 0
heapq.heappush(que, [0, S-1])

while not(len(que) == 0):
    q = heapq.heappop(que)
    ver = q[1]
    dis = q[0]
    
    if d[ver] < dis:
        continue

    for i in range(len(G[ver])):
        e = G[ver][i]
        if d[e[0]] > d[ver] + e[1]:
            d[e[0]] = d[ver] + e[1]
            heapq.heappush(que, [d[e[0]], e[0]])

if d[T-1] == INF:
    print(-1)
else:
    print(d[T-1]//3)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
N, M = map(int, input().split())

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

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


par = [i for i in range(3*N)]
par2 = [-1 for i in range(3*N)]
rank = [0 for i in range(3*N)]

def find(x):
    if par[x] == x:
        return x
    else:
        return find(par[x])

def same(x, y):
    return find(x) == find(y)

def unite(x, y):
    x = find(x)
    y = find(y)
    if x == y:
        return
    if rank[x] < rank[y]:
        par[x] = y
    else:
        par[y] = x
        if rank[x] == rank[y]:
            rank[x] += 1

ans = 0
start = [S-1]
frag = 1
g = -1
while frag:
    frag = 0
    ans += 1
    g += 1
    g %= 3
    l = len(start)
    for i in range(l):
        s = start.pop(0)
        if par2[s + g * N] == 1:
            continue
        par2[s + g * N] == 1
        for j in range(len(G[s])):
            start.append(G[s][j])
            if g == 2:
                if same(s + 2 * N, G[s][j]):
                    continue
                else:
                    frag = 1
                    unite(s + 2 * N, G[s][j])

            else:
                if same(s + g * N, G[s][j] + (g+1) * N):
                    continue
                else:
                    frag = 1
                unite(s+ g * N, G[s][j] + (g+1) * N)

        if same(S-1, T-1) and par[S-1] != -1:
            print(ans//3)
            sys.exit()
print(-1)

Fixed submission:
import sys
import heapq
N, M = map(int, input().split())

G = [[] for i in range(3*N)]
for i in range(M):
    u, v = map(int, input().split())
    G[u-1].append([v-1 + N, 1])
    G[u-1 + N].append([v-1 + 2*N, 1])
    G[u-1 + 2*N].append([v-1, 1])

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

INF = 10**10
d = [INF for i in range(3*N)]

que = []
d[S-1] = 0
heapq.heappush(que, [0, S-1])

while not(len(que) == 0):
    q = heapq.heappop(que)
    ver = q[1]
    dis = q[0]
    
    if d[ver] < dis:
        continue

    for i in range(len(G[ver])):
        e = G[ver][i]
        if d[e[0]] > d[ver] + e[1]:
            d[e[0]] = d[ver] + e[1]
            heapq.heappush(que, [d[e[0]], e[0]])

if d[T-1] == INF:
    print(-1)
else:
    print(d[T-1]//3)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
N, M = map(int, input().split())

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

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


par = [i for i in range(3*N)]
rank = [0 for i in range(3*N)]

def find(x):
    if par[x] == x:
        return x
    else:
        return find(par[x])

def same(x, y):
    return find(x) == find(y)

def unite(x, y):
    x = find(x)
    y = find(y)
    if x == y:
        return
    if rank[x] < rank[y]:
        par[x] = y
    else:
        par[y] = x
        if rank[x] == rank[y]:
            rank[x] += 1

ans = 0
start = [S-1]
frag = 1
g = -1
while frag:
    frag = 0
    ans += 1
    g += 1
    g %= 3
    l = len(start)
    for i in range(l):
        s = start.pop(0)
        for j in range(len(G[s])):
            start.append(G[s][j])
            if g == 2:
                if same(s + 2 * N, G[s][j]):
                    continue
                else:
                    frag = 1
                    unite(s + 2 * N, G[s][j])

            else:
                if same(s + g * N, G[s][j] + (g+1) * N):
                    continue
                else:
                    frag = 1
                unite(s+ g * N, G[s][j] + (g+1) * N)

        if same(S-1, T-1) and par[S-1] != -1:
            print(ans//3)
            sys.exit()
print(-1)

Fixed submission:
import sys
import heapq
N, M = map(int, input().split())

G = [[] for i in range(3*N)]
for i in range(M):
    u, v = map(int, input().split())
    G[u-1].append([v-1 + N, 1])
    G[u-1 + N].append([v-1 + 2*N, 1])
    G[u-1 + 2*N].append([v-1, 1])

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

INF = 10**10
d = [INF for i in range(3*N)]

que = []
d[S-1] = 0
heapq.heappush(que, [0, S-1])

while not(len(que) == 0):
    q = heapq.heappop(que)
    ver = q[1]
    dis = q[0]
    
    if d[ver] < dis:
        continue

    for i in range(len(G[ver])):
        e = G[ver][i]
        if d[e[0]] > d[ver] + e[1]:
            d[e[0]] = d[ver] + e[1]
            heapq.heappush(que, [d[e[0]], e[0]])

if d[T-1] == INF:
    print(-1)
else:
    print(d[T-1]//3)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
N, M = map(int, input().split())

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

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


par = [i for i in range(3*N)]
rank = [0 for i in range(3*N)]

def find(x):
    if par[x] == x:
        return x
    else:
        return find(par[x])

def same(x, y):
    return find(x) == find(y)

def unite(x, y):
    x = find(x)
    y = find(y)
    if x == y:
        return
    if rank[x] < rank[y]:
        par[x] = y
    else:
        par[y] = x
        if rank[x] == rank[y]:
            rank[x] += 1

ans = 0
start = [S-1]
frag = 1
g = -1
while frag:
    frag = 0
    ans += 1
    g += 1
    g %= 3
    l = len(start)
    for i in range(l):
        s = start.pop(0)
        for j in range(len(G[s])):
            start.append(G[s][j])
            if g == 2:
                if same(s + 2 * N, G[s][j]):
                    continue
                else:
                    frag = 1
                    unite(s + 2 * N, G[s][j])

            else:
                if same(s + g * N, G[s][j] + (g+1) * N):
                    continue
                else:
                    frag = 1
                unite(s+ g * N, G[s][j] + (g+1) * N)

        if same(S-1, T-1) and par[S-1] != -1:
            print(ans//3)
            sys.exit()
print(-1)

Fixed submission:
import sys
import heapq
N, M = map(int, input().split())

G = [[] for i in range(3*N)]
for i in range(M):
    u, v = map(int, input().split())
    G[u-1].append([v-1 + N, 1])
    G[u-1 + N].append([v-1 + 2*N, 1])
    G[u-1 + 2*N].append([v-1, 1])

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

INF = 10**10
d = [INF for i in range(3*N)]

que = []
d[S-1] = 0
heapq.heappush(que, [0, S-1])

while not(len(que) == 0):
    q = heapq.heappop(que)
    ver = q[1]
    dis = q[0]
    
    if d[ver] < dis:
        continue

    for i in range(len(G[ver])):
        e = G[ver][i]
        if d[e[0]] > d[ver] + e[1]:
            d[e[0]] = d[ver] + e[1]
            heapq.heappush(que, [d[e[0]], e[0]])

if d[T-1] == INF:
    print(-1)
else:
    print(d[T-1]//3)