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

def solve():
    input = sys.stdin.readline
    N, M = map(int, input().split())
    son = [[] for i in range(N)]
    for i in range(M):
        u, v = map(int, input().split())
        son[u-1].append(v-1)
    S, T = map(int, input().split())

    graphB = [set() for i in range(N)] 
    for i in range(N):
        for s1 in son[i]:
            for s2 in son[s1]:
                graphB[i] |= set(son[s2])
    
    q = collections.deque()
    visited = [-1] * N
    q.append((0, S-1))
    while q:
        cost, now = q.popleft()
        if visited[now] == -1:
            visited[now] = cost
            for s in graphB[now]:
                if visited[s] == -1: q.append((cost + 1, s))

    print(visited[T-1])
    return 0

if __name__ == "__main__":
    solve()

Fixed submission:
import sys, collections

def solve():
    input = sys.stdin.readline
    N, M = map(int, input().split())
    son = [[] for i in range(N)] 
    for i in range(M):
        u, v = map(int, input().split())
        son[u-1].append(v-1)
    S, T = map(int, input().split())

    dist = [[-1, -1, -1] for i in range(N)]
    que = collections.deque()
    que.append((0, S-1, 0)) #k回目の移動, 場所, mod
    while que:
        cost, now, mod = que.popleft()
        if dist[now][mod] == -1:
            dist[now][mod] = cost
            if mod == 0:
                for s in son[now]:
                    if dist[s][1] == -1: que.append((cost + 1, s, 1))
            else:
                for s in son[now]:
                    if dist[s][(mod + 1) % 3] == -1: que.append((cost, s, (mod + 1) % 3))

    print(dist[T-1][0])  
    return 0

if __name__ == "__main__":
    solve()

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

def solve():
    input = sys.stdin.readline
    N, M = map(int, input().split())
    son = [[] for i in range(N)]
    for i in range(M):
        u, v = map(int, input().split())
        son[u-1].append(v-1)
    S, T = map(int, input().split())

    graphB = [set() for i in range(N)] 
    for i in range(N):
        for s1 in son[i]:
            for s2 in son[s1]:
                graphB[i] |= set(son[s2])
    
    q = collections.deque()
    visited = [-1] * N
    q.append((0, S-1))
    while q:
        cost, now = q.popleft()
        if visited[now] == -1:
            visited[now] = cost
            for s in graphB[now]:
                if visited[s] == -1: q.append((cost + 1, s))

    print(visited[T-1])
    return 0

if __name__ == "__main__":
    solve()

Fixed submission:
import sys, collections

def solve():
    input = sys.stdin.readline
    N, M = map(int, input().split())
    son = [[] for i in range(N)] 
    for i in range(M):
        u, v = map(int, input().split())
        son[u-1].append(v-1)
    S, T = map(int, input().split())

    dist = [[-1, -1, -1] for i in range(N)]
    que = collections.deque()
    que.append((0, S-1, 0)) #k回目の移動, 場所, mod
    while que:
        cost, now, mod = que.popleft()
        if dist[now][mod] == -1:
            dist[now][mod] = cost
            if mod == 0:
                for s in son[now]:
                    if dist[s][1] == -1: que.append((cost + 1, s, 1))
            else:
                for s in son[now]:
                    if dist[s][(mod + 1) % 3] == -1: que.append((cost, s, (mod + 1) % 3))

    print(dist[T-1][0])  
    return 0

if __name__ == "__main__":
    solve()

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

def solve():
    input = sys.stdin.readline
    N, M = map(int, input().split())
    son = [[] for i in range(N)]
    for i in range(M):
        u, v = map(int, input().split())
        son[u-1].append(v-1)
    S, T = map(int, input().split())

    graphB = [[] for i in range(N)] 
    for i in range(N):
        for s1 in son[i]:
            for s2 in son[s1]:
                for s3 in son[s2]: graphB[i].append(s3)
    
    q = collections.deque()
    visited = [-1] * N
    q.append((0, S-1))
    while q:
        cost, now = q.popleft()
        if visited[now] == -1:
            visited[now] = cost
            for s in graphB[now]:
                if visited[s] == -1: q.append((cost + 1, s))

    print(visited[T-1])
    return 0

if __name__ == "__main__":
    solve()

Fixed submission:
import sys, collections

def solve():
    input = sys.stdin.readline
    N, M = map(int, input().split())
    son = [[] for i in range(N)] 
    for i in range(M):
        u, v = map(int, input().split())
        son[u-1].append(v-1)
    S, T = map(int, input().split())

    dist = [[-1, -1, -1] for i in range(N)]
    que = collections.deque()
    que.append((0, S-1, 0)) #k回目の移動, 場所, mod
    while que:
        cost, now, mod = que.popleft()
        if dist[now][mod] == -1:
            dist[now][mod] = cost
            if mod == 0:
                for s in son[now]:
                    if dist[s][1] == -1: que.append((cost + 1, s, 1))
            else:
                for s in son[now]:
                    if dist[s][(mod + 1) % 3] == -1: que.append((cost, s, (mod + 1) % 3))

    print(dist[T-1][0])  
    return 0

if __name__ == "__main__":
    solve()

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

def solve():
    input = sys.stdin.readline
    N, M = map(int, input().split())
    son = [[] for i in range(N)]
    for i in range(M):
        u, v = map(int, input().split())
        son[u-1].append(v-1)
    S, T = map(int, input().split())

    graphB = [set() for i in range(N)]
    q = collections.deque()
    for i in range(N):
        q.append((0, i))
        pathi = set()
        while q:
            cost, now = q.popleft()
            if cost <= 1:
                for s in son[now]: q.append((cost+1, s))
            elif cost == 2: 
                for s in son[now]: pathi |= {s}
        graphB[i] = pathi
    
    visited = [-1] * N
    q.append((0, S-1))
    while q:
        cost, now = q.popleft()
        if visited[now] < 0:
            visited[now] = cost
            for s in graphB[now]:
                if visited[s] < 0: q.append((cost + 1, s))

    print(visited[T-1])
    return 0

if __name__ == "__main__":
    solve()

Fixed submission:
import sys, collections

def solve():
    input = sys.stdin.readline
    N, M = map(int, input().split())
    son = [[] for i in range(N)] 
    for i in range(M):
        u, v = map(int, input().split())
        son[u-1].append(v-1)
    S, T = map(int, input().split())

    dist = [[-1, -1, -1] for i in range(N)]
    que = collections.deque()
    que.append((0, S-1, 0)) #k回目の移動, 場所, mod
    while que:
        cost, now, mod = que.popleft()
        if dist[now][mod] == -1:
            dist[now][mod] = cost
            if mod == 0:
                for s in son[now]:
                    if dist[s][1] == -1: que.append((cost + 1, s, 1))
            else:
                for s in son[now]:
                    if dist[s][(mod + 1) % 3] == -1: que.append((cost, s, (mod + 1) % 3))

    print(dist[T-1][0])  
    return 0

if __name__ == "__main__":
    solve()

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

def solve():
    input = sys.stdin.readline
    N, M = map(int, input().split())
    son = [[] for i in range(N)]
    for i in range(M):
        u, v = map(int, input().split())
        son[u-1].append(v-1)
    S, T = map(int, input().split())

    graphB = [set() for i in range(N)]
    for i in range(N):
        q = collections.deque()
        q.append((0, i))
        pathi = set()
        while q:
            cost, now = q.popleft()
            if cost > 3: break
            elif cost == 3:
                pathi |= {now}
                for s in son[now]: q.append((cost + 1, s))
            else:
                for s in son[now]: q.append((cost + 1, s))
        graphB[i] = pathi
    
    q = collections.deque()
    visited = [-1] * N
    q.append((0, S-1))
    while q:
        cost, now = q.popleft()
        if visited[now] < 0:
            visited[now] = cost
            for s in graphB[now]:
                if visited[s] < 0: q.append((cost + 1, s))

    print(visited[T-1])
    return 0

if __name__ == "__main__":
    solve()

Fixed submission:
import sys, collections

def solve():
    input = sys.stdin.readline
    N, M = map(int, input().split())
    son = [[] for i in range(N)] 
    for i in range(M):
        u, v = map(int, input().split())
        son[u-1].append(v-1)
    S, T = map(int, input().split())

    dist = [[-1, -1, -1] for i in range(N)]
    que = collections.deque()
    que.append((0, S-1, 0)) #k回目の移動, 場所, mod
    while que:
        cost, now, mod = que.popleft()
        if dist[now][mod] == -1:
            dist[now][mod] = cost
            if mod == 0:
                for s in son[now]:
                    if dist[s][1] == -1: que.append((cost + 1, s, 1))
            else:
                for s in son[now]:
                    if dist[s][(mod + 1) % 3] == -1: que.append((cost, s, (mod + 1) % 3))

    print(dist[T-1][0])  
    return 0

if __name__ == "__main__":
    solve()

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

def solve():
    input = sys.stdin.readline
    N, M = map(int, input().split())
    son = [[] for i in range(N)]
    for i in range(M):
        u, v = map(int, input().split())
        son[u-1].append(v-1)
    S, T = map(int, input().split())

    graphB = [set() for i in range(N)]
    for i in range(N):
        q = collections.deque()
        q.append((0, i))
        pathi = set()
        while q:
            cost, now = q.popleft()
            if cost > 3: break
            elif cost == 3:
                pathi |= {now}
                for s in son[now]: q.append((cost + 1, s))
            else:
                for s in son[now]: q.append((cost + 1, s))
        graphB[i] = pathi
    
    q = collections.deque()
    visited = [-1] * N
    q.append((0, S-1))
    while q:
        cost, now = q.popleft()
        if visited[now] < 0:
            visited[now] = cost
            for s in graphB[now]:
                if visited[s] < 0: q.append((cost + 1, s))

    print(visited[T-1])
    return 0

if __name__ == "__main__":
    solve()

Fixed submission:
import sys, collections

def solve():
    input = sys.stdin.readline
    N, M = map(int, input().split())
    son = [[] for i in range(N)] 
    for i in range(M):
        u, v = map(int, input().split())
        son[u-1].append(v-1)
    S, T = map(int, input().split())

    dist = [[-1, -1, -1] for i in range(N)]
    que = collections.deque()
    que.append((0, S-1, 0)) #k回目の移動, 場所, mod
    while que:
        cost, now, mod = que.popleft()
        if dist[now][mod] == -1:
            dist[now][mod] = cost
            if mod == 0:
                for s in son[now]:
                    if dist[s][1] == -1: que.append((cost + 1, s, 1))
            else:
                for s in son[now]:
                    if dist[s][(mod + 1) % 3] == -1: que.append((cost, s, (mod + 1) % 3))

    print(dist[T-1][0])  
    return 0

if __name__ == "__main__":
    solve()