Buggy python submission (verdict: Time Limit Exceeded):
import heapq
N,M = map(int,input().split())
edge = [[] for i in range(N)]
double_edge = [[] for i in range(N)]
triple_edge = [[] for i in range(N)]

for i in range(M):
	u,v = map(int,input().split())
	u -= 1
	v -= 1
	edge[u].append(v)
S,T = map(int,input().split())
S -= 1
T -= 1
for i in range(N):
	e = edge[i]
	l = []
	for x in e:
		l += edge[x]
	double_edge[i] = list(set(l))
for i in range(N):
	e = edge[i]
	l = []
	for x in e:
		l += double_edge[x]
	l = list(set(l))
	if i in l:
		l.remove(i)
	triple_edge[i] = l

INF = 10**10
dist = [INF if i != S else 0 for i in range(N)]
search = []
searched = [False for i in range(N)]
heapq.heapify(search)
heapq.heappush(search, (0,S))
while len(search) > 0 and dist[T] == INF:
	x = heapq.heappop(search)
	d, node = x[0], x[1]
	if searched[node]:
		continue
	searched[S] = True
	dist[node] = d
	for i in triple_edge[node]:
		if not searched[i]:
			heapq.heappush(search, (min(d+1, dist[i]), i))
if dist[T] < INF:
	print(dist[T])
else:
	print(-1)

Fixed submission:
import heapq
N,M = map(int,input().split())
edge = [[[] for i in range(N)] for j in range(3)]

for i in range(M):
	u,v = map(int,input().split())
	u -= 1
	v -= 1
	edge[0][u].append(v)
	edge[1][u].append(v)
	edge[2][u].append(v)
S,T = map(int,input().split())
S -= 1
T -= 1
INF = 10**10
dist = [[0 if i == S and j == 0 else INF for i in range(N)] for j in range(3)]
search = []
searched = [[False for i in range(N)] for j in range(3)]
heapq.heapify(search)
heapq.heappush(search, (0, 0, S))
while len(search) > 0 and dist[0][T] == INF:
	x = heapq.heappop(search)
	d, t, node = x[0], x[1], x[2]
	if searched[t][node]:
		continue
	searched[t][node] = True
	dist[t][node] = d
	for i in edge[t][node]:
		if not searched[(t+1)%3][i]:
			heapq.heappush(search, (d+(t+1)//3, (t+1) % 3, i))
if dist[0][T] < INF:
	print(dist[0][T])
else:
	print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
N,M = map(int,input().split())
edge = [[] for i in range(N)]
double_edge = [[] for i in range(N)]
triple_edge = [[] for i in range(N)]

for i in range(M):
	u,v = map(int,input().split())
	u -= 1
	v -= 1
	edge[u].append(v)
S,T = map(int,input().split())
S -= 1
T -= 1
for i in range(N):
	e = edge[i]
	l = []
	for x in e:
		l += edge[x]
	double_edge[i] = list(set(l))
for i in range(N):
	e = edge[i]
	l = []
	for x in e:
		l += double_edge[x]
	l = list(set(l))
	if i in l:
		l.remove(i)
	triple_edge[i] = l

INF = 10**10
dist = [INF if i != S else 0 for i in range(N)]
search = []
searched = [False for i in range(N)]
heapq.heapify(search)
heapq.heappush(search, (0,S))
while len(search) > 0 and dist[T] == INF:
	x = heapq.heappop(search)
	d, node = x[0], x[1]
	if searched[node]:
		continue
	searched[S] = True
	dist[node] = d
	for i in triple_edge[node]:
		if not searched[i]:
			heapq.heappush(search, (min(d+1, dist[i]), i))
if dist[T] < INF:
	print(dist[T])
else:
	print(-1)

Fixed submission:
import heapq
N,M = map(int,input().split())
edge = [[[] for i in range(N)] for j in range(3)]

for i in range(M):
	u,v = map(int,input().split())
	u -= 1
	v -= 1
	edge[0][u].append(v)
	edge[1][u].append(v)
	edge[2][u].append(v)
S,T = map(int,input().split())
S -= 1
T -= 1
INF = 10**10
dist = [[0 if i == S and j == 0 else INF for i in range(N)] for j in range(3)]
search = []
searched = [[False for i in range(N)] for j in range(3)]
heapq.heapify(search)
heapq.heappush(search, (0, 0, S))
while len(search) > 0 and dist[0][T] == INF:
	x = heapq.heappop(search)
	d, t, node = x[0], x[1], x[2]
	if searched[t][node]:
		continue
	searched[t][node] = True
	dist[t][node] = d
	for i in edge[t][node]:
		if not searched[(t+1)%3][i]:
			heapq.heappush(search, (d+(t+1)//3, (t+1) % 3, i))
if dist[0][T] < INF:
	print(dist[0][T])
else:
	print(-1)