from collections import*

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

outs=defaultdict(list)

ins=defaultdict(int)

for _ in range(m):

    a,b=list(map(int,input().split()))

    outs[a-1]+=b-1,

    ins[b-1]+=1

q=deque(i for i in range(n)if ins[i]==0)

res=[]

while q:

    v=q.popleft()

    res+=v,

    for w in outs[v]:

        ins[w]-=1

        if ins[w]==0:

            q.append(w)

print((-(len(res)==n)))
from collections import*

n,m,*t=list(map(int,open(0).read().split()))

i,o=[0]*n,[[]for _ in range(n)]

for a,b in zip(*[iter(t)]*2):

  o[a-1]+=b-1,

  i[b-1]+=1

q=deque(v for v in range(n)if i[v]<1)

r=[]

while q:

  v=q.popleft()

  r+=v,

  for w in o[v]:

    i[w]-=1

    if i[w]==0:q+=w,

print((-(len(r)==n)))
from collections import *

n, m = list(map(int, input().split()))
outs = defaultdict(list)
ins = defaultdict(int)
for _ in range(m):
    a, b = list(map(int, input().split()))
    outs[a - 1] += (b - 1,)
    ins[b - 1] += 1
q = deque(i for i in range(n) if ins[i] == 0)
res = []
while q:
    v = q.popleft()
    res += (v,)
    for w in outs[v]:
        ins[w] -= 1
        if ins[w] == 0:
            q.append(w)
print((-(len(res) == n)))

from collections import *

n, m, *t = list(map(int, open(0).read().split()))
i, o = [0] * n, [[] for _ in range(n)]
for a, b in zip(*[iter(t)] * 2):
    o[a - 1] += (b - 1,)
    i[b - 1] += 1
q = deque(v for v in range(n) if i[v] < 1)
r = []
while q:
    v = q.popleft()
    r += (v,)
    for w in o[v]:
        i[w] -= 1
        if i[w] == 0:
            q += (w,)
print((-(len(r) == n)))

-n, m = list(map(int, input().split())) -outs = defaultdict(list) -ins = defaultdict(int) -for _ in range(m): -    a, b = list(map(int, input().split())) -    outs[a - 1] += (b - 1,) -    ins[b - 1] += 1 -q = deque(i for i in range(n) if ins[i] == 0) -res = [] +n, m, *t = list(map(int, open(0).read().split())) +i, o = [0] * n, [[] for _ in range(n)] +for a, b in zip(*[iter(t)] * 2): +    o[a - 1] += (b - 1,) +    i[b - 1] += 1 +q = deque(v for v in range(n) if i[v] < 1) +r = [] -    res += (v,) -    for w in outs[v]: -        ins[w] -= 1 -        if ins[w] == 0: -            q.append(w) -print((-(len(res) == n))) +    r += (v,) +    for w in o[v]: +        i[w] -= 1 +        if i[w] == 0: +            q += (w,) +print((-(len(r) == n)))