Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int,input().split())
L1 = [set() for k in range(3*(N+1))]
for k in range(M):
	u, v = map(int, input().split())
	L1[3*u].add(3*v+1)
	L1[3*u+1].add(3*v+2)
	L1[3*u+2].add(3*v)
S, T = map(int, input().split())
S = 3*S
T = set([3*T])

t = 0
sumi = set([S])
now = set([S])
flg = 0
while(len(now) != 0):
	t += 1
	pre = set()
	for n in now:
		pre = pre.union(L1[n])
	now = pre - sumi
	sumi = sumi.union(pre)
	if T <= pre:
		print(t//3)
		flg =1
		break
if flg == 0:
	print(-1)

Fixed submission:
N, M = map(int,input().split())
L1 = [[] for k in range(3*(N+1))]
for k in range(M):
	u, v = map(int, input().split())
	L1[3*u].append(3*v+1)
	L1[3*u+1].append(3*v+2)
	L1[3*u+2].append(3*v)
S, T = map(int, input().split())
S = 3*S
T = 3*T

t = 0
sumi = [None for k in range(3*(N+1))]
sumi[T] = -1
now = [S]
flg = 0
while(len(now) != 0):
	t += 1
	pre = []
	for n in now:
		pre.extend(L1[n])
	now = []
	for p in pre:
		if sumi[p] == None:
			sumi[p] = t
			now.append(p)
		elif sumi[p] == -1:
			flg = 1
	if flg == 1:
		print(t//3)
		flg = 2
		break
if flg == 0:
	print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int,input().split())
L1 = [set() for k in range(N+1)]
for k in range(M):
	u, v = map(int, input().split())
	L1[u].add(v)
S, T = map(int, input().split())
L2 = [None for k in range(N+1)]
L3 = [None for k in range(N+1)]
for n in range(N+1):
	a = set()
	for l in L1[n]:
		a = a.union(L1[l])
	L2[n] = a
	a = set()
	for l in L2[n]:
		a = a.union(L1[l])
	a.discard(n)
	L3[n] = a
t = 0
sumi = set([S])
now = set([S])
flg = 0
ansT = set([T])
while(len(now) != 0):
	t +=1
	pre = set()
	for d in now:
		pre = pre.union(L3[d])
	now = pre - sumi
	sumi = sumi.union(now)
	if ansT <= sumi:
		print(t)
		flg = 1
		now = set()
if flg == 0:
	print(-1)

Fixed submission:
N, M = map(int,input().split())
L1 = [[] for k in range(3*(N+1))]
for k in range(M):
	u, v = map(int, input().split())
	L1[3*u].append(3*v+1)
	L1[3*u+1].append(3*v+2)
	L1[3*u+2].append(3*v)
S, T = map(int, input().split())
S = 3*S
T = 3*T

t = 0
sumi = [None for k in range(3*(N+1))]
sumi[T] = -1
now = [S]
flg = 0
while(len(now) != 0):
	t += 1
	pre = []
	for n in now:
		pre.extend(L1[n])
	now = []
	for p in pre:
		if sumi[p] == None:
			sumi[p] = t
			now.append(p)
		elif sumi[p] == -1:
			flg = 1
	if flg == 1:
		print(t//3)
		flg = 2
		break
if flg == 0:
	print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int,input().split())
L1 = [set() for k in range(N+1)]
for k in range(M):
	u, v = map(int, input().split())
	L1[u].add(v)
S, T = map(int, input().split())
L2 = [None for k in range(N+1)]
L3 = [None for k in range(N+1)]
for n in range(N+1):
	a = set()
	for l in L1[n]:
		a = a.union(L1[l])
	L2[n] = a
	a = set()
	for l in L2[n]:
		a = a.union(L1[l])
	a.discard(n)
	L3[n] = a
t = 0
sumi = set([S])
now = set([S])
flg = 0
while(len(now) != 0):
	t +=1
	for d in now:
		now = now.union(L3[d])
	now = now - sumi
	sumi = sumi.union(now)
	if set([T]) <= sumi:
		print(t)
		flg = 1
		now = set()
if flg == 0:
	print(-1)

Fixed submission:
N, M = map(int,input().split())
L1 = [[] for k in range(3*(N+1))]
for k in range(M):
	u, v = map(int, input().split())
	L1[3*u].append(3*v+1)
	L1[3*u+1].append(3*v+2)
	L1[3*u+2].append(3*v)
S, T = map(int, input().split())
S = 3*S
T = 3*T

t = 0
sumi = [None for k in range(3*(N+1))]
sumi[T] = -1
now = [S]
flg = 0
while(len(now) != 0):
	t += 1
	pre = []
	for n in now:
		pre.extend(L1[n])
	now = []
	for p in pre:
		if sumi[p] == None:
			sumi[p] = t
			now.append(p)
		elif sumi[p] == -1:
			flg = 1
	if flg == 1:
		print(t//3)
		flg = 2
		break
if flg == 0:
	print(-1)