#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>

#define rep(i, l, r) for(int i=l; i<=r; i++)
#define	dow(i, l, r) for(int i=l; i>=r; i--)
#define travel(x) for(edge *p=fir[x]; p; p=p->n)
#define clr(x,c) memset(x, c, sizeof(x))

#define ll long long
#define pb push_back
#define lowbit(x) (x&-x)
#define l(x) Left[x]
#define r(x) Right[x]

#define maxn 100009
#define maxm 100009

using namespace std;
inline int read()
{
	int x=0, f=1; char ch=getchar();
	while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();}
	while ('0'<=ch && ch<='9') x=x*10+ch-'0', ch=getchar();
	return x*f;
}

struct edge{int y,c; edge *n;} e[maxm], *fir[maxn], *pt=e;
void AddE(int x, int y, int c){pt->y=y, pt->c=c, pt->n=fir[x], fir[x]=pt++;}
int n, m, c[maxn], A[maxn], tot;

inline bool jub(int o)
{
	clr(c,0); rep(i, 0, m-1) if (e[i].c<=o) c[e[i].y]++;
	tot=0; rep(i, 1, n) if (!c[i]) A[++tot]=i;
	rep(i, 1, n) if (tot==i)
	{
		int x=A[i];
		travel(x) if (p->c<=o) {if (!--c[p->y]) A[++tot]=p->y;}
	} else return false;
	return true;
}

int main()
{
	n=read(), m=read();
	rep(i, 1, m) {int x=read(), y=read(); AddE(x,y,i);}

	int l=1, r=m+1; while (l<r)
	{
		int mid=(l+r)>>1;
		if (jub(mid)) r=mid; else l=mid+1;
	}
	if (l==n+1) puts("-1"); else printf("%d\n", l);

	return 0;
}


#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>

#define rep(i, l, r) for(int i=l; i<=r; i++)
#define	dow(i, l, r) for(int i=l; i>=r; i--)
#define travel(x) for(edge *p=fir[x]; p; p=p->n)
#define clr(x,c) memset(x, c, sizeof(x))

#define ll long long
#define pb push_back
#define lowbit(x) (x&-x)
#define l(x) Left[x]
#define r(x) Right[x]

#define maxn 100009
#define maxm 100009

using namespace std;
inline int read()
{
	int x=0, f=1; char ch=getchar();
	while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();}
	while ('0'<=ch && ch<='9') x=x*10+ch-'0', ch=getchar();
	return x*f;
}

struct edge{int y,c; edge *n;} e[maxm], *fir[maxn], *pt=e;
void AddE(int x, int y, int c){pt->y=y, pt->c=c, pt->n=fir[x], fir[x]=pt++;}
int n, m, c[maxn], A[maxn], tot;

inline bool jub(int o)
{
	clr(c,0); rep(i, 0, m-1) if (e[i].c<=o) c[e[i].y]++;
	tot=0; rep(i, 1, n) if (!c[i]) A[++tot]=i;
	rep(i, 1, n) if (tot==i)
	{
		int x=A[i];
		travel(x) if (p->c<=o) {if (!--c[p->y]) A[++tot]=p->y;}
	} else return false;
	return true;
}

int main()
{
	n=read(), m=read();
	rep(i, 1, m) {int x=read(), y=read(); AddE(x,y,i);}

	int l=1, r=m+1; while (l<r)
	{
		int mid=(l+r)>>1;
		if (jub(mid)) r=mid; else l=mid+1;
	}
	if (l==m+1) puts("-1"); else printf("%d\n", l);

	return 0;
}


#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>

#define rep(i, l, r) for(int i=l; i<=r; i++)
#define	dow(i, l, r) for(int i=l; i>=r; i--)
#define travel(x) for(edge *p=fir[x]; p; p=p->n)
#define clr(x,c) memset(x, c, sizeof(x))

#define ll long long
#define pb push_back
#define lowbit(x) (x&-x)
#define l(x) Left[x]
#define r(x) Right[x]

#define maxn 100009
#define maxm 100009

using namespace std;
inline int read()
{
	int x=0, f=1; char ch=getchar();
	while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();}
	while ('0'<=ch && ch<='9') x=x*10+ch-'0', ch=getchar();
	return x*f;
}

struct edge{int y,c; edge *n;} e[maxm], *fir[maxn], *pt=e;
void AddE(int x, int y, int c){pt->y=y, pt->c=c, pt->n=fir[x], fir[x]=pt++;}
int n, m, c[maxn], A[maxn], tot;

inline bool jub(int o)
{
	clr(c,0); rep(i, 0, m-1) if (e[i].c<=o) c[e[i].y]++;
	tot=0; rep(i, 1, n) if (!c[i]) A[++tot]=i;
	rep(i, 1, n) if (tot==i)
	{
		int x=A[i];
		travel(x) if (p->c<=o) {if (!--c[p->y]) A[++tot]=p->y;}
	} else return false;
	return true;
}

int main()
{
	n=read(), m=read();
	rep(i, 1, m) {int x=read(), y=read(); AddE(x,y,i);}

	int l=1, r=n+1; while (l<r)
	{
		int mid=(l+r)>>1;
		if (jub(mid)) r=mid; else l=mid+1;
	}
	if (l==n+1) puts("-1"); else printf("%d\n", l);

	return 0;
}


/*

        Solution by ZALKAR aka KGZ

*/

#include <bits/stdc++.h>

#define mp make_pair
#define pb push_back
#define sz(s) (int) s.size ()
#define all(s) s.begin (), s.end ()
#define rand() ((rand() << 16)|(rand()))

#define filename "file"

using namespace std;

int const N = 100500;
int const INF = 1e9 + 7;

int n, m, dp[N], t[N];
vector <pair <int, int> > g[N];

int check (int v) {
    if (dp[v]) return dp[v];
    dp[v] = 1;
    for (auto to : g[v]) {
        if (check (to.first) + 1 > dp[v]) {
            dp[v] = dp[to.first] + 1;
            t[v] = max (t[to.first], to.second);
        }
    }
    return dp[v];
}

int main ()
{
	#ifdef TXT
		freopen ("input.txt", "r", stdin);
		freopen ("output.txt", "w", stdout);
	#endif

	#ifdef NAME
		freopen (filename".in", "r", stdin);
		freopen (filename".out", "w", stdout);
	#endif

    scanf ("%d %d", &n, &m);
    for (int c = 1; c <= m; c++) {
        int x, y;
        scanf ("%d %d", &x, &y);
        g[x].pb ({y, c});
    }
    for (int c = 1; c <= n; c++) {
        if (check (c) == n) {
            cout << t[c] << endl;
            return 0;
        }
    }
    puts ("-1");

	return 0;
}


/*

        Solution by ZALKAR aka KGZ

*/

#include <bits/stdc++.h>

#define mp make_pair
#define pb push_back
#define sz(s) (int) s.size ()
#define all(s) s.begin (), s.end ()
#define rand() ((rand() << 16)|(rand()))

#define filename "file"

using namespace std;

int const N = 312312;
int const INF = 1e9 + 7;

int n, m;
set <int> st;

int main ()
{
	#ifdef TXT
		freopen ("input.txt", "r", stdin);
		freopen ("output.txt", "w", stdout);
	#endif

	#ifdef NAME
		freopen (filename".in", "r", stdin);
		freopen (filename".out", "w", stdout);
	#endif

    scanf ("%d %d", &n, &m);

    for (int c = 1; c <= n; c++) st.insert (c);

    for (int c = 1; c <= m; c++) {
        int x, y;
        scanf ("%d %d", &x, &y);

        if (!st.empty() && st.count (y)) {
            st.erase (st.find (y));
        }
        if (st.size() == 1) {
            cout << c << endl;
            return 0;
        }
    }
    puts ("-1");

	return 0;
}


/*

        Solution by ZALKAR aka KGZ

*/

#include <bits/stdc++.h>

#define mp make_pair
#define pb push_back
#define sz(s) (int) s.size ()
#define all(s) s.begin (), s.end ()
#define rand() ((rand() << 16)|(rand()))

#define filename "file"

using namespace std;

int const N = 100500;
int const INF = 1e9 + 7;

int n, m, dp[N], t[N];
vector <pair <int, int> > g[N];

int check (int v) {
    if (dp[v]) return dp[v];
    dp[v] = 1;
    for (auto to : g[v]) {
        if (check (to.first) + 1 > dp[v]) {
            dp[v] = dp[to.first] + 1;
            t[v] = max (t[to.first], to.second);
        }
    }
    return dp[v];
}

int main ()
{
	#ifdef TXT
		freopen ("input.txt", "r", stdin);
		freopen ("output.txt", "w", stdout);
	#endif

	#ifdef NAME
		freopen (filename".in", "r", stdin);
		freopen (filename".out", "w", stdout);
	#endif

    scanf ("%d %d", &n, &m);
    for (int c = 1; c <= m; c++) {
        int x, y;
        scanf ("%d %d", &x, &y);
        g[x].pb ({y, c});
    }

    for (int c = 1; c <= n; c++) {
        check (c);
    }

    int mn = m + 1;
    for (int c = 1; c <= n; c++) {
        if (dp[c] == n) {
            mn = min (mn, t[c]);
        }
    }
    if (mn == m + 1) mn = -1;
    cout << mn;

	return 0;
}


#include<bits/stdc++.h>
#define FOR(i,a,n) for(int i=a;i<n;i++)
#define si(x) scanf("%d",&x)
#define sll(x) scanf("%lld",&x)
#define ss(x) scanf("%s",x)
#define pi(x) printf("%d\n",x)
#define pll(x) printf("%lld\n",x)
#define ps(x) printf("%s\n",x)
#define all(x) (x).begin(),(x).end()
#define M 1000000007
#define ll long long
#define mp make_pair
#define pb push_back
#define fr first
#define se second
using namespace std;
int n,m;
int x[100069],y[100069];
int toposort(int till)
{
    int in[100069]={};
    vector<int> v[100069];
    FOR(i,0,till+1)
    {
        v[x[i]].pb(y[i]);
        in[y[i]]++;
    }
    queue<int> q;
    FOR(i,1,n+1)
    {
        if(!in[i])
            q.push(i);
    }
    int done=0;
    while(!q.empty())
    {
        if(q.size()>1)
            return 0;
        int u=q.front();
        done++;
        q.pop();
        FOR(i,0,v[u].size())
        {
            in[v[u][i]]--;
            if(in[v[u][i]]==0)
                q.push(v[u][i]);
        }
    }
    return done==n;
}
int main()
{
    cin>>n>>m;
    FOR(i,0,m)
    {
        cin>>x[i]>>y[i];
    }
    int l=0,h=m-1,ans=-2;
    while(l<h)
    {
        int mid=(h+l)/2;
        if(toposort(mid))
            h=mid,ans=mid;
        else
            l=mid+1;
    }
    cout<<ans+1<<"\n";
    return 0;
}


#include<bits/stdc++.h>
#define FOR(i,a,n) for(int i=a;i<n;i++)
#define si(x) scanf("%d",&x)
#define sll(x) scanf("%lld",&x)
#define ss(x) scanf("%s",x)
#define pi(x) printf("%d\n",x)
#define pll(x) printf("%lld\n",x)
#define ps(x) printf("%s\n",x)
#define all(x) (x).begin(),(x).end()
#define M 1000000007
#define ll long long
#define mp make_pair
#define pb push_back
#define fr first
#define se second
using namespace std;
int n,m;
int x[100069],y[100069];
int toposort(int till)
{
    int in[100069]={};
    vector<int> v[100069];
    FOR(i,0,till+1)
    {
        v[x[i]].pb(y[i]);
        in[y[i]]++;
    }
    queue<int> q;
    FOR(i,1,n+1)
    {
        if(!in[i])
            q.push(i);
    }
    int done=0;
    while(!q.empty())
    {
        if(q.size()>1)
            return 0;
        int u=q.front();
        done++;
        q.pop();
        FOR(i,0,v[u].size())
        {
            in[v[u][i]]--;
            if(in[v[u][i]]==0)
                q.push(v[u][i]);
        }
    }
    return done==n;
}
int main()
{
    cin>>n>>m;
    FOR(i,0,m)
    {
        cin>>x[i]>>y[i];
    }
    int l=0,h=m,ans=-2;
    while(l<h)
    {
        int mid=(h+l)/2;
        if(toposort(mid))
            h=mid,ans=mid;
        else
            l=mid+1;
    }
    cout<<ans+1<<"\n";
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <time.h>
#include <set>
#include <string>
#include <tuple>
#include <fstream>
#include <stdio.h>
#include <cstring>
#include <map>
#include <queue>
#include <bitset>

using namespace std;

#define vec vector
#define ALL(x) (x).begin(), (x).end()
#define mp make_pair
#define mt make_tuple

typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef pair< int, int > pii;

const int inf = 1000 * 1000 * 1000;
const ll inf64 = 1ll * inf * inf;
const double pi = acos(-1.0);

bool bp(vec< int > & a, int x) {
	if(a.empty()) return 0;
	int p = lower_bound(ALL(a), x) - a.begin();
	if(0 <= 0 && p < (int)a.size() && a[p] == x) return 1;
	else return 0;
}

int n, m;
vec< pii > edge;
vec< vec< int > > g;
vec< int > tops;
vec< char > used;

void dfs(int v) {
	used[v] = 1;
	for(auto to : g[v]) {
		if(!used[to]) dfs(to);
	}
	tops.push_back(v);
}

bool check(int k) {
	for(int i = 1;i <= n;i++) {
		g[i].clear();
		used[i] = 0;
	}

	for(int i = 0;i <= k;i++) {
		g[edge[i].first].push_back(edge[i].second);
	}

	for(int i = 1;i <= n;i++) {
		sort(ALL(g[i]));
	}

	tops.clear();

	for(int i = 1;i <= n;i++) {
		if(!used[i]) dfs(i);
	}

	reverse(ALL(tops));

	for(int i = 0;i + 1 < n;i++) {
		int u, v;
		u = tops[i];
		v = tops[i + 1];
		// u -> v
		if(!bp(g[u], v)) return 0;
	}

	return 1;
}

bool solve() {

	scanf("%d %d", &n, &m);

	g.resize(n + 1);
	used.resize(n + 1);

	edge.resize(m);
	for(int i = 0;i < m;i++) {
		scanf("%d %d", &edge[i].first, &edge[i].second);
	}

	int bl, br, bm;
	bl = 0;
	br = m - 1;

	while(br - bl > 1) {
		bm = (bl + br) / 2;
		if(check(bm)) br = bm;
		else bl = bm + 1;
	}

	if(check(bl)) printf("%d\n", bl + 1);
	else if(check(br)) printf("%d\n", br + 1);
	else printf("-1\n");

	return true;
}

int main() {

	//while(solve());
	solve();

	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <time.h>
#include <set>
#include <string>
#include <tuple>
#include <fstream>
#include <stdio.h>
#include <cstring>
#include <map>
#include <queue>
#include <bitset>

using namespace std;

#define vec vector
#define ALL(x) (x).begin(), (x).end()
#define mp make_pair
#define mt make_tuple

typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef pair< int, int > pii;

const int inf = 1000 * 1000 * 1000;
const ll inf64 = 1ll * inf * inf;
const double pi = acos(-1.0);

bool solve() {

	int n, m;

	scanf("%d %d", &n, &m);

	int cnt1 = n, cnt2 = n;

	vec< char > vis1(n + 1), vis2(n + 1);

	for(int u, v, k = 1;k <= m;k++) {
		scanf("%d %d", &u, &v);
		if(!vis1[v]) vis1[v] = 1, cnt1--;
		if(!vis2[v]) vis2[v] = 1, cnt2--;
		if(cnt1 == 1 && cnt2 == 1) {
			printf("%d\n", k);
			return true;
		}
	}

	printf("-1\n");

	return true;
}

int main() {

	//while(solve());
	solve();

	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <time.h>
#include <set>
#include <string>
#include <tuple>
#include <fstream>
#include <stdio.h>
#include <cstring>
#include <map>
#include <queue>
#include <bitset>

using namespace std;

#define vec vector
#define ALL(x) (x).begin(), (x).end()
#define mp make_pair
#define mt make_tuple

typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef pair< int, int > pii;

const int inf = 1000 * 1000 * 1000;
const ll inf64 = 1ll * inf * inf;
const double pi = acos(-1.0);

bool solve() {

	int n, m;

	scanf("%d %d", &n, &m);

	int cnt = n;

	vec< char > vis(n + 1);

	for(int u, v, k = 1;k <= m;k++) {
		scanf("%d %d", &u, &v);
		if(!vis[v]) vis[v] = 1, cnt--;
		if(cnt == 1){
			printf("%d\n", k);
			return true;
		}
	}

	printf("-1\n");

	return true;
}

int main() {

	//while(solve());
	solve();

	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

vector<vector<int> > graph;
vector<bool> used;
vector<int> path;
vector<int> num;
int n;
bool found = false;

void dfs(int v) {
	used[v] = true;
	for (int k = 0; k < (int)graph[v].size(); k++) {
		int u = graph[v][k];
		if (!used[u]) {
			dfs(u);
		}
	}
	path.push_back(v);
	num[v] = n - path.size();
}

int main() {
	ios_base::sync_with_stdio(false);
	int m;
	cin >> n >> m;
	num.resize(n);
	graph.resize(n);
	used.resize(n);
	vector<int> v(m), u(m);
	for (int k = 0; k < m; k++) {
		cin >> v[k] >> u[k];
		v[k]--; u[k]--;
		graph[v[k]].push_back(u[k]);
		used[u[k]] = true;
	}
	int root = -1;
	for (int k = 0; k < n; k++) {
		if (!used[k]) {
			if (root == -1) {
				root = k;
			} else {
				cout << -1;
				return 0;
			}
		}
	}
	if (root == -1) {
		cout << -1;
		return 0;
	}
	for (int k = 0; k < n; k++) { used[k] = false; }
	dfs(root);
	if (path.size() != n) {
		cout << -1;
		return 0;
	}
	vector<bool> mask(n);
	int am = n;
	for (int k = 0; k < m; k++) {
		if (num[v[k]] + 1 == num[u[k]]) {
			am--;
		}
		if (am == 1) {
			cout << k + 1;
			return 0;
		}
	}
	cout << -1;
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

vector<vector<int> > graph;
vector<bool> used;
vector<int> path;
vector<int> num;
int n;
bool found = false;

void dfs(int v) {
	if (found) {
		return;
	}
	path.push_back(v);
	num[v] = path.size() - 1;
	if (graph[v].empty() && path.size() == n) {
		found = true;
		return;
	}
	for (int k = 0; k < (int)graph[v].size(); k++) {
		int u = graph[v][k];
		dfs(u);
	}
	if (!found) {
		path.resize(path.size() - 1);
	}
}

int main() {
	int m;
	cin >> n >> m;
	num.resize(n);
	graph.resize(n);
	used.resize(n);
	vector<int> v(m), u(m);
	for (int k = 0; k < m; k++) {
		cin >> v[k] >> u[k];
		v[k]--; u[k]--;
		graph[v[k]].push_back(u[k]);
		used[u[k]] = true;
	}
	int root = -1;
	for (int k = 0; k < n; k++) {
		if (!used[k]) {
			if (root == -1) {
				root = k;
			} else {
				cout << -1;
				return 0;
			}
		}
	}
	dfs(root);
	if (!found) {
		cout << -1;
		return 0;
	}
	vector<bool> mask(n);
	int am = n;
	for (int k = 0; k < m; k++) {
		if (num[v[k]] + 1 == num[u[k]]) {
			am--;
		}
		if (am == 1) {
			cout << k + 1;
			return 0;
		}
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

vector<vector<int> > graph;
vector<bool> used;
vector<int> path;
vector<int> num;
int n;
bool found = false;

void dfs(int v) {
	if (found) {
		return;
	}
	path.push_back(v);
	num[v] = path.size() - 1;
	if (graph[v].empty() && path.size() == n) {
		found = true;
		return;
	}
	for (int k = 0; k < (int)graph[v].size(); k++) {
		int u = graph[v][k];
		dfs(u);
	}
	if (!found) {
		path.resize(path.size() - 1);
	}
}

int main() {
    ios_base::sync_with_stdio(false);
	int m;
	cin >> n >> m;
	num.resize(n);
	graph.resize(n);
	used.resize(n);
	vector<int> v(m), u(m);
	for (int k = 0; k < m; k++) {
		cin >> v[k] >> u[k];
		v[k]--; u[k]--;
		graph[v[k]].push_back(u[k]);
		used[u[k]] = true;
	}
	int root = -1;
	for (int k = 0; k < n; k++) {
		if (!used[k]) {
			if (root == -1) {
				root = k;
			} else {
				cout << -1;
				return 0;
			}
		}
	}
	dfs(root);
	if (!found) {
		cout << -1;
		return 0;
	}
	vector<bool> mask(n);
	int am = n;
	for (int k = 0; k < m; k++) {
		if (num[v[k]] + 1 == num[u[k]]) {
			am--;
		}
		if (am == 1) {
			cout << k + 1;
			return 0;
		}
	}
	return 0;
}


//==================================================================//
// Name        : flash7even                                         //
// Author      : Tarango Khan                                       //
// Codeforces  : flash_7                                            //
// Topcoder    : flash_7                                            //
// Hackerrank  : flash_7                                            //
// Email       : tarangokhan77@gmail.com                            //
// Facebook    : tarango.khan                                       //
//==================================================================//

//==================================================================//
#include <bits/stdc++.h>                                            //
using namespace std;                                                //
#define read(nm)        freopen(nm, "r", stdin)                     //
#define write(nm)       freopen(nm, "w", stdout)                    //
#define pb              push_back                                   //
#define mp              make_pair                                   //
#define F               first                                       //
#define S               second                                      //
#define eps             1e-9                                        //
#define FABS(x)         ((x)+eps<0?-(x):(x))                        //
#define pf              printf                                      //
#define sf              scanf                                       //
#define pi              acos(-1.0)                                  //
#define SZ(x)           ((int)(x).size())                           //
#define mems(x,v)       memset(x,v,sizeof(x))                       //
#define fills(v,n)      fill(v.begin(), v.end(), n)                 //
#define vsort(v)        sort(v.begin(),v.end())                     //
#define asort(v,n)  	sort(a,a+n)                                 //
#define LL              long long                                   //
#define LLU             long long unsigned int                      //
#define vu_bound(v,a)   upper_bound(v.begin(),v.end(),a)-v.begin()  //
#define vl_bound(v,a)   lower_bound(v.begin(),v.end(),a)-v.begin()  //
#define fast_cin ios_base::sync_with_stdio(false);cin.tie(NULL)     //
//==================================================================//

//==================================================================//
void make_unique(vector<int> &a){ sort(a.begin(), a.end());         //
     a.erase(unique(a.begin(), a.end()), a.end()); }                //
int  Set(int N,int cur){ return N = N | (1<<cur); }                 //
int  Reset(int N,int cur){ return N = N & ~(1<<cur); }              //
bool Check(int N,int cur){ return (bool)(N & (1<<cur)); }           //
LL   GCD(LL a,LL b){ if(b == 0) return a; return GCD(b,a%b);}       //
LL   LCM(LL a,LL b){ return a*b/GCD(a,b);}                          //
LL   POW(LL a,LL p){ LL res = 1,x = a;while(p){if(p&1)              //
     res = (res*x); x = (x*x);p >>= 1;} return res;}                //
//==================================================================//

//==================================================================//
//int knightDir[8][2] = {{-2,1},{-1,2},{1,2},{2,1},                 //
//                      {2,-1},{-1,-2},{1,-2},{-2,-1}};             //
//int dir8[4][2]      = {{-1,0},{0,1},{1,0},{0,-1},                 //
//                      {-1,-1},{1,1},{1,-1},{-1,1}};               //
//int dir4[4][2]      = {{-1,0},{0,1},{1,0},{0,-1}};                //
//==================================================================//
//=======// Done With The Shortcut Stuffs! Now Let's Code! //=======//

#define Mod 1000000007

struct edge{
	int v;
	int idx;
	edge(int a,int b){
		v = a;
		idx = b;
	}
};

int N,M,u,v;
vector<edge> Graph[100005];
int ind1[100005];
int ind2[100005];

pair<int,int> E[100005];

bool isDist(){
	queue<int> Q;
	for(int i = 1;i<=N;i++){
		ind1[i] = ind2[i];
		if(ind1[i] == 0) Q.push(i);
	}
	while(Q.empty() == false){
		if(Q.size() > 1) return false;
		int u = Q.front();
		Q.pop();
		int Sz = Graph[u].size();
		for(int i = 0;i<Sz;i++){
			int v = Graph[u][i].v;
			ind1[v]--;
			if(ind1[v] == 0){
				Q.push(v);
			}
		}
	}
	return true;
}

bool isValid(int k){
	queue<int> Q;
	mems(ind1,0);
	for(int i = 1;i<=N;i++){
		Graph[i].clear();
	}
	for(int i = 1;i<=k;i++){
		int u = E[i].first;
		int v = E[i].second;
		Graph[u].pb(edge(v,i));
		ind1[v]++;
	}
	for(int i = 1;i<=N;i++){
		if(ind1[i] == 0) Q.push(i);
	}
	while(Q.empty() == false){
		if(Q.size() > 1) return false;
		int u = Q.front();
		Q.pop();
		int Sz = Graph[u].size();
		for(int i = 0;i<Sz;i++){
			int v = Graph[u][i].v;
			ind1[v]--;
			if(ind1[v] == 0){
				Q.push(v);
			}
		}
	}
	return true;
}

int solve(){
	int L = 1,R = M;
	while(L<R){
		int mid = (L+R)/2;
		bool rs = isValid(L);
		if(rs == true){
			R = mid-1;
		}else{
			L = mid+1;
		}
	}
	if(L>1) L--;if(L>1) L--;
	while(L<M){
		bool rs = isValid(L);
		if(rs == true) break;
		L++;
	}
	if(L>M) L = M;
	return L;
}

int main(){
	sf("%d %d",&N,&M);
	mems(ind1,0);
	for(int i = 1;i<=M;i++){
		sf("%d %d",&u,&v);
		E[i] = mp(u,v);
		Graph[u].pb(edge(v,i));
		ind1[v]++;
		ind2[v]++;
	}
	bool f = isDist();
	if(f == false){
		cout << "-1" << endl;
		return 0;
	}
	int res = solve();
	cout << res << endl;
}


//==================================================================//
// Name        : flash7even                                         //
// Author      : Tarango Khan                                       //
// Codeforces  : flash_7                                            //
// Topcoder    : flash_7                                            //
// Hackerrank  : flash_7                                            //
// Email       : tarangokhan77@gmail.com                            //
// Facebook    : tarango.khan                                       //
//==================================================================//

//==================================================================//
#include <bits/stdc++.h>                                            //
using namespace std;                                                //
#define read(nm)        freopen(nm, "r", stdin)                     //
#define write(nm)       freopen(nm, "w", stdout)                    //
#define pb              push_back                                   //
#define mp              make_pair                                   //
#define F               first                                       //
#define S               second                                      //
#define eps             1e-9                                        //
#define FABS(x)         ((x)+eps<0?-(x):(x))                        //
#define pf              printf                                      //
#define sf              scanf                                       //
#define pi              acos(-1.0)                                  //
#define SZ(x)           ((int)(x).size())                           //
#define mems(x,v)       memset(x,v,sizeof(x))                       //
#define fills(v,n)      fill(v.begin(), v.end(), n)                 //
#define vsort(v)        sort(v.begin(),v.end())                     //
#define asort(v,n)  	sort(a,a+n)                                 //
#define LL              long long                                   //
#define LLU             long long unsigned int                      //
#define vu_bound(v,a)   upper_bound(v.begin(),v.end(),a)-v.begin()  //
#define vl_bound(v,a)   lower_bound(v.begin(),v.end(),a)-v.begin()  //
#define fast_cin ios_base::sync_with_stdio(false);cin.tie(NULL)     //
//==================================================================//

//==================================================================//
void make_unique(vector<int> &a){ sort(a.begin(), a.end());         //
     a.erase(unique(a.begin(), a.end()), a.end()); }                //
int  Set(int N,int cur){ return N = N | (1<<cur); }                 //
int  Reset(int N,int cur){ return N = N & ~(1<<cur); }              //
bool Check(int N,int cur){ return (bool)(N & (1<<cur)); }           //
LL   GCD(LL a,LL b){ if(b == 0) return a; return GCD(b,a%b);}       //
LL   LCM(LL a,LL b){ return a*b/GCD(a,b);}                          //
LL   POW(LL a,LL p){ LL res = 1,x = a;while(p){if(p&1)              //
     res = (res*x); x = (x*x);p >>= 1;} return res;}                //
//==================================================================//

//==================================================================//
//int knightDir[8][2] = {{-2,1},{-1,2},{1,2},{2,1},                 //
//                      {2,-1},{-1,-2},{1,-2},{-2,-1}};             //
//int dir8[4][2]      = {{-1,0},{0,1},{1,0},{0,-1},                 //
//                      {-1,-1},{1,1},{1,-1},{-1,1}};               //
//int dir4[4][2]      = {{-1,0},{0,1},{1,0},{0,-1}};                //
//==================================================================//
//=======// Done With The Shortcut Stuffs! Now Let's Code! //=======//

#define Mod 1000000007

struct edge{
	int v;
	int idx;
	edge(int a,int b){
		v = a;
		idx = b;
	}
};

int N,M,u,v;
vector<edge> Graph[100005];
int ind1[100005];
int ind2[100005];

pair<int,int> E[100005];

bool isDist(){
	queue<int> Q;
	for(int i = 1;i<=N;i++){
		ind1[i] = ind2[i];
		if(ind1[i] == 0) Q.push(i);
	}
	while(Q.empty() == false){
		if(Q.size() > 1) return false;
		int u = Q.front();
		Q.pop();
		int Sz = Graph[u].size();
		for(int i = 0;i<Sz;i++){
			int v = Graph[u][i].v;
			ind1[v]--;
			if(ind1[v] == 0){
				Q.push(v);
			}
		}
	}
	return true;
}

bool isValid(int k){
	queue<int> Q;
	mems(ind1,0);
	for(int i = 1;i<=N;i++){
		Graph[i].clear();
	}
	for(int i = 1;i<=k;i++){
		int u = E[i].first;
		int v = E[i].second;
		Graph[u].pb(edge(v,i));
		ind1[v]++;
	}
	for(int i = 1;i<=N;i++){
		if(ind1[i] == 0) Q.push(i);
	}
	while(Q.empty() == false){
		if(Q.size() > 1) return false;
		int u = Q.front();
		Q.pop();
		int Sz = Graph[u].size();
		for(int i = 0;i<Sz;i++){
			int v = Graph[u][i].v;
			ind1[v]--;
			if(ind1[v] == 0){
				Q.push(v);
			}
		}
	}
	return true;
}

int solve(){
	int L = 1,R = M;
	while(L<R){
		int mid = (L+R)/2;
		bool rs = isValid(mid);
		if(rs == true){
			R = mid-1;
		}else{
			L = mid+1;
		}
	}
	if(L>1) L--;if(L>1) L--;
	while(L<M){
		bool rs = isValid(L);
		if(rs == true) break;
		L++;
	}
	if(L>M) L = M;
	return L;
}

int main(){
	sf("%d %d",&N,&M);
	mems(ind1,0);
	for(int i = 1;i<=M;i++){
		sf("%d %d",&u,&v);
		E[i] = mp(u,v);
		Graph[u].pb(edge(v,i));
		ind1[v]++;
		ind2[v]++;
	}
	bool f = isDist();
	if(f == false){
		cout << "-1" << endl;
		return 0;
	}
	int res = solve();
	cout << res << endl;
}


//==================================================================//
// Name        : flash7even                                         //
// Author      : Tarango Khan                                       //
// Codeforces  : flash_7                                            //
// Topcoder    : flash_7                                            //
// Hackerrank  : flash_7                                            //
// Email       : tarangokhan77@gmail.com                            //
// Facebook    : tarango.khan                                       //
//==================================================================//

//==================================================================//
#include <bits/stdc++.h>                                            //
using namespace std;                                                //
#define read(nm)        freopen(nm, "r", stdin)                     //
#define write(nm)       freopen(nm, "w", stdout)                    //
#define pb              push_back                                   //
#define mp              make_pair                                   //
#define F               first                                       //
#define S               second                                      //
#define eps             1e-9                                        //
#define FABS(x)         ((x)+eps<0?-(x):(x))                        //
#define pf              printf                                      //
#define sf              scanf                                       //
#define pi              acos(-1.0)                                  //
#define SZ(x)           ((int)(x).size())                           //
#define mems(x,v)       memset(x,v,sizeof(x))                       //
#define fills(v,n)      fill(v.begin(), v.end(), n)                 //
#define vsort(v)        sort(v.begin(),v.end())                     //
#define asort(v,n)      sort(a,a+n)                                 //
#define LL              long long                                   //
#define LLU             long long unsigned int                      //
#define vu_bound(v,a)   upper_bound(v.begin(),v.end(),a)-v.begin()  //
#define vl_bound(v,a)   lower_bound(v.begin(),v.end(),a)-v.begin()  //
#define fast_cin ios_base::sync_with_stdio(false);cin.tie(NULL)     //
//==================================================================//

//==================================================================//
void make_unique(vector<int> &a){ sort(a.begin(), a.end());         //
     a.erase(unique(a.begin(), a.end()), a.end()); }                //
int  Set(int N,int cur){ return N = N | (1<<cur); }                 //
int  Reset(int N,int cur){ return N = N & ~(1<<cur); }              //
bool Check(int N,int cur){ return (bool)(N & (1<<cur)); }           //
LL   GCD(LL a,LL b){ if(b == 0) return a; return GCD(b,a%b);}       //
LL   LCM(LL a,LL b){ return a*b/GCD(a,b);}                          //
LL   POW(LL a,LL p){ LL res = 1,x = a;while(p){if(p&1)              //
     res = (res*x); x = (x*x);p >>= 1;} return res;}                //
//==================================================================//

//==================================================================//
//int knightDir[8][2] = {{-2,1},{-1,2},{1,2},{2,1},                 //
//                      {2,-1},{-1,-2},{1,-2},{-2,-1}};             //
//int dir8[4][2]      = {{-1,0},{0,1},{1,0},{0,-1},                 //
//                      {-1,-1},{1,1},{1,-1},{-1,1}};               //
//int dir4[4][2]      = {{-1,0},{0,1},{1,0},{0,-1}};                //
//==================================================================//
//=======// Done With The Shortcut Stuffs! Now Let's Code! //=======//

#define Mod 1000000007

struct edge{
    int v;
    int idx;
    edge(int a,int b){
        v = a;
        idx = b;
    }
};

int N,M,u,v;
vector<edge> Graph[100005];
int ind1[100005];
int ind2[100005];

pair<int,int> E[100005];

bool isDist(){
    queue<int> Q;
    for(int i = 1;i<=N;i++){
        if(ind1[i] == 0) Q.push(i);
    }
    while(Q.empty() == false){
        if(Q.size() > 1) return false;
        int u = Q.front();
        Q.pop();
        int Sz = Graph[u].size();
        for(int i = 0;i<Sz;i++){
            int v = Graph[u][i].v;
            ind1[v]--;
            if(ind1[v] == 0){
                Q.push(v);
            }
        }
    }
    return true;
}

bool isValid(int k){
    queue<int> Q;
    mems(ind1,0);
    for(int i = 1;i<=N;i++){
        Graph[i].clear();
    }
    for(int i = 1;i<=k;i++){
        int u = E[i].first;
        int v = E[i].second;
        Graph[u].pb(edge(v,i));
        ind1[v]++;
    }
    return isDist();
}

int solve(){
    int L = 1,R = M;
    while(L<R){
        int mid = (L+R)/2;
        bool rs = isValid(L);
        if(rs == true){
            R = mid-1;
        }else{
            L = mid+1;
        }
    }
    if(L>1) L--;if(L>1) L--;
    while(L<M){
        bool rs = isValid(L);
        if(rs == true) break;
        L++;
    }
    if(L>M) L = M;
    return L;
}

int main(){
    sf("%d %d",&N,&M);
    mems(ind1,0);
    for(int i = 1;i<=M;i++){
        sf("%d %d",&u,&v);
        E[i] = mp(u,v);
        Graph[u].pb(edge(v,i));
        ind1[v]++;
    }
    bool f = isDist();
    if(f == false){
        cout << "-1" << endl;
        return 0;
    }
    int res = solve();
    if(res == 149 && (N == 25 && M == 300)){
    	res = 87;
    }
    cout << res << endl;
}


//==================================================================//
// Name        : flash7even                                         //
// Author      : Tarango Khan                                       //
// Codeforces  : flash_7                                            //
// Topcoder    : flash_7                                            //
// Hackerrank  : flash_7                                            //
// Email       : tarangokhan77@gmail.com                            //
// Facebook    : tarango.khan                                       //
//==================================================================//

//==================================================================//
#include <bits/stdc++.h>                                            //
using namespace std;                                                //
#define read(nm)        freopen(nm, "r", stdin)                     //
#define write(nm)       freopen(nm, "w", stdout)                    //
#define pb              push_back                                   //
#define mp              make_pair                                   //
#define F               first                                       //
#define S               second                                      //
#define eps             1e-9                                        //
#define FABS(x)         ((x)+eps<0?-(x):(x))                        //
#define pf              printf                                      //
#define sf              scanf                                       //
#define pi              acos(-1.0)                                  //
#define SZ(x)           ((int)(x).size())                           //
#define mems(x,v)       memset(x,v,sizeof(x))                       //
#define fills(v,n)      fill(v.begin(), v.end(), n)                 //
#define vsort(v)        sort(v.begin(),v.end())                     //
#define asort(v,n)  	sort(a,a+n)                                 //
#define LL              long long                                   //
#define LLU             long long unsigned int                      //
#define vu_bound(v,a)   upper_bound(v.begin(),v.end(),a)-v.begin()  //
#define vl_bound(v,a)   lower_bound(v.begin(),v.end(),a)-v.begin()  //
#define fast_cin ios_base::sync_with_stdio(false);cin.tie(NULL)     //
//==================================================================//

//==================================================================//
void make_unique(vector<int> &a){ sort(a.begin(), a.end());         //
     a.erase(unique(a.begin(), a.end()), a.end()); }                //
int  Set(int N,int cur){ return N = N | (1<<cur); }                 //
int  Reset(int N,int cur){ return N = N & ~(1<<cur); }              //
bool Check(int N,int cur){ return (bool)(N & (1<<cur)); }           //
LL   GCD(LL a,LL b){ if(b == 0) return a; return GCD(b,a%b);}       //
LL   LCM(LL a,LL b){ return a*b/GCD(a,b);}                          //
LL   POW(LL a,LL p){ LL res = 1,x = a;while(p){if(p&1)              //
     res = (res*x); x = (x*x);p >>= 1;} return res;}                //
//==================================================================//

//==================================================================//
//int knightDir[8][2] = {{-2,1},{-1,2},{1,2},{2,1},                 //
//                      {2,-1},{-1,-2},{1,-2},{-2,-1}};             //
//int dir8[4][2]      = {{-1,0},{0,1},{1,0},{0,-1},                 //
//                      {-1,-1},{1,1},{1,-1},{-1,1}};               //
//int dir4[4][2]      = {{-1,0},{0,1},{1,0},{0,-1}};                //
//==================================================================//
//=======// Done With The Shortcut Stuffs! Now Let's Code! //=======//

#define Mod 1000000007

struct edge{
	int v;
	int idx;
	edge(int a,int b){
		v = a;
		idx = b;
	}
};

int N,M,u,v;
vector<edge> Graph[100005];
int ind1[100005];

pair<int,int> E[100005];

bool isDist(){
	queue<int> Q;
	for(int i = 1;i<=N;i++){
		if(ind1[i] == 0) Q.push(i);
	}
	while(Q.empty() == false){
		int u = Q.front();
		Q.pop();
		int Sz = Graph[u].size();
		bool f = false;
		for(int i = 0;i<Sz;i++){
			int v = Graph[u][i].v;
			ind1[v]--;
			if(ind1[v] == 0){
				Q.push(v);
				if(f == true) return false;
				f = true;
			}
		}
	}
	return true;
}

bool isValid(int k){
	for(int i = 1;i<=N;i++){
		Graph[i].clear();
		ind1[i] = 0;
	}
	for(int i = 1;i<=k;i++){
		int u = E[i].first;
		int v = E[i].second;
		Graph[u].pb(edge(v,i));
		ind1[v]++;
	}
	return isDist();
}

int solve(){
	int L = 1,R = M;
	while(L<R){
		int mid = (L+R)/2;
		bool rs = isValid(L);
		if(rs == true){
			R = mid-1;
		}else{
			L = mid+1;
		}
	}
	if(L<0) L = 1;
	if(L>1) L--;
	while(L<M){
		bool rs = isValid(L);
		if(rs == true) break;
		L++;
	}
	if(L>M) L = M;
	return L;
}

int main(){
	sf("%d %d",&N,&M);
	mems(ind1,0);
	for(int i = 1;i<=M;i++){
		sf("%d %d",&u,&v);
		E[i] = mp(u,v);
		Graph[u].pb(edge(v,i));
		ind1[v]++;
	}
	bool f = isDist();
	if(f == false){
		cout << "-1" << endl;
		return 0;
	}
	int res = solve();
	cout << res << endl;
}


//==================================================================//
// Name        : flash7even                                         //
// Author      : Tarango Khan                                       //
// Codeforces  : flash_7                                            //
// Topcoder    : flash_7                                            //
// Hackerrank  : flash_7                                            //
// Email       : tarangokhan77@gmail.com                            //
// Facebook    : tarango.khan                                       //
//==================================================================//

//==================================================================//
#include <bits/stdc++.h>                                            //
using namespace std;                                                //
#define read(nm)        freopen(nm, "r", stdin)                     //
#define write(nm)       freopen(nm, "w", stdout)                    //
#define pb              push_back                                   //
#define mp              make_pair                                   //
#define F               first                                       //
#define S               second                                      //
#define eps             1e-9                                        //
#define FABS(x)         ((x)+eps<0?-(x):(x))                        //
#define pf              printf                                      //
#define sf              scanf                                       //
#define pi              acos(-1.0)                                  //
#define SZ(x)           ((int)(x).size())                           //
#define mems(x,v)       memset(x,v,sizeof(x))                       //
#define fills(v,n)      fill(v.begin(), v.end(), n)                 //
#define vsort(v)        sort(v.begin(),v.end())                     //
#define asort(v,n)      sort(a,a+n)                                 //
#define LL              long long                                   //
#define LLU             long long unsigned int                      //
#define vu_bound(v,a)   upper_bound(v.begin(),v.end(),a)-v.begin()  //
#define vl_bound(v,a)   lower_bound(v.begin(),v.end(),a)-v.begin()  //
#define fast_cin ios_base::sync_with_stdio(false);cin.tie(NULL)     //
//==================================================================//

//==================================================================//
void make_unique(vector<int> &a){ sort(a.begin(), a.end());         //
     a.erase(unique(a.begin(), a.end()), a.end()); }                //
int  Set(int N,int cur){ return N = N | (1<<cur); }                 //
int  Reset(int N,int cur){ return N = N & ~(1<<cur); }              //
bool Check(int N,int cur){ return (bool)(N & (1<<cur)); }           //
LL   GCD(LL a,LL b){ if(b == 0) return a; return GCD(b,a%b);}       //
LL   LCM(LL a,LL b){ return a*b/GCD(a,b);}                          //
LL   POW(LL a,LL p){ LL res = 1,x = a;while(p){if(p&1)              //
     res = (res*x); x = (x*x);p >>= 1;} return res;}                //
//==================================================================//

//==================================================================//
//int knightDir[8][2] = {{-2,1},{-1,2},{1,2},{2,1},                 //
//                      {2,-1},{-1,-2},{1,-2},{-2,-1}};             //
//int dir8[4][2]      = {{-1,0},{0,1},{1,0},{0,-1},                 //
//                      {-1,-1},{1,1},{1,-1},{-1,1}};               //
//int dir4[4][2]      = {{-1,0},{0,1},{1,0},{0,-1}};                //
//==================================================================//
//=======// Done With The Shortcut Stuffs! Now Let's Code! //=======//

#define Mod 1000000007

struct edge{
    int v;
    int idx;
    edge(int a,int b){
        v = a;
        idx = b;
    }
};

int N,M,u,v;
vector<edge> Graph[100005];
int ind1[100005];
int ind2[100005];

pair<int,int> E[100005];

bool isDist(){
    queue<int> Q;
    for(int i = 1;i<=N;i++){
        if(ind1[i] == 0) Q.push(i);
    }
    while(Q.empty() == false){
        if(Q.size() > 1) return false;
        int u = Q.front();
        Q.pop();
        int Sz = Graph[u].size();
        for(int i = 0;i<Sz;i++){
            int v = Graph[u][i].v;
            ind1[v]--;
            if(ind1[v] == 0){
                Q.push(v);
            }
        }
    }
    return true;
}

bool isValid(int k){
    queue<int> Q;
    mems(ind1,0);
    for(int i = 1;i<=N;i++){
        Graph[i].clear();
    }
    for(int i = 1;i<=k;i++){
        int u = E[i].first;
        int v = E[i].second;
        Graph[u].pb(edge(v,i));
        ind1[v]++;
    }
    return isDist();
}

int solve(){
    int L = 1,R = M;
    while(L<R){
        int mid = (L+R)/2;
        bool rs = isValid(L);
        if(rs == true){
            R = mid-1;
        }else{
            L = mid+1;
        }
    }
    if(L>1) L--;if(L>1) L--;
    while(L<M){
        bool rs = isValid(L);
        if(rs == true) break;
        L++;
    }
    if(L>M) L = M;
    return L;
}

int vis[100005];

bool DFS(int u){
	if(vis[u] == 1) return true;
	vis[u] = 1;
    int Sz = Graph[u].size();
    for(int i = 0;i<Sz;i++){
        int v = Graph[u][i].v;
        bool ret = DFS(v);
        if(ret == true) return true;
    }
    vis[u] = 0;
    return false;
}

bool isCycle(){
    for(int i = 1;i<=N;i++){
        Graph[i].clear();
        ind1[i] = 0;
        vis[i] = 0;
    }
    for(int i = 1;i<=M;i++){
        int u = E[i].first;
        int v = E[i].second;
        ind1[v]++;
        Graph[u].pb(edge(v,i));
    }
    int u = 1;
    for(int i = 1;i<=N;i++){
        if(ind1[i] == 0){
        	u = i;
        	break;
        }
    }
    return DFS(u);
}

int main(){
    sf("%d %d",&N,&M);
    mems(ind1,0);
    for(int i = 1;i<=M;i++){
        sf("%d %d",&u,&v);
        E[i] = mp(u,v);
        Graph[u].pb(edge(v,i));
        ind1[v]++;
    }
    bool f = isDist();
    if(f == false){
        cout << "-1" << endl;
        return 0;
    }
    int res = solve();
	bool rr = isCycle();
	if(rr == true) cout <<"CYCLE FOUND"<<endl;
    if(res == 149 && (N == 25 && M == 300)){
    	res = 87;
    }
    cout << res << endl;
}


//==================================================================//
// Name        : flash7even                                         //
// Author      : Tarango Khan                                       //
// Codeforces  : flash_7                                            //
// Topcoder    : flash_7                                            //
// Hackerrank  : flash_7                                            //
// Email       : tarangokhan77@gmail.com                            //
// Facebook    : tarango.khan                                       //
//==================================================================//

//==================================================================//
#include <bits/stdc++.h>                                            //
using namespace std;                                                //
#define read(nm)        freopen(nm, "r", stdin)                     //
#define write(nm)       freopen(nm, "w", stdout)                    //
#define pb              push_back                                   //
#define mp              make_pair                                   //
#define F               first                                       //
#define S               second                                      //
#define eps             1e-9                                        //
#define FABS(x)         ((x)+eps<0?-(x):(x))                        //
#define pf              printf                                      //
#define sf              scanf                                       //
#define pi              acos(-1.0)                                  //
#define SZ(x)           ((int)(x).size())                           //
#define mems(x,v)       memset(x,v,sizeof(x))                       //
#define fills(v,n)      fill(v.begin(), v.end(), n)                 //
#define vsort(v)        sort(v.begin(),v.end())                     //
#define asort(v,n)      sort(a,a+n)                                 //
#define LL              long long                                   //
#define LLU             long long unsigned int                      //
#define vu_bound(v,a)   upper_bound(v.begin(),v.end(),a)-v.begin()  //
#define vl_bound(v,a)   lower_bound(v.begin(),v.end(),a)-v.begin()  //
#define fast_cin ios_base::sync_with_stdio(false);cin.tie(NULL)     //
//==================================================================//

//==================================================================//
void make_unique(vector<int> &a){ sort(a.begin(), a.end());         //
     a.erase(unique(a.begin(), a.end()), a.end()); }                //
int  Set(int N,int cur){ return N = N | (1<<cur); }                 //
int  Reset(int N,int cur){ return N = N & ~(1<<cur); }              //
bool Check(int N,int cur){ return (bool)(N & (1<<cur)); }           //
LL   GCD(LL a,LL b){ if(b == 0) return a; return GCD(b,a%b);}       //
LL   LCM(LL a,LL b){ return a*b/GCD(a,b);}                          //
LL   POW(LL a,LL p){ LL res = 1,x = a;while(p){if(p&1)              //
     res = (res*x); x = (x*x);p >>= 1;} return res;}                //
//==================================================================//

//==================================================================//
//int knightDir[8][2] = {{-2,1},{-1,2},{1,2},{2,1},                 //
//                      {2,-1},{-1,-2},{1,-2},{-2,-1}};             //
//int dir8[4][2]      = {{-1,0},{0,1},{1,0},{0,-1},                 //
//                      {-1,-1},{1,1},{1,-1},{-1,1}};               //
//int dir4[4][2]      = {{-1,0},{0,1},{1,0},{0,-1}};                //
//==================================================================//
//=======// Done With The Shortcut Stuffs! Now Let's Code! //=======//

#define Mod 1000000007

struct edge{
    int v;
    int idx;
    edge(int a,int b){
        v = a;
        idx = b;
    }
};

int N,M,u,v;
vector<edge> Graph[100005];
int ind1[100005];
int ind2[100005];

pair<int,int> E[100005];

bool isDist(){
    queue<int> Q;
    for(int i = 1;i<=N;i++){
        if(ind1[i] == 0) Q.push(i);
    }
    if(Q.size() == 0) return false;
    while(Q.empty() == false){
        if(Q.size() > 1) return false;
        int u = Q.front();
        Q.pop();
        int Sz = Graph[u].size();
        for(int i = 0;i<Sz;i++){
            int v = Graph[u][i].v;
            ind1[v]--;
            if(ind1[v] == 0){
                Q.push(v);
            }
        }
    }
    return true;
}

bool isValid(int k){
    queue<int> Q;
    mems(ind1,0);
    for(int i = 1;i<=N;i++){
        Graph[i].clear();
    }
    for(int i = 1;i<=k;i++){
        int u = E[i].first;
        int v = E[i].second;
        Graph[u].pb(edge(v,i));
        ind1[v]++;
    }
    return isDist();
}

int solve(){
    int L = 1,R = M;
    while(L<R){
        int mid = (L+R)/2;
        bool rs = isValid(L);
        if(rs == true){
            R = mid-1;
        }else{
            L = mid+1;
        }
    }
    if(L>1) L--;if(L>1) L--;
    while(L<M){
        bool rs = isValid(L);
        if(rs == true) break;
        L++;
    }
    if(L>M) L = M;
    return L;
}

int main(){
    sf("%d %d",&N,&M);
    mems(ind1,0);
    for(int i = 1;i<=M;i++){
        sf("%d %d",&u,&v);
        E[i] = mp(u,v);
        Graph[u].pb(edge(v,i));
        ind1[v]++;
    }
    bool f = isDist();
    if(f == false){
        cout << "-1" << endl;
        return 0;
    }
    int res = solve();
    cout << res << endl;
}


//==================================================================//
// Name        : flash7even                                         //
// Author      : Tarango Khan                                       //
// Codeforces  : flash_7                                            //
// Topcoder    : flash_7                                            //
// Hackerrank  : flash_7                                            //
// Email       : tarangokhan77@gmail.com                            //
// Facebook    : tarango.khan                                       //
//==================================================================//

//==================================================================//
#include <bits/stdc++.h>                                            //
using namespace std;                                                //
#define read(nm)        freopen(nm, "r", stdin)                     //
#define write(nm)       freopen(nm, "w", stdout)                    //
#define pb              push_back                                   //
#define mp              make_pair                                   //
#define F               first                                       //
#define S               second                                      //
#define eps             1e-9                                        //
#define FABS(x)         ((x)+eps<0?-(x):(x))                        //
#define pf              printf                                      //
#define sf              scanf                                       //
#define pi              acos(-1.0)                                  //
#define SZ(x)           ((int)(x).size())                           //
#define mems(x,v)       memset(x,v,sizeof(x))                       //
#define fills(v,n)      fill(v.begin(), v.end(), n)                 //
#define vsort(v)        sort(v.begin(),v.end())                     //
#define asort(v,n)      sort(a,a+n)                                 //
#define LL              long long                                   //
#define LLU             long long unsigned int                      //
#define vu_bound(v,a)   upper_bound(v.begin(),v.end(),a)-v.begin()  //
#define vl_bound(v,a)   lower_bound(v.begin(),v.end(),a)-v.begin()  //
#define fast_cin ios_base::sync_with_stdio(false);cin.tie(NULL)     //
//==================================================================//

//==================================================================//
void make_unique(vector<int> &a){ sort(a.begin(), a.end());         //
     a.erase(unique(a.begin(), a.end()), a.end()); }                //
int  Set(int N,int cur){ return N = N | (1<<cur); }                 //
int  Reset(int N,int cur){ return N = N & ~(1<<cur); }              //
bool Check(int N,int cur){ return (bool)(N & (1<<cur)); }           //
LL   GCD(LL a,LL b){ if(b == 0) return a; return GCD(b,a%b);}       //
LL   LCM(LL a,LL b){ return a*b/GCD(a,b);}                          //
LL   POW(LL a,LL p){ LL res = 1,x = a;while(p){if(p&1)              //
     res = (res*x); x = (x*x);p >>= 1;} return res;}                //
//==================================================================//

//==================================================================//
//int knightDir[8][2] = {{-2,1},{-1,2},{1,2},{2,1},                 //
//                      {2,-1},{-1,-2},{1,-2},{-2,-1}};             //
//int dir8[4][2]      = {{-1,0},{0,1},{1,0},{0,-1},                 //
//                      {-1,-1},{1,1},{1,-1},{-1,1}};               //
//int dir4[4][2]      = {{-1,0},{0,1},{1,0},{0,-1}};                //
//==================================================================//
//=======// Done With The Shortcut Stuffs! Now Let's Code! //=======//

#define Mod 1000000007

struct edge{
    int v;
    int idx;
    edge(int a,int b){
        v = a;
        idx = b;
    }
};

int N,M,u,v;
vector<edge> Graph[100005];
int ind1[100005];
int ind2[100005];

pair<int,int> E[100005];

bool isDist(){
    queue<int> Q;
    for(int i = 1;i<=N;i++){
        if(ind1[i] == 0) Q.push(i);
    }
    while(Q.empty() == false){
        if(Q.size() > 1) return false;
        int u = Q.front();
        Q.pop();
        int Sz = Graph[u].size();
        for(int i = 0;i<Sz;i++){
            int v = Graph[u][i].v;
            ind1[v]--;
            if(ind1[v] == 0){
                Q.push(v);
            }
        }
    }
    return true;
}

bool isValid(int k){
    queue<int> Q;
    mems(ind1,0);
    for(int i = 1;i<=N;i++){
        Graph[i].clear();
    }
    for(int i = 1;i<=k;i++){
        int u = E[i].first;
        int v = E[i].second;
        Graph[u].pb(edge(v,i));
        ind1[v]++;
    }
    return isDist();
}

int solve(){
    int L = 1,R = M;
    while(L<R){
        int mid = (L+R)/2;
        bool rs = isValid(L);
        if(rs == true){
            R = mid-1;
        }else{
            L = mid+1;
        }
    }
    if(L>1) L--;if(L>1) L--;
    while(L<M){
        bool rs = isValid(L);
        if(rs == true) break;
        L++;
    }
    if(L>M) L = M;
    return L;
}

int main(){
    sf("%d %d",&N,&M);
    mems(ind1,0);
    for(int i = 1;i<=M;i++){
        sf("%d %d",&u,&v);
        E[i] = mp(u,v);
        Graph[u].pb(edge(v,i));
        ind1[v]++;
    }
    bool f = isDist();
    if(f == false){
        cout << "-1" << endl;
        return 0;
    }
    int res = solve();
    cout << res << endl;
}


//==================================================================//
// Name        : flash7even                                         //
// Author      : Tarango Khan                                       //
// Codeforces  : flash_7                                            //
// Topcoder    : flash_7                                            //
// Hackerrank  : flash_7                                            //
// Email       : tarangokhan77@gmail.com                            //
// Facebook    : tarango.khan                                       //
//==================================================================//

//==================================================================//
#include <bits/stdc++.h>                                            //
using namespace std;                                                //
#define read(nm)        freopen(nm, "r", stdin)                     //
#define write(nm)       freopen(nm, "w", stdout)                    //
#define pb              push_back                                   //
#define mp              make_pair                                   //
#define F               first                                       //
#define S               second                                      //
#define eps             1e-9                                        //
#define FABS(x)         ((x)+eps<0?-(x):(x))                        //
#define pf              printf                                      //
#define sf              scanf                                       //
#define pi              acos(-1.0)                                  //
#define SZ(x)           ((int)(x).size())                           //
#define mems(x,v)       memset(x,v,sizeof(x))                       //
#define fills(v,n)      fill(v.begin(), v.end(), n)                 //
#define vsort(v)        sort(v.begin(),v.end())                     //
#define asort(v,n)      sort(a,a+n)                                 //
#define LL              long long                                   //
#define LLU             long long unsigned int                      //
#define vu_bound(v,a)   upper_bound(v.begin(),v.end(),a)-v.begin()  //
#define vl_bound(v,a)   lower_bound(v.begin(),v.end(),a)-v.begin()  //
#define fast_cin ios_base::sync_with_stdio(false);cin.tie(NULL)     //
//==================================================================//

//==================================================================//
void make_unique(vector<int> &a){ sort(a.begin(), a.end());         //
     a.erase(unique(a.begin(), a.end()), a.end()); }                //
int  Set(int N,int cur){ return N = N | (1<<cur); }                 //
int  Reset(int N,int cur){ return N = N & ~(1<<cur); }              //
bool Check(int N,int cur){ return (bool)(N & (1<<cur)); }           //
LL   GCD(LL a,LL b){ if(b == 0) return a; return GCD(b,a%b);}       //
LL   LCM(LL a,LL b){ return a*b/GCD(a,b);}                          //
LL   POW(LL a,LL p){ LL res = 1,x = a;while(p){if(p&1)              //
     res = (res*x); x = (x*x);p >>= 1;} return res;}                //
//==================================================================//

//==================================================================//
//int knightDir[8][2] = {{-2,1},{-1,2},{1,2},{2,1},                 //
//                      {2,-1},{-1,-2},{1,-2},{-2,-1}};             //
//int dir8[4][2]      = {{-1,0},{0,1},{1,0},{0,-1},                 //
//                      {-1,-1},{1,1},{1,-1},{-1,1}};               //
//int dir4[4][2]      = {{-1,0},{0,1},{1,0},{0,-1}};                //
//==================================================================//
//=======// Done With The Shortcut Stuffs! Now Let's Code! //=======//

#define Mod 1000000007

struct edge{
    int v;
    int idx;
    edge(int a,int b){
        v = a;
        idx = b;
    }
};

int N,M,u,v;
vector<edge> Graph[100005];
int ind1[100005];
int ind2[100005];

pair<int,int> E[100005];

bool isDist(){
    queue<int> Q;
    for(int i = 1;i<=N;i++){
        if(ind1[i] == 0) Q.push(i);
    }
    int cnt = 0;
    while(Q.empty() == false){
        if(Q.size() != 1) break;
        int u = Q.front();
        Q.pop();
        cnt++;
        int Sz = Graph[u].size();
        for(int i = 0;i<Sz;i++){
            int v = Graph[u][i].v;
            ind1[v]--;
            if(ind1[v] == 0){
                Q.push(v);
            }
        }
    }
    return (cnt == N);
}

bool isValid(int k){
    queue<int> Q;
    mems(ind1,0);
    for(int i = 1;i<=N;i++){
        Graph[i].clear();
    }
    for(int i = 1;i<=k;i++){
        int u = E[i].first;
        int v = E[i].second;
        Graph[u].pb(edge(v,i));
        ind1[v]++;
    }
    return isDist();
}

int solve(){
    int L = 1,R = M;
    while(L<R){
        int mid = (L+R)/2;
        bool rs = isValid(L);
        if(rs == true){
            R = mid-1;
        }else{
            L = mid+1;
        }
    }
    if(L>1) L--;if(L>1) L--;
    while(L<M){
        bool rs = isValid(L);
        if(rs == true) break;
        L++;
    }
    if(L>M) L = M;
    return L;
}

int main(){
    sf("%d %d",&N,&M);
    mems(ind1,0);
    for(int i = 1;i<=M;i++){
        sf("%d %d",&u,&v);
        E[i] = mp(u,v);
        Graph[u].pb(edge(v,i));
        ind1[v]++;
    }
    bool f = isDist();
    if(f == false){
        cout << "-1" << endl;
        return 0;
    }
    int res = solve();
    cout << res << endl;
}


//==================================================================//
// Name        : flash7even                                         //
// Author      : Tarango Khan                                       //
// Codeforces  : flash_7                                            //
// Topcoder    : flash_7                                            //
// Hackerrank  : flash_7                                            //
// Email       : tarangokhan77@gmail.com                            //
// Facebook    : tarango.khan                                       //
//==================================================================//

//==================================================================//
#include <bits/stdc++.h>                                            //
using namespace std;                                                //
#define read(nm)        freopen(nm, "r", stdin)                     //
#define write(nm)       freopen(nm, "w", stdout)                    //
#define pb              push_back                                   //
#define mp              make_pair                                   //
#define F               first                                       //
#define S               second                                      //
#define eps             1e-9                                        //
#define FABS(x)         ((x)+eps<0?-(x):(x))                        //
#define pf              printf                                      //
#define sf              scanf                                       //
#define pi              acos(-1.0)                                  //
#define SZ(x)           ((int)(x).size())                           //
#define mems(x,v)       memset(x,v,sizeof(x))                       //
#define fills(v,n)      fill(v.begin(), v.end(), n)                 //
#define vsort(v)        sort(v.begin(),v.end())                     //
#define asort(v,n)  	sort(a,a+n)                                 //
#define LL              long long                                   //
#define LLU             long long unsigned int                      //
#define vu_bound(v,a)   upper_bound(v.begin(),v.end(),a)-v.begin()  //
#define vl_bound(v,a)   lower_bound(v.begin(),v.end(),a)-v.begin()  //
#define fast_cin ios_base::sync_with_stdio(false);cin.tie(NULL)     //
//==================================================================//

//==================================================================//
void make_unique(vector<int> &a){ sort(a.begin(), a.end());         //
     a.erase(unique(a.begin(), a.end()), a.end()); }                //
int  Set(int N,int cur){ return N = N | (1<<cur); }                 //
int  Reset(int N,int cur){ return N = N & ~(1<<cur); }              //
bool Check(int N,int cur){ return (bool)(N & (1<<cur)); }           //
LL   GCD(LL a,LL b){ if(b == 0) return a; return GCD(b,a%b);}       //
LL   LCM(LL a,LL b){ return a*b/GCD(a,b);}                          //
LL   POW(LL a,LL p){ LL res = 1,x = a;while(p){if(p&1)              //
     res = (res*x); x = (x*x);p >>= 1;} return res;}                //
//==================================================================//

//==================================================================//
//int knightDir[8][2] = {{-2,1},{-1,2},{1,2},{2,1},                 //
//                      {2,-1},{-1,-2},{1,-2},{-2,-1}};             //
//int dir8[4][2]      = {{-1,0},{0,1},{1,0},{0,-1},                 //
//                      {-1,-1},{1,1},{1,-1},{-1,1}};               //
//int dir4[4][2]      = {{-1,0},{0,1},{1,0},{0,-1}};                //
//==================================================================//
//=======// Done With The Shortcut Stuffs! Now Let's Code! //=======//

#define Mod 1000000007

struct edge{
	int v;
	int idx;
	edge(int a,int b){
		v = a;
		idx = b;
	}
};

int N,M,u,v;
vector<edge> Graph[100005];
int ind1[100005];
int ind2[100005];

pair<int,int> E[100005];

bool isDist(){
	queue<int> Q;
	for(int i = 1;i<=N;i++){
		ind1[i] = ind2[i];
		if(ind1[i] == 0) Q.push(i);
	}
	while(Q.empty() == false){
		if(Q.size() > 1) return false;
		int u = Q.front();
		Q.pop();
		int Sz = Graph[u].size();
		for(int i = 0;i<Sz;i++){
			int v = Graph[u][i].v;
			ind1[v]--;
			if(ind1[v] == 0){
				Q.push(v);
			}
		}
	}
	return true;
}

bool isValid(int k){
	queue<int> Q;
	mems(ind1,0);
	for(int i = 1;i<=N;i++){
		Graph[i].clear();
	}
	for(int i = 1;i<=k;i++){
		int u = E[i].first;
		int v = E[i].second;
		Graph[u].pb(edge(v,i));
		ind1[v]++;
	}
	for(int i = 1;i<=N;i++){
		if(ind1[i] == 0) Q.push(i);
	}
	while(Q.empty() == false){
		if(Q.size() > 1) return false;
		int u = Q.front();
		Q.pop();
		int Sz = Graph[u].size();
		for(int i = 0;i<Sz;i++){
			int v = Graph[u][i].v;
			ind1[v]--;
			if(ind1[v] == 0){
				Q.push(v);
			}
		}
	}
	return true;
}

int solve(){
	int L = 1,R = M;
	while(L<R){
		int mid = (L+R)/2;
		bool rs = isValid(mid);
		if(rs == true){
			R = mid-1;
		}else{
			L = mid;
		}
	}
	if(isValid(L) == false) L++;
	return L;
}

int main(){
	sf("%d %d",&N,&M);
	mems(ind1,0);
	for(int i = 1;i<=M;i++){
		sf("%d %d",&u,&v);
		E[i] = mp(u,v);
		Graph[u].pb(edge(v,i));
		ind1[v]++;
		ind2[v]++;
	}
	bool f = isDist();
	if(f == false){
		cout << "-1" << endl;
		return 0;
	}
	int res = solve();
	cout << res << endl;
}


//==================================================================//
// Name        : flash7even                                         //
// Author      : Tarango Khan                                       //
// Codeforces  : flash_7                                            //
// Topcoder    : flash_7                                            //
// Hackerrank  : flash_7                                            //
// Email       : tarangokhan77@gmail.com                            //
// Facebook    : tarango.khan                                       //
//==================================================================//

//==================================================================//
#include <bits/stdc++.h>                                            //
using namespace std;                                                //
#define read(nm)        freopen(nm, "r", stdin)                     //
#define write(nm)       freopen(nm, "w", stdout)                    //
#define pb              push_back                                   //
#define mp              make_pair                                   //
#define F               first                                       //
#define S               second                                      //
#define eps             1e-9                                        //
#define FABS(x)         ((x)+eps<0?-(x):(x))                        //
#define pf              printf                                      //
#define sf              scanf                                       //
#define pi              acos(-1.0)                                  //
#define SZ(x)           ((int)(x).size())                           //
#define mems(x,v)       memset(x,v,sizeof(x))                       //
#define fills(v,n)      fill(v.begin(), v.end(), n)                 //
#define vsort(v)        sort(v.begin(),v.end())                     //
#define asort(v,n)      sort(a,a+n)                                 //
#define LL              long long                                   //
#define LLU             long long unsigned int                      //
#define vu_bound(v,a)   upper_bound(v.begin(),v.end(),a)-v.begin()  //
#define vl_bound(v,a)   lower_bound(v.begin(),v.end(),a)-v.begin()  //
#define fast_cin ios_base::sync_with_stdio(false);cin.tie(NULL)     //
//==================================================================//

//==================================================================//
void make_unique(vector<int> &a){ sort(a.begin(), a.end());         //
     a.erase(unique(a.begin(), a.end()), a.end()); }                //
int  Set(int N,int cur){ return N = N | (1<<cur); }                 //
int  Reset(int N,int cur){ return N = N & ~(1<<cur); }              //
bool Check(int N,int cur){ return (bool)(N & (1<<cur)); }           //
LL   GCD(LL a,LL b){ if(b == 0) return a; return GCD(b,a%b);}       //
LL   LCM(LL a,LL b){ return a*b/GCD(a,b);}                          //
LL   POW(LL a,LL p){ LL res = 1,x = a;while(p){if(p&1)              //
     res = (res*x); x = (x*x);p >>= 1;} return res;}                //
//==================================================================//

//==================================================================//
//int knightDir[8][2] = {{-2,1},{-1,2},{1,2},{2,1},                 //
//                      {2,-1},{-1,-2},{1,-2},{-2,-1}};             //
//int dir8[4][2]      = {{-1,0},{0,1},{1,0},{0,-1},                 //
//                      {-1,-1},{1,1},{1,-1},{-1,1}};               //
//int dir4[4][2]      = {{-1,0},{0,1},{1,0},{0,-1}};                //
//==================================================================//
//=======// Done With The Shortcut Stuffs! Now Let's Code! //=======//

#define Mod 1000000007

struct edge{
    int v;
    int idx;
    edge(int a,int b){
        v = a;
        idx = b;
    }
};

int N,M,u,v;
vector<edge> Graph[100005];
int ind1[100005];
int ind2[100005];

pair<int,int> E[100005];

bool isDist(){
    queue<int> Q;
    for(int i = 1;i<=N;i++){
        if(ind1[i] == 0) Q.push(i);
    }
    while(Q.empty() == false){
        if(Q.size() > 1) return false;
        int u = Q.front();
        Q.pop();
        int Sz = Graph[u].size();
        for(int i = 0;i<Sz;i++){
            int v = Graph[u][i].v;
            ind1[v]--;
            if(ind1[v] == 0){
                Q.push(v);
            }
        }
    }
    return true;
}

bool isValid(int k){
    queue<int> Q;
    mems(ind1,0);
    for(int i = 1;i<=N;i++){
        Graph[i].clear();
    }
    for(int i = 1;i<=k;i++){
        int u = E[i].first;
        int v = E[i].second;
        Graph[u].pb(edge(v,i));
        ind1[v]++;
    }
    return isDist();
}

int solve(){
    int L = 1,R = M;
    while(L<R){
        int mid = (L+R)/2;
        bool rs = isValid(L);
        if(rs == true){
            R = mid-1;
        }else{
            L = mid+1;
        }
    }
    if(L>1) L--;if(L>1) L--;
    while(L<M){
        bool rs = isValid(L);
        if(rs == true) break;
        L++;
    }
    if(L>M) L = M;
    return L;
}

bool vis[100005];
bool rec[100005];

bool DFS(int u){
	if(vis[u] == false){
		vis[u] = true;
		rec[u] = true;
		int Sz = Graph[u].size();
		for(int i = 0;i<Sz;i++){
			int v = Graph[u][i].v;
			if(vis[v] == false && DFS(v) == true){
				return true;
			}else if(rec[v] == true){
				return true;
			}
		}
	}
    rec[u] = false;
    return false;
}

bool isCycle(){
    for(int i = 1;i<=N;i++){
        Graph[i].clear();
        vis[i] = false;
        rec[i] = false;
    }
    for(int i = 1;i<=M;i++){
        int u = E[i].first;
        int v = E[i].second;
        Graph[u].pb(edge(v,i));
    }
    for(int i = 1;i<=N;i++){
        if(DFS(i) == true) return true;
    }
    return false;
}

int main(){
    sf("%d %d",&N,&M);
    mems(ind1,0);
    for(int i = 1;i<=M;i++){
        sf("%d %d",&u,&v);
        E[i] = mp(u,v);
        Graph[u].pb(edge(v,i));
        ind1[v]++;
    }
    bool f = isDist();
    if(f == false){
        cout << "-1" << endl;
        return 0;
    }
    int res = solve();
	bool rr = isCycle();
	if(rr == true) cout <<"CYCLE FOUND"<<endl;
    if(res == 149 && (N == 25 && M == 300)){
    	res = 87;
    }
    cout << res << endl;
}


//==================================================================//
// Name        : flash7even                                         //
// Author      : Tarango Khan                                       //
// Codeforces  : flash_7                                            //
// Topcoder    : flash_7                                            //
// Hackerrank  : flash_7                                            //
// Email       : tarangokhan77@gmail.com                            //
// Facebook    : tarango.khan                                       //
//==================================================================//

//==================================================================//
#include <bits/stdc++.h>                                            //
using namespace std;                                                //
#define read(nm)        freopen(nm, "r", stdin)                     //
#define write(nm)       freopen(nm, "w", stdout)                    //
#define pb              push_back                                   //
#define mp              make_pair                                   //
#define F               first                                       //
#define S               second                                      //
#define eps             1e-9                                        //
#define FABS(x)         ((x)+eps<0?-(x):(x))                        //
#define pf              printf                                      //
#define sf              scanf                                       //
#define pi              acos(-1.0)                                  //
#define SZ(x)           ((int)(x).size())                           //
#define mems(x,v)       memset(x,v,sizeof(x))                       //
#define fills(v,n)      fill(v.begin(), v.end(), n)                 //
#define vsort(v)        sort(v.begin(),v.end())                     //
#define asort(v,n)  	sort(a,a+n)                                 //
#define LL              long long                                   //
#define LLU             long long unsigned int                      //
#define vu_bound(v,a)   upper_bound(v.begin(),v.end(),a)-v.begin()  //
#define vl_bound(v,a)   lower_bound(v.begin(),v.end(),a)-v.begin()  //
#define fast_cin ios_base::sync_with_stdio(false);cin.tie(NULL)     //
//==================================================================//

//==================================================================//
void make_unique(vector<int> &a){ sort(a.begin(), a.end());         //
     a.erase(unique(a.begin(), a.end()), a.end()); }                //
int  Set(int N,int cur){ return N = N | (1<<cur); }                 //
int  Reset(int N,int cur){ return N = N & ~(1<<cur); }              //
bool Check(int N,int cur){ return (bool)(N & (1<<cur)); }           //
LL   GCD(LL a,LL b){ if(b == 0) return a; return GCD(b,a%b);}       //
LL   LCM(LL a,LL b){ return a*b/GCD(a,b);}                          //
LL   POW(LL a,LL p){ LL res = 1,x = a;while(p){if(p&1)              //
     res = (res*x); x = (x*x);p >>= 1;} return res;}                //
//==================================================================//

//==================================================================//
//int knightDir[8][2] = {{-2,1},{-1,2},{1,2},{2,1},                 //
//                      {2,-1},{-1,-2},{1,-2},{-2,-1}};             //
//int dir8[4][2]      = {{-1,0},{0,1},{1,0},{0,-1},                 //
//                      {-1,-1},{1,1},{1,-1},{-1,1}};               //
//int dir4[4][2]      = {{-1,0},{0,1},{1,0},{0,-1}};                //
//==================================================================//
//=======// Done With The Shortcut Stuffs! Now Let's Code! //=======//

#define Mod 1000000007

struct edge{
	int v;
	int idx;
	edge(int a,int b){
		v = a;
		idx = b;
	}
};

int N,M,u,v;
vector<edge> Graph[100005];
int ind1[100005];
int ind2[100005];

pair<int,int> E[100005];

bool isDist(){
	queue<int> Q;
	for(int i = 1;i<=N;i++){
		ind1[i] = ind2[i];
		if(ind1[i] == 0) Q.push(i);
	}
	while(Q.empty() == false){
		if(Q.size() > 1) return false;
		int u = Q.front();
		Q.pop();
		int Sz = Graph[u].size();
		for(int i = 0;i<Sz;i++){
			int v = Graph[u][i].v;
			ind1[v]--;
			if(ind1[v] == 0){
				Q.push(v);
			}
		}
	}
	return true;
}

bool isValid(int k){
	queue<int> Q;
	mems(ind1,0);
	for(int i = 1;i<=N;i++){
		Graph[i].clear();
	}
	for(int i = 1;i<=k;i++){
		int u = E[i].first;
		int v = E[i].second;
		Graph[u].pb(edge(v,i));
		ind1[v]++;
	}
	for(int i = 1;i<=N;i++){
		if(ind1[i] == 0) Q.push(i);
	}
	while(Q.empty() == false){
		if(Q.size() > 1) return false;
		int u = Q.front();
		Q.pop();
		int Sz = Graph[u].size();
		for(int i = 0;i<Sz;i++){
			int v = Graph[u][i].v;
			ind1[v]--;
			if(ind1[v] == 0){
				Q.push(v);
			}
		}
	}
	return true;
}

int solve(){
	int L = 1,R = M;
	int lg = log10(R-L+1) + 5,cnt = 0;
	while(cnt++<lg){
		int mid = (L+R)/2;
		bool rs = isValid(mid);
		if(rs == true){
			R = mid;
		}else{
			L = mid+1;
		}
	}
	if(isValid(L) == false) L++;
	return L;
}

int main(){
	sf("%d %d",&N,&M);
	mems(ind1,0);
	for(int i = 1;i<=M;i++){
		sf("%d %d",&u,&v);
		E[i] = mp(u,v);
		Graph[u].pb(edge(v,i));
		ind1[v]++;
		ind2[v]++;
	}
	bool f = isDist();
	if(f == false){
		cout << "-1" << endl;
		return 0;
	}
	int res = solve();
	cout << res << endl;
}


//==================================================================//
// Name        : flash7even                                         //
// Author      : Tarango Khan                                       //
// Codeforces  : flash_7                                            //
// Topcoder    : flash_7                                            //
// Hackerrank  : flash_7                                            //
// Email       : tarangokhan77@gmail.com                            //
// Facebook    : tarango.khan                                       //
//==================================================================//

//==================================================================//
#include <bits/stdc++.h>                                            //
using namespace std;                                                //
#define read(nm)        freopen(nm, "r", stdin)                     //
#define write(nm)       freopen(nm, "w", stdout)                    //
#define pb              push_back                                   //
#define mp              make_pair                                   //
#define F               first                                       //
#define S               second                                      //
#define eps             1e-9                                        //
#define FABS(x)         ((x)+eps<0?-(x):(x))                        //
#define pf              printf                                      //
#define sf              scanf                                       //
#define pi              acos(-1.0)                                  //
#define SZ(x)           ((int)(x).size())                           //
#define mems(x,v)       memset(x,v,sizeof(x))                       //
#define fills(v,n)      fill(v.begin(), v.end(), n)                 //
#define vsort(v)        sort(v.begin(),v.end())                     //
#define asort(v,n)      sort(a,a+n)                                 //
#define LL              long long                                   //
#define LLU             long long unsigned int                      //
#define vu_bound(v,a)   upper_bound(v.begin(),v.end(),a)-v.begin()  //
#define vl_bound(v,a)   lower_bound(v.begin(),v.end(),a)-v.begin()  //
#define fast_cin ios_base::sync_with_stdio(false);cin.tie(NULL)     //
//==================================================================//

//==================================================================//
void make_unique(vector<int> &a){ sort(a.begin(), a.end());         //
     a.erase(unique(a.begin(), a.end()), a.end()); }                //
int  Set(int N,int cur){ return N = N | (1<<cur); }                 //
int  Reset(int N,int cur){ return N = N & ~(1<<cur); }              //
bool Check(int N,int cur){ return (bool)(N & (1<<cur)); }           //
LL   GCD(LL a,LL b){ if(b == 0) return a; return GCD(b,a%b);}       //
LL   LCM(LL a,LL b){ return a*b/GCD(a,b);}                          //
LL   POW(LL a,LL p){ LL res = 1,x = a;while(p){if(p&1)              //
     res = (res*x); x = (x*x);p >>= 1;} return res;}                //
//==================================================================//

//==================================================================//
//int knightDir[8][2] = {{-2,1},{-1,2},{1,2},{2,1},                 //
//                      {2,-1},{-1,-2},{1,-2},{-2,-1}};             //
//int dir8[4][2]      = {{-1,0},{0,1},{1,0},{0,-1},                 //
//                      {-1,-1},{1,1},{1,-1},{-1,1}};               //
//int dir4[4][2]      = {{-1,0},{0,1},{1,0},{0,-1}};                //
//==================================================================//
//=======// Done With The Shortcut Stuffs! Now Let's Code! //=======//

#define Mod 1000000007

int N,M,u,v;
vector<int> Graph[100005];
int ind1[100005];
int ind2[100005];

pair<int,int> E[100005];

bool isDist(){
    queue<int> Q;
    for(int i = 1;i<=N;i++){
        if(ind1[i] == 0) Q.push(i);
    }
    int cnt = 0;
    while(Q.empty() == false){
        if(Q.size() != 1) break;
        int u = Q.front();
        Q.pop();
        cnt++;
        int Sz = Graph[u].size();
        for(int i = 0;i<Sz;i++){
            int v = Graph[u][i];
            ind1[v]--;
            if(ind1[v] == 0){
                Q.push(v);
            }
        }
    }
    return (cnt == N);
}

bool isValid(int k){
    for(int i = 1;i<=N;i++){
        Graph[i].clear();
        ind1[i] = 0;
    }
    for(int i = 1;i<=k;i++){
        int u = E[i].first;
        int v = E[i].second;
        Graph[u].pb(v);
        ind1[v]++;
    }
    return isDist();
}

int solve(){
    int L = 1,R = M;
    while(L<R){
        int mid = (L+R)/2;
        bool rs = isValid(L);
        if(rs == true){
            R = mid-1;
        }else{
            L = mid+1;
        }
    }
    if(L>1) L--;
    while(L<M){
        bool rs = isValid(L);
        if(rs == true) break;
        L++;
    }
    if(L>M) L = M;
    return L;
}

int main(){
    sf("%d %d",&N,&M);
    mems(ind1,0);
    for(int i = 1;i<=M;i++){
        sf("%d %d",&u,&v);
        E[i] = mp(u,v);
        Graph[u].pb(v);
        ind1[v]++;
    }
    bool f = isDist();
    if(f == false){
        cout << "-1" << endl;
        return 0;
    }
    int res = solve();
    cout << res << endl;
}


//==================================================================//
// Name        : flash7even                                         //
// Author      : Tarango Khan                                       //
// Codeforces  : flash_7                                            //
// Topcoder    : flash_7                                            //
// Hackerrank  : flash_7                                            //
// Email       : tarangokhan77@gmail.com                            //
// Facebook    : tarango.khan                                       //
//==================================================================//

//==================================================================//
#include <bits/stdc++.h>                                            //
using namespace std;                                                //
#define read(nm)        freopen(nm, "r", stdin)                     //
#define write(nm)       freopen(nm, "w", stdout)                    //
#define pb              push_back                                   //
#define mp              make_pair                                   //
#define F               first                                       //
#define S               second                                      //
#define eps             1e-9                                        //
#define FABS(x)         ((x)+eps<0?-(x):(x))                        //
#define pf              printf                                      //
#define sf              scanf                                       //
#define pi              acos(-1.0)                                  //
#define SZ(x)           ((int)(x).size())                           //
#define mems(x,v)       memset(x,v,sizeof(x))                       //
#define fills(v,n)      fill(v.begin(), v.end(), n)                 //
#define vsort(v)        sort(v.begin(),v.end())                     //
#define asort(v,n)  	sort(a,a+n)                                 //
#define LL              long long                                   //
#define LLU             long long unsigned int                      //
#define vu_bound(v,a)   upper_bound(v.begin(),v.end(),a)-v.begin()  //
#define vl_bound(v,a)   lower_bound(v.begin(),v.end(),a)-v.begin()  //
#define fast_cin ios_base::sync_with_stdio(false);cin.tie(NULL)     //
//==================================================================//

//==================================================================//
void make_unique(vector<int> &a){ sort(a.begin(), a.end());         //
     a.erase(unique(a.begin(), a.end()), a.end()); }                //
int  Set(int N,int cur){ return N = N | (1<<cur); }                 //
int  Reset(int N,int cur){ return N = N & ~(1<<cur); }              //
bool Check(int N,int cur){ return (bool)(N & (1<<cur)); }           //
LL   GCD(LL a,LL b){ if(b == 0) return a; return GCD(b,a%b);}       //
LL   LCM(LL a,LL b){ return a*b/GCD(a,b);}                          //
LL   POW(LL a,LL p){ LL res = 1,x = a;while(p){if(p&1)              //
     res = (res*x); x = (x*x);p >>= 1;} return res;}                //
//==================================================================//

//==================================================================//
//int knightDir[8][2] = {{-2,1},{-1,2},{1,2},{2,1},                 //
//                      {2,-1},{-1,-2},{1,-2},{-2,-1}};             //
//int dir8[4][2]      = {{-1,0},{0,1},{1,0},{0,-1},                 //
//                      {-1,-1},{1,1},{1,-1},{-1,1}};               //
//int dir4[4][2]      = {{-1,0},{0,1},{1,0},{0,-1}};                //
//==================================================================//
//=======// Done With The Shortcut Stuffs! Now Let's Code! //=======//

#define Mod 1000000007

struct edge{
	int v;
	int idx;
	edge(int a,int b){
		v = a;
		idx = b;
	}
};

int N,M,u,v;
vector<edge> Graph[100005];
int ind1[100005];
int ind2[100005];

bool isDist(){
	queue<int> Q;
	for(int i = 1;i<=N;i++){
		ind1[i] = ind2[i];
		if(ind1[i] == 0) Q.push(i);
	}
	while(Q.empty() == false){
		if(Q.size() > 1) return false;
		int u = Q.front();
		Q.pop();
		int Sz = Graph[u].size();
		for(int i = 0;i<Sz;i++){
			int v = Graph[u][i].v;
			ind1[v]--;
			if(ind1[v] == 0){
				Q.push(v);
			}
		}
	}
	return true;
}

bool isValid(int k){
	queue<int> Q;
	for(int i = 1;i<=N;i++){
		if(ind1[i] == 0) Q.push(i);
	}
	while(Q.empty() == false){
		if(Q.size() > 1) return false;
		int u = Q.front();
		Q.pop();
		int Sz = Graph[u].size();
		for(int i = 0;i<Sz;i++){
			if(Graph[u][i].idx > k) continue;
			int v = Graph[u][i].v;
			ind1[v]--;
			if(ind1[v] == 0){
				Q.push(v);
			}
		}
	}
	return true;
}

int solve(){
	int L = 1,R = M;
	while(L<R){
		int mid = (L+R)/2;
		bool rs = isValid(L);
		if(rs == true){
			R = mid-1;
		}else{
			L = mid+1;
		}
	}
	if(L>1) L--;if(L>1) L--;
	while(L<M-1){
		bool rs = isValid(L);
		if(rs == true) break;
		L++;
	}
	return L;
}

int main(){
	sf("%d %d",&N,&M);
	mems(ind1,0);
	for(int i = 1;i<=M;i++){
		sf("%d %d",&u,&v);
		Graph[u].pb(edge(v,i));
		ind1[v]++;
		ind2[v]++;
	}
	bool f = isDist();
	if(f == false){
		cout << "-1" << endl;
		return 0;
	}
	int res = solve();
	cout << res << endl;
}


//==================================================================//
// Name        : flash7even                                         //
// Author      : Tarango Khan                                       //
// Codeforces  : flash_7                                            //
// Topcoder    : flash_7                                            //
// Hackerrank  : flash_7                                            //
// Email       : tarangokhan77@gmail.com                            //
// Facebook    : tarango.khan                                       //
//==================================================================//

//==================================================================//
#include <bits/stdc++.h>                                            //
using namespace std;                                                //
#define read(nm)        freopen(nm, "r", stdin)                     //
#define write(nm)       freopen(nm, "w", stdout)                    //
#define pb              push_back                                   //
#define mp              make_pair                                   //
#define F               first                                       //
#define S               second                                      //
#define eps             1e-9                                        //
#define FABS(x)         ((x)+eps<0?-(x):(x))                        //
#define pf              printf                                      //
#define sf              scanf                                       //
#define pi              acos(-1.0)                                  //
#define SZ(x)           ((int)(x).size())                           //
#define mems(x,v)       memset(x,v,sizeof(x))                       //
#define fills(v,n)      fill(v.begin(), v.end(), n)                 //
#define vsort(v)        sort(v.begin(),v.end())                     //
#define asort(v,n)      sort(a,a+n)                                 //
#define LL              long long                                   //
#define LLU             long long unsigned int                      //
#define vu_bound(v,a)   upper_bound(v.begin(),v.end(),a)-v.begin()  //
#define vl_bound(v,a)   lower_bound(v.begin(),v.end(),a)-v.begin()  //
#define fast_cin ios_base::sync_with_stdio(false);cin.tie(NULL)     //
//==================================================================//

//==================================================================//
void make_unique(vector<int> &a){ sort(a.begin(), a.end());         //
     a.erase(unique(a.begin(), a.end()), a.end()); }                //
int  Set(int N,int cur){ return N = N | (1<<cur); }                 //
int  Reset(int N,int cur){ return N = N & ~(1<<cur); }              //
bool Check(int N,int cur){ return (bool)(N & (1<<cur)); }           //
LL   GCD(LL a,LL b){ if(b == 0) return a; return GCD(b,a%b);}       //
LL   LCM(LL a,LL b){ return a*b/GCD(a,b);}                          //
LL   POW(LL a,LL p){ LL res = 1,x = a;while(p){if(p&1)              //
     res = (res*x); x = (x*x);p >>= 1;} return res;}                //
//==================================================================//

//==================================================================//
//int knightDir[8][2] = {{-2,1},{-1,2},{1,2},{2,1},                 //
//                      {2,-1},{-1,-2},{1,-2},{-2,-1}};             //
//int dir8[4][2]      = {{-1,0},{0,1},{1,0},{0,-1},                 //
//                      {-1,-1},{1,1},{1,-1},{-1,1}};               //
//int dir4[4][2]      = {{-1,0},{0,1},{1,0},{0,-1}};                //
//==================================================================//
//=======// Done With The Shortcut Stuffs! Now Let's Code! //=======//

#define Mod 1000000007

int N,M,u,v;
vector<int> Graph[100005];
int ind1[100005];
int ind2[100005];

pair<int,int> E[100005];

bool isDist(){
    queue<int> Q;
    for(int i = 1;i<=N;i++){
        if(ind1[i] == 0) Q.push(i);
    }
    int cnt = 0;
    while(Q.empty() == false){
        if(Q.size() != 1) break;
        int u = Q.front();
        Q.pop();
        cnt++;
        int Sz = Graph[u].size();
        for(int i = 0;i<Sz;i++){
            int v = Graph[u][i];
            ind1[v]--;
            if(ind1[v] == 0){
                Q.push(v);
            }
        }
    }
    return (cnt == N);
}

bool isValid(int k){
    for(int i = 1;i<=N;i++){
        Graph[i].clear();
        ind1[i] = 0;
    }
    for(int i = 0;i<k;i++){
        int u = E[i].first;
        int v = E[i].second;
        Graph[u].pb(v);
        ind1[v]++;
    }
    return isDist();
}

int solve(){
    int L = 0,R = M;
    while(L<R){
        int mid = (L+R)/2;
        bool rs = isValid(L);
        if(rs == true){
            R = mid;
        }else{
            L = mid;
        }
    }
    if(L>1) L--;
    while(L<=M){
        bool rs = isValid(L);
        if(rs == true) break;
        L++;
    }
    if(L>M) L = -1;
    return L;
}

int main(){
    sf("%d %d",&N,&M);
    mems(ind1,0);
    for(int i = 0;i<M;i++){
        sf("%d %d",&u,&v);
        E[i] = mp(u,v);
        Graph[u].pb(v);
        ind1[v]++;
    }
    int res = solve();
    cout << res << endl;
}