#include<bits/stdc++.h>#definepiipair<int,int>#definexfirst#defineysecondusingnamespace std;constint N =10010, M = N *2;int e[M],h[N],ne[M],idx;int d[N];int n,m,res;intadd(int a,int b){
e[idx]= b, ne[idx]= h[a], h[a]= idx ++;}inttopsort(){int tt =0;int res =0, w =888;
queue<pii> q;for(int i =1; i <= n; i ++)if(!d[i]){
q.push({i,0});
res += w;}while(q.size()){
pii t = q.front();
q.pop();
tt ++;for(int i = h[t.x];~i ;i = ne[i]){int j = e[i];if(-- d[j]==0){
q.push({j,t.second+1});
res += w + t.second +1;}}}if(tt == n)return res;elsereturn-1;}intmain(){while(cin >> n >> m){
idx =0;memset(h,-1,sizeof h);memset(d,0,sizeof d);for(int i =1; i <= m; i ++){int a,b; cin >> a >> b ;add(b,a);
d[a]++;}
cout <<topsort()<< endl;}}