Buggy python submission (verdict: Time Limit Exceeded):
def f(n, m):
    to1 = {}
    for _ in range(m):
        u, v = map(int, input().split())
        to1.setdefault(u, [])
        to1[u] += [v]
    s, t = map(int, input().split())

    vst1 = set()
    cnt = 0
    to = {}

    for i in range(1, n + 1):
        ss = set([i])
        for _ in range(3):
            nss = []
            for ii in ss:
                if ii not in to1: continue
                nss+=to1[ii]
            ss = set(nss)
        to[i] = list(ss)

#    print(to)

    ss = set([s])
    while 1:
        cnt += 1
#        print(ss)
        nss=set()
        for i in ss:
            if i not in to: continue
            for j in to[i]:
                nss.add(j)
        ss = nss
#        print(ss)
        if t in ss:
            print(cnt)
            exit()
        if ss <= vst1:
            print(-1)
            exit()
        vst1 |= ss

n, m = map(int, input().split())
f(n, m)


Fixed submission:
def f(n, m):
    to = {}
    for _ in range(m):
        u, v = map(int, input().split())
        to.setdefault(u, [])
        to[u] += [v]
    s, t = map(int, input().split())
    vst = [[False] * 3 for _ in range(n + 1)]
    a = [s]
    vst[s][0] = True
    cnt = 0
    while a:
        b = []
        cnt += 1
        j = cnt % 3
        for ak in a:
            if ak not in to: continue
            for i in to[ak]:
                if vst[i][j]: continue
                vst[i][j] = True
                b.append(i)
            if vst[t][0]:
                print(cnt // 3)
                exit()
        a = b
    print(-1)

n, m = map(int, input().split())
f(n, m)

Buggy python submission (verdict: Time Limit Exceeded):
def f(n, m):
    to1 = {}
    for _ in range(m):
        u, v = map(int, input().split())
        to1.setdefault(u, [])
        to1[u] += [v]
    s, t = map(int, input().split())

    vst1 = set()
    cnt = 0
    to = {}

    for i in range(1, n + 1):
        ss = set([i])
        for _ in range(3):
            nss = set()
            for ii in ss:
                if ii not in to1: continue
                for j in to1[ii]:
                    nss.add(j)
            ss = nss
        to[i] = list(ss)

#    print(to)

    ss = set([s])
    while 1:
        cnt += 1
#        print(ss)
        nss=set()
        for i in ss:
            if i not in to: continue
            for j in to[i]:
                nss.add(j)
        ss = nss
#        print(ss)
        if t in ss:
            print(cnt)
            exit()
        if ss <= vst1:
            print(-1)
            exit()
        vst1 |= ss

n, m = map(int, input().split())
f(n, m)


Fixed submission:
def f(n, m):
    to = {}
    for _ in range(m):
        u, v = map(int, input().split())
        to.setdefault(u, [])
        to[u] += [v]
    s, t = map(int, input().split())
    vst = [[False] * 3 for _ in range(n + 1)]
    a = [s]
    vst[s][0] = True
    cnt = 0
    while a:
        b = []
        cnt += 1
        j = cnt % 3
        for ak in a:
            if ak not in to: continue
            for i in to[ak]:
                if vst[i][j]: continue
                vst[i][j] = True
                b.append(i)
            if vst[t][0]:
                print(cnt // 3)
                exit()
        a = b
    print(-1)

n, m = map(int, input().split())
f(n, m)

Buggy python submission (verdict: Time Limit Exceeded):
def f(n,m):
    to = {}
    rv = {}
    for _ in range(m):
        u, v = map(int, input().split())
        to.setdefault(u, [])
        to[u] += [v]
        rv.setdefault(v, [])
        rv[v] += [u]
    s, t = map(int, input().split())

    ss = set()
    ss.add(s)
    gs = set()
    gs.add(t)
    vst1 = set()
    vst2 = set()
    cnt = 0

    while 1:
        cnt += 1
        for _ in range(3):
            nss = set()
            for i in ss:
                if i not in to: continue
                for j in to[i]:
                    nss.add(j)
            ss = nss
        if ss & gs:
            print(cnt)
            exit()
        if ss <= vst1:
            print(-1)
            exit()
        vst1 |= ss

        cnt += 1
        for _ in range(3):
            ngs = set()
            for i in gs:
                if i not in rv: continue
                for j in rv[i]:
                    ngs.add(j)
            gs = ngs
        if ss & gs:
            print(cnt)
            exit()
        if gs <= vst2:
            print(-1)
            exit()
        vst2 |= gs

n,m=map(int,input().split())
f(n,m)


Fixed submission:
def f(n, m):
    to = {}
    for _ in range(m):
        u, v = map(int, input().split())
        to.setdefault(u, [])
        to[u] += [v]
    s, t = map(int, input().split())
    vst = [[False] * 3 for _ in range(n + 1)]
    a = [s]
    vst[s][0] = True
    cnt = 0
    while a:
        b = []
        cnt += 1
        j = cnt % 3
        for ak in a:
            if ak not in to: continue
            for i in to[ak]:
                if vst[i][j]: continue
                vst[i][j] = True
                b.append(i)
            if vst[t][0]:
                print(cnt // 3)
                exit()
        a = b
    print(-1)

n, m = map(int, input().split())
f(n, m)

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
to={}
rv={}
for _ in range(m):
    u,v=map(int,input().split())
    to.setdefault(u,[])
    to[u]+=[v]
    rv.setdefault(v, [])
    rv[v] += [u]
s,t=map(int,input().split())

ss=set()
ss.add(s)
gs=set()
gs.add(t)
vst1=set()
vst2=set()
cnt=0

while 1:
    cnt+=1
    for _ in range(3):
        nss=set()
        for i in ss:
            if i not in to:continue
            for j in to[i]:
                nss.add(j)
        ss=nss
    if ss&gs:
        print(cnt)
        exit()
    if ss<=vst1:
        print(-1)
        exit()
    vst1|=ss

    cnt+=1
    for _ in range(3):
        ngs=set()
        for i in gs:
            if i not in rv:continue
            for j in rv[i]:
                ngs.add(j)
        gs=ngs
    if ss&gs:
        print(cnt)
        exit()
    if gs<=vst2:
        print(-1)
        exit()
    vst2|=gs


Fixed submission:
def f(n, m):
    to = {}
    for _ in range(m):
        u, v = map(int, input().split())
        to.setdefault(u, [])
        to[u] += [v]
    s, t = map(int, input().split())
    vst = [[False] * 3 for _ in range(n + 1)]
    a = [s]
    vst[s][0] = True
    cnt = 0
    while a:
        b = []
        cnt += 1
        j = cnt % 3
        for ak in a:
            if ak not in to: continue
            for i in to[ak]:
                if vst[i][j]: continue
                vst[i][j] = True
                b.append(i)
            if vst[t][0]:
                print(cnt // 3)
                exit()
        a = b
    print(-1)

n, m = map(int, input().split())
f(n, m)

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
to={}
rv={}
for _ in range(m):
    u,v=map(int,input().split())
    to.setdefault(u,[])
    to[u]+=[v]
    rv.setdefault(v, [])
    rv[v] += [u]
s,t=map(int,input().split())

ss=set()
ss.add(s)
gs=set()
gs.add(t)
vst1=set()
vst2=set()
cnt=0

while 1:
    cnt+=1
    for _ in range(3):
        nss=set()
        for i in ss:
            if i not in to:continue
            for j in to[i]:
                nss.add(j)
        ss=nss
    if ss&gs:
        print(cnt)
        exit()
    if ss<=vst1:
        print(-1)
        exit()
    vst1|=ss

    cnt+=1
    for _ in range(3):
        ngs=set()
        for i in gs:
            if i not in rv:continue
            for j in rv[i]:
                ngs.add(j)
        gs=ngs
    if ss&gs:
        print(cnt)
        exit()
    if gs<=vst2:
        print(-1)
        exit()
    vst2|=gs


Fixed submission:
def f(n, m):
    to = {}
    for _ in range(m):
        u, v = map(int, input().split())
        to.setdefault(u, [])
        to[u] += [v]
    s, t = map(int, input().split())
    vst = [[False] * 3 for _ in range(n + 1)]
    a = [s]
    vst[s][0] = True
    cnt = 0
    while a:
        b = []
        cnt += 1
        j = cnt % 3
        for ak in a:
            if ak not in to: continue
            for i in to[ak]:
                if vst[i][j]: continue
                vst[i][j] = True
                b.append(i)
            if vst[t][0]:
                print(cnt // 3)
                exit()
        a = b
    print(-1)

n, m = map(int, input().split())
f(n, m)