from collections import deque



N, M = list(map(int, input().split()))

d = {i:[] for i in range(1, N+1)}

l = []

INF = 10**12

v = INF



for _ in range(M):

    A, B = list(map(int, input().split()))

    d[A].append(B)

    l.append((A, B))



ans = []

for i, j in l:

    INF = 10**12

    dist = [-1]*(N+1)

    prev = [-1]*(N+1)

    dist[j] = 0

    prev[j] = 0

    q = deque([j])



    while q:

        a = q.popleft()

        for b in d[a]:

            if dist[b]==-1:

                dist[b] = dist[a]+1

                prev[b] = a

                q.append(b)



    if v>dist[i]+1 and dist[i]!=-1:

        v = dist[i]+1

        buf = i

        ans = []

        while buf!=0:

            ans.append(buf)

            buf = prev[buf]



if ans:

    print((len(ans)))

    for i in ans:

        print(i)

else:

    print((-1))

from collections import deque

import sys

input=sys.stdin.readline





def solve():

    N, M = list(map(int, input().split()))

    d = {i:[] for i in range(1, N+1)}

    l = []

    INF = 10**12

    v = INF



    for _ in range(M):

        A, B = list(map(int, input().split()))

        d[A].append(B)

        l.append((A, B))



    ans = []

    for i, j in l:

        INF = 10**12

        dist = [-1]*(N+1)

        prev = [-1]*(N+1)

        dist[j] = 0

        prev[j] = 0

        q = deque([j])



        while q:

            a = q.popleft()

            for b in d[a]:

                if dist[b]==-1:

                    dist[b] = dist[a]+1

                    prev[b] = a

                    q.append(b)



        if v>dist[i]+1 and dist[i]!=-1:

            v = dist[i]+1

            buf = i

            ans = []

            while buf:

                ans.append(buf)

                buf = prev[buf]



    if ans:

        print((len(ans)))

        for i in ans:

            print(i)

    else:

        print((-1))





if __name__ == "__main__":

    solve()

from collections import deque

N, M = list(map(int, input().split()))
d = {i: [] for i in range(1, N + 1)}
l = []
INF = 10**12
v = INF
for _ in range(M):
    A, B = list(map(int, input().split()))
    d[A].append(B)
    l.append((A, B))
ans = []
for i, j in l:
    INF = 10**12
    dist = [-1] * (N + 1)
    prev = [-1] * (N + 1)
    dist[j] = 0
    prev[j] = 0
    q = deque([j])
    while q:
        a = q.popleft()
        for b in d[a]:
            if dist[b] == -1:
                dist[b] = dist[a] + 1
                prev[b] = a
                q.append(b)
    if v > dist[i] + 1 and dist[i] != -1:
        v = dist[i] + 1
        buf = i
        ans = []
        while buf != 0:
            ans.append(buf)
            buf = prev[buf]
if ans:
    print((len(ans)))
    for i in ans:
        print(i)
else:
    print((-1))

from collections import deque
import sys

input = sys.stdin.readline


def solve():
    N, M = list(map(int, input().split()))
    d = {i: [] for i in range(1, N + 1)}
    l = []
    INF = 10**12
    v = INF
    for _ in range(M):
        A, B = list(map(int, input().split()))
        d[A].append(B)
        l.append((A, B))
    ans = []
    for i, j in l:
        INF = 10**12
        dist = [-1] * (N + 1)
        prev = [-1] * (N + 1)
        dist[j] = 0
        prev[j] = 0
        q = deque([j])
        while q:
            a = q.popleft()
            for b in d[a]:
                if dist[b] == -1:
                    dist[b] = dist[a] + 1
                    prev[b] = a
                    q.append(b)
        if v > dist[i] + 1 and dist[i] != -1:
            v = dist[i] + 1
            buf = i
            ans = []
            while buf:
                ans.append(buf)
                buf = prev[buf]
    if ans:
        print((len(ans)))
        for i in ans:
            print(i)
    else:
        print((-1))


if __name__ == "__main__":
    solve()

+import sys -N, M = list(map(int, input().split())) -d = {i: [] for i in range(1, N + 1)} -l = [] -INF = 10**12 -v = INF -for _ in range(M): -    A, B = list(map(int, input().split())) -    d[A].append(B) -    l.append((A, B)) -ans = [] -for i, j in l: +input = sys.stdin.readline + + +def solve(): +    N, M = list(map(int, input().split())) +    d = {i: [] for i in range(1, N + 1)} +    l = [] -    dist = [-1] * (N + 1) -    prev = [-1] * (N + 1) -    dist[j] = 0 -    prev[j] = 0 -    q = deque([j]) -    while q: -        a = q.popleft() -        for b in d[a]: -            if dist[b] == -1: -                dist[b] = dist[a] + 1 -                prev[b] = a -                q.append(b) -    if v > dist[i] + 1 and dist[i] != -1: -        v = dist[i] + 1 -        buf = i -        ans = [] -        while buf != 0: -            ans.append(buf) -            buf = prev[buf] -if ans: -    print((len(ans))) -    for i in ans: -        print(i) -else: -    print((-1)) +    v = INF +    for _ in range(M): +        A, B = list(map(int, input().split())) +        d[A].append(B) +        l.append((A, B)) +    ans = [] +    for i, j in l: +        INF = 10**12 +        dist = [-1] * (N + 1) +        prev = [-1] * (N + 1) +        dist[j] = 0 +        prev[j] = 0 +        q = deque([j]) +        while q: +            a = q.popleft() +            for b in d[a]: +                if dist[b] == -1: +                    dist[b] = dist[a] + 1 +                    prev[b] = a +                    q.append(b) +        if v > dist[i] + 1 and dist[i] != -1: +            v = dist[i] + 1 +            buf = i +            ans = [] +            while buf: +                ans.append(buf) +                buf = prev[buf] +    if ans: +        print((len(ans))) +        for i in ans: +            print(i) +    else: +        print((-1)) + + +if __name__ == "__main__": +    solve()