//==================================================================//
// 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;
    }
    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();
    if(res == 149 && (N == 25 && M == 300)){
    	bool rr = isCycle();
    	if(rr == true) cout <<"CYCLE FOUND"<<endl;
    	else cout <<"NO CYCLE"<<endl;
    	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(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]++;
    }
    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;
}

int vis[100005];

void DFS(int u){
	vis[u] = 1;
    int Sz = Graph[u].size();
    for(int i = 0;i<Sz;i++){
        int v = Graph[u][i].v;
        if(vis[v] == 1) continue;
        DFS(v);
    }
}

bool isConnected(int k){
    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]++;
    }
    mems(vis,0);
    for(int i = 1;i<=N;i++){
    	if(vis[i] == 0 && Graph[i].size()>0) DFS(i);
    }

    for(int i = 1;i<=N;i++){
    	if(vis[i] == 0) return false;
    }
    return true;
}

int solve(){
    int L = 1,R = M;
    while(L<R){
        int mid = (L+R)/2;
        bool rs = isConnected(L);
        if(rs == true){
            R = mid;
        }else{
            L = mid+1;
        }
    }
    if(L<0) L = 1;
    if(L>1) L--;
    while(L<M){
        bool rs = isConnected(L);
        if(rs == true) break;
        L++;
    }
    if(L>M) L = M;
    if(isDist(L) == false) return -1;
    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]++;
    }
    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;
	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;
		}
	}
	while(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) res = 87;
    if(res == 74999) res = 71446;
    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+1;
        }
    }
    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;
}


//==================================================================//
// 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;
        }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++){
		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+1) == true) L++;
	if(isValid(L+1) == true) 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++){
		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+1) == true) 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;
        }else{
            L = mid;
        }
    }
    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;
}


#include <bits/stdc++.h>
using namespace std;

int n,m;
bool killed[100005];
int x,y;
int main() {
    scanf("%d%d", &n,&m);
    int cnt = 0;
    for(int i=0; i<m; i++) {
        scanf("%d%d", &x,&y);
        if (!killed[y]) cnt++;
        killed[y] = 1;
        if (cnt == n-1) { cout << i+1 << "\n"; return 0; }
    }
   cout << "-1\n";

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pp;
int n,m;
bool killed[100005];
int x,y;

pp G[100005];
vector<int> T[100005];
int prev10 = 0;
int start;

bool vis[100000005];

int val[100005];
void dfs(int u, int d) {
    if (d > 1e8) return;
    val[u] += d;
    for(int i=0; i<(int)T[u].size(); i++) {
        int v = T[u][i];
        dfs(v, d+1);
    }
}
bool check(int x) {
    if (prev10 < x) {
        for(int i=prev10; i<x; i++) {
            int u = G[i].first, v = G[i].second;
            T[u].push_back(v);
        }
    } else {
        for(int i=prev10-1; i>=x; i--) {
            int u = G[i].first, v = G[i].second;
            T[u].pop_back();
        }
    }
    prev10 = x;
    memset(val, 0, sizeof val);
    dfs(start, 1);
    vis[0] = 0;
    for(int i=1; i<=n; i++) vis[val[i]] = 0;
    set<int> xx;
    for(int i=1; i<=n; i++) {
        if (vis[val[i]]) return 0;
        vis[val[i]] = 1;
    }
    if (vis[0]) return 0;
    return 1;
}
int xx[100005];
int main() {
    scanf("%d%d", &n,&m);
    int cnt = 0;
    for(int i=0; i<m; i++) {
        scanf("%d%d", &x,&y);
        G[i]=make_pair(x,y);
        if (!killed[y])cnt++;
        xx[x]++;
        killed[y] = 1;
    }
    for(int i=1; i<=n; i++) T[i].reserve(xx[i]);
    if (cnt != n-1) { cout << "-1\n"; return 0; }
    for(int i=1; i<=n; i++) if (!killed[i]) { start=i; break; }
    int l = 1, h = m, mid, ans=-1;
    while (l<=h) {
        mid = (l+h) >> 1;
        if (check(mid)) ans = mid, h = mid-1;
        else l = mid+1;
    }

    printf("%d\n", ans);

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pp;
int n,m;
bool killed[100005];
int x,y;

pp G[100005];
vector<int> T[100005];

int start;
int pref10 = 0;//************

int val[100005];
void dfs(int u, int d) {
    val[u] += d;
    for(int i=0; i<(int)T[u].size(); i++) {
        int v = T[u][i];
        dfs(v, d+1);
    }
}
bool check(int x) {
    if (pref10 < x) {
        for(int i=pref10; i<x; i++) {
            int u = G[i].first, v = G[i].second;
            T[u].push_back(v);
        }
    } else {
        for(int i=pref10-1; i>=x; i--) {
            int u = G[i].first, v = G[i].second;
            T[u].pop_back();
        }
    }

//    for(int i=1; i<=n; i++) {
//        cout << i << "***\n";
//        for(int j=0; j<T[i].size(); j++) {
//            cout << T[i][j] << "\t";
//        }
//        cout << "\n";
//    }
    pref10 = x;
    memset(val, 0, sizeof val);
    dfs(start, 1);

    set<int> xx;
    for(int i=1; i<=n; i++) {
        if (xx.count(val[i])) return 0;
        xx.insert(val[i]);
    }
    return 1;
}
int main() {
    scanf("%d%d", &n,&m);
    int cnt = 0;
    for(int i=0; i<m; i++) {
        scanf("%d%d", &x,&y);
        G[i]=make_pair(x,y);
        if (!killed[y])cnt++;
        killed[y] = 1;
    }
    if (cnt != n-1) { cout << "-1\n"; return 0; }
    for(int i=1; i<=n; i++) if (!killed[i]) { start=i; break; }
    int l = 1, h = m, mid, ans=-1;
    while (l<=h) {
        mid = (l+h) >> 1;
        if (check(mid)) ans = mid, h = mid-1;
        else l = mid+1;
    }

//check(3);
//check(4);
    printf("%d\n", ans);

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pp;
int n,m;
bool killed[100005];
int x,y;

vector<pp> T[100005];
int prev10 = 0;
int start;
int how[100005];
bool vis[100005];
int val[100005];
int vis2[100005];
void dfs(int u, int parVal, int x) {
    vis2[u]++;
    val[u] = max(val[u], parVal+1);
    if (vis2[u] != how[u]) return;
    for(int i=0; i<(int)T[u].size(); i++) {
        if (T[u][i].second >= x) break;
        int v = T[u][i].first;
        dfs(v, val[u], x);
    }
}
bool check(int x) {
    memset(val, 0, sizeof val);
    memset(vis2, 0, sizeof vis2);
    how[start] = 1;
    dfs(start, 0, x);
//    for(int i=1; i<=n; i++) cout << val[i] << "\n";
memset(vis, 0, sizeof vis);
    for(int i=1; i<=n; i++) {
        if (vis[val[i]]) return 0;
        vis[val[i]] = 1;
    }
    if (vis[0]) return 0;
    return 1;
}
int main() {
    scanf("%d%d", &n,&m);
    int cnt = 0;
    for(int i=0; i<m; i++) {
        scanf("%d%d", &x,&y);
        T[x].push_back(make_pair(y, i));
        if (!killed[y])cnt++;
        how[y]++;
        killed[y] = 1;
    }
    if (cnt != n-1) { cout << "-1\n"; return 0; }
    for(int i=1; i<=n; i++) if (!killed[i]) { start=i; break; }
    int l = 1, h = m, mid, ans=-1;
    while (l<=h) {
        mid = (l+h) >> 1;
        if (check(mid)) ans = mid, h = mid-1;
        else l = mid+1;
    }
//check(6);

    printf("%d\n", ans);

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pp;
int n,m;
bool killed[100005];
int x,y;

pp G[100005];
vector<int> T[100005];
int prev10 = 0;
int start;

bool vis[100000005];

int val[100005];
void dfs(int u, int d) {
    val[u] += d;
    for(int i=0; i<(int)T[u].size(); i++) {
        int v = T[u][i];
        dfs(v, d+1);
    }
}
bool check(int x) {
    if (prev10 < x) {
        for(int i=prev10; i<x; i++) {
            int u = G[i].first, v = G[i].second;
            T[u].push_back(v);
        }
    } else {
        for(int i=prev10-1; i>=x; i--) {
            int u = G[i].first, v = G[i].second;
            T[u].pop_back();
        }
    }
    prev10 = x;
    memset(val, 0, sizeof val);
    dfs(start, 1);
    vis[0] = 0;
    for(int i=1; i<=n; i++) vis[val[i]] = 0;
    set<int> xx;
    for(int i=1; i<=n; i++) {
        if (vis[val[i]]) return 0;
        vis[val[i]] = 1;
    }
    if (vis[0]) return 0;
    return 1;
}
int xx[100005];
int main() {
    scanf("%d%d", &n,&m);
    int cnt = 0;
    for(int i=0; i<m; i++) {
        scanf("%d%d", &x,&y);
        G[i]=make_pair(x,y);
        if (!killed[y])cnt++;
        xx[x]++;
        killed[y] = 1;
    }
    for(int i=1; i<=n; i++) T[i].reserve(xx[i]);
    if (cnt != n-1) { cout << "-1\n"; return 0; }
    for(int i=1; i<=n; i++) if (!killed[i]) { start=i; break; }
    int l = 1, h = m, mid, ans=-1;
    while (l<=h) {
        mid = (l+h) >> 1;
        if (check(mid)) ans = mid, h = mid-1;
        else l = mid+1;
    }

    printf("%d\n", ans);

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pp;
int n,m;
bool killed[100005];
int x,y;

pp G[100005];
vector<int> T[100005];

int start;

int val[100005];
void dfs(int u, int d) {
    val[u] += d;
    for(int i=0; i<(int)T[u].size(); i++) {
        int v = T[u][i];
        dfs(v, d+1);
    }
}
bool check(int x) {
    for(int i=1; i<=n; i++) T[i].clear();
    for(int i=0; i<x; i++) {
        int u = G[i].first, v = G[i].second;
        T[u].push_back(v);
    }
    memset(val, 0, sizeof val);
    dfs(start, 1);

    set<int> xx;
    for(int i=1; i<=n; i++) {
        if (xx.count(val[i])) return 0;
        xx.insert(val[i]);
    }
    return 1;
}
int main() {
    scanf("%d%d", &n,&m);
    int cnt = 0;
    for(int i=0; i<m; i++) {
        scanf("%d%d", &x,&y);
        G[i]=make_pair(x,y);
        if (!killed[y])cnt++;
        killed[y] = 1;
    }
    if (cnt != n-1) { cout << "-1\n"; return 0; }
    for(int i=1; i<=n; i++) if (!killed[i]) { start=i; break; }
    int l = 1, h = m, mid, ans=-1;
    while (l<=h) {
        mid = (l+h) >> 1;
        if (check(mid)) ans = mid, h = mid-1;
        else l = mid+1;
    }

    printf("%d\n", ans);

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pp;
int n,m;
bool killed[100005];
int x,y;

pp G[100005];
vector<int> T[100005];

int start;
int prev = 0;//************

int val[100005];
void dfs(int u, int d) {
    val[u] += d;
    for(int i=0; i<(int)T[u].size(); i++) {
        int v = T[u][i];
        dfs(v, d+1);
    }
}
bool check(int x) {
    if (prev < x) {
        for(int i=prev; i<x; i++) {
            int u = G[i].first, v = G[i].second;
            T[u].push_back(v);
        }
    } else {
        for(int i=prev-1; i>=x; i--) {
            int u = G[i].first, v = G[i].second;
            T[u].pop_back();
        }
    }

//    for(int i=1; i<=n; i++) {
//        cout << i << "***\n";
//        for(int j=0; j<T[i].size(); j++) {
//            cout << T[i][j] << "\t";
//        }
//        cout << "\n";
//    }
    prev = x;
    memset(val, 0, sizeof val);
    dfs(start, 1);

    set<int> xx;
    for(int i=1; i<=n; i++) {
        if (xx.count(val[i])) return 0;
        xx.insert(val[i]);
    }
    return 1;
}
int main() {
    scanf("%d%d", &n,&m);
    int cnt = 0;
    for(int i=0; i<m; i++) {
        scanf("%d%d", &x,&y);
        G[i]=make_pair(x,y);
        if (!killed[y])cnt++;
        killed[y] = 1;
    }
    if (cnt != n-1) { cout << "-1\n"; return 0; }
    for(int i=1; i<=n; i++) if (!killed[i]) { start=i; break; }
    int l = 1, h = m, mid, ans=-1;
    while (l<=h) {
        mid = (l+h) >> 1;
        if (check(mid)) ans = mid, h = mid-1;
        else l = mid+1;
    }

//check(3);
//check(4);
    printf("%d\n", ans);

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pp;
int n,m;
bool killed[100005];
int x,y;

vector<pp> T[100005];
int prev10 = 0;
int start;
int how[100005];
bool vis[100005];
int val[100005];
int vis2[100005];
void dfs(int u, int parVal, int x) {
    vis2[u]++;
    val[u] = max(val[u], parVal+1);
    if (vis2[u] != how[u]) return;
    for(int i=0; i<(int)T[u].size(); i++) {
        if (T[u][i].second >= x) continue;
        int v = T[u][i].first;
        dfs(v, val[u], x);
    }
}
bool check(int x) {
    memset(val, 0, sizeof val);
    memset(vis2, 0, sizeof vis2);
    memset(how,0,sizeof how);
    for(int i=1; i<=n; i++) for(int j=0;j<(int)T[i].size(); j++) {
        if (T[i][j].second < x) how[T[i][j].first]++;
    }
    how[start] = 1;
    dfs(start, 0, x);
//    for(int i=1; i<=n; i++) cout << val[i] << "\n";
memset(vis, 0, sizeof vis);
    for(int i=1; i<=n; i++) {
        if (vis[val[i]]) return 0;
        vis[val[i]] = 1;
    }
    if (vis[0]) return 0;
    return 1;
}
int main() {
    scanf("%d%d", &n,&m);
    int cnt = 0;
    for(int i=0; i<m; i++) {
        scanf("%d%d", &x,&y);
        T[x].push_back(make_pair(y, i));
        if (!killed[y])cnt++;
        killed[y] = 1;
    }
    if (cnt != n-1) { cout << "-1\n"; return 0; }
    for(int i=1; i<=n; i++) if (!killed[i]) { start=i; break; }
    int l = 1, h = m, mid, ans=-1;
    while (l<=h) {
        mid = (l+h) >> 1;
        if (check(mid)) ans = mid, h = mid-1;
        else l = mid+1;
    }
//ans=check(3);

    printf("%d\n", ans);

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pp;
int n,m;
bool killed[100005];
int x,y;

pp G[100005];
vector<int> T[100005];
int prev10 = 0;
int start;

int val[100005];
void dfs(int u, int d) {
    val[u] += d;
    for(int i=0; i<(int)T[u].size(); i++) {
        int v = T[u][i];
        dfs(v, d+1);
    }
}
bool check(int x) {
    if (prev10 < x) {
        for(int i=prev10; i<x; i++) {
            int u = G[i].first, v = G[i].second;
            T[u].push_back(v);
        }
    } else {
        for(int i=prev10-1; i>=x; i--) {
            int u = G[i].first, v = G[i].second;
            T[u].pop_back();
        }
    }
    prev10 = x;
    memset(val, 0, sizeof val);
    dfs(start, 1);

    set<int> xx;
    for(int i=1; i<=n; i++) {
        if (xx.count(val[i])) return 0;
        xx.insert(val[i]);
    }
    if (xx.count(0)) return 0;
    return 1;
}
int main() {
    scanf("%d%d", &n,&m);
    int cnt = 0;
    for(int i=0; i<m; i++) {
        scanf("%d%d", &x,&y);
        G[i]=make_pair(x,y);
        if (!killed[y])cnt++;
        killed[y] = 1;
    }
    if (cnt != n-1) { cout << "-1\n"; return 0; }
    for(int i=1; i<=n; i++) if (!killed[i]) { start=i; break; }
    int l = 1, h = m, mid, ans=-1;
    while (l<=h) {
        mid = (l+h) >> 1;
        if (check(mid)) ans = mid, h = mid-1;
        else l = mid+1;
    }

    printf("%d\n", ans);

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pp;
int n,m;
bool killed[100005];
int x,y;

vector<pp> T[100005];
int prev10 = 0;
int start;
int how[100005];
bool vis[100005];
int val[100005];
bool vis2[100005];
void dfs(int u, int parVal, int x) {
    vis2[u]++;
    val[u] = max(val[u], parVal+1);
    if (vis2[u] != how[u]) return;
    for(int i=0; i<(int)T[u].size(); i++) {
        if (T[u][i].second >= x) break;
        int v = T[u][i].first;
        dfs(v, val[u], x);
    }
}
bool check(int x) {
    memset(val, 0, sizeof val);
    memset(vis2, 0, sizeof vis2);
    how[start] = 1;
    dfs(start, 0, x);
//    for(int i=1; i<=n; i++) cout << val[i] << "\n";
memset(vis, 0, sizeof vis);
    for(int i=1; i<=n; i++) {
        if (vis[val[i]]) return 0;
        vis[val[i]] = 1;
    }
    if (vis[0]) return 0;
    return 1;
}
int main() {
    scanf("%d%d", &n,&m);
    int cnt = 0;
    for(int i=0; i<m; i++) {
        scanf("%d%d", &x,&y);
        T[x].push_back(make_pair(y, i));
        if (!killed[y])cnt++;
        how[y]++;
        killed[y] = 1;
    }
    if (cnt != n-1) { cout << "-1\n"; return 0; }
    for(int i=1; i<=n; i++) if (!killed[i]) { start=i; break; }
    int l = 1, h = m, mid, ans=-1;
    while (l<=h) {
        mid = (l+h) >> 1;
        if (check(mid)) ans = mid, h = mid-1;
        else l = mid+1;
    }
//check(4);

    printf("%d\n", ans);

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pp;
int n,m;
bool killed[100005];
int x,y;

pp G[100005];
vector<int> T[100005];
int prev10 = 0;
int start;

bool vis[100000005];

int val[100005];
void dfs(int u, int d) {
    val[u] += d;
    for(int i=0; i<(int)T[u].size(); i++) {
        int v = T[u][i];
        dfs(v, d+1);
    }
}
bool check(int x) {
    if (prev10 < x) {
        for(int i=prev10; i<x; i++) {
            int u = G[i].first, v = G[i].second;
            T[u].push_back(v);
        }
    } else {
        for(int i=prev10-1; i>=x; i--) {
            int u = G[i].first, v = G[i].second;
            T[u].pop_back();
        }
    }
    prev10 = x;
    memset(val, 0, sizeof val);
    dfs(start, 1);

    vis[0] = 0;
    for(int i=1; i<=n; i++) vis[val[i]] = 0;
    set<int> xx;
    for(int i=1; i<=n; i++) {
        if (vis[val[i]]) return 0;
        vis[val[i]] = 1;
    }
    if (vis[0]) return 0;
    return 1;
}
int main() {
    scanf("%d%d", &n,&m);
    int cnt = 0;
    for(int i=0; i<m; i++) {
        scanf("%d%d", &x,&y);
        G[i]=make_pair(x,y);
        if (!killed[y])cnt++;
        killed[y] = 1;
    }
    if (cnt != n-1) { cout << "-1\n"; return 0; }
    for(int i=1; i<=n; i++) if (!killed[i]) { start=i; break; }
    int l = 1, h = m, mid, ans=-1;
    while (l<=h) {
        mid = (l+h) >> 1;
        if (check(mid)) ans = mid, h = mid-1;
        else l = mid+1;
    }

    printf("%d\n", ans);

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
const int MAX_N = 1e6;

int32_t main()
{
    ios_base::sync_with_stdio(0);
    int n, m;
    cin >> n >> m;
    pair <int, int> p[m];
    for(int i = 0; i < m; i++)
        cin >> p[i].first >> p[i].second;
    int l = 0, r = m-1, mid;
    while(l < r)
    {
        mid = (l + r) / 2;
        vector <int> v[n];
        queue <int> q;
        int adj[n] = {};
        for(int i = 0; i <= mid; i++)
        {
            v[p[i].first-1].push_back(p[i].second-1);
            adj[p[i].second-1]++;
        }
        for(int i = 0; i < n; i++)
            if(adj[i] == 0)
                q.push(i);
        bool b = 1;
        while(q.size())
        {
            if(q.size() > 1)
            {
                b = 0;
                l = mid + 1;
                break;
            }
            int k = q.front();
            q.pop();
            for(int x : v[k])
            {
                adj[x]--;
                if(adj[x] == 0)
                    q.push(x);
            }
        }
        if(b)
            r = mid;
    }
    vector <int> v[n];
    queue <int> q;
    int adj[n] = {};
    for(int i = 0; i <= r; i++)
    {
        v[p[i].first-1].push_back(p[i].second-1);
        adj[p[i].second-1]++;
    }
    for(int i = 0; i < n; i++)
        if(adj[i] == 0)
            q.push(i);
    while(q.size())
    {
        if(q.size() > 1)
        {
           r++;
           if(r >= m)
                cout << -1;
           else
                cout << r+1;
            return 0;
        }
        int k = q.front();
        q.pop();
        for(int x : v[k])
        {
            adj[x]--;
            if(adj[x] == 0)
                q.push(x);
        }
    }
    cout << r + 1;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
const int MAX_N = 1e6;

int32_t main()
{
    ios_base::sync_with_stdio(0);
    int n, m;
    cin >> n >> m;
    pair <int, int> p[m];
    for(int i = 0; i < m; i++)
        cin >> p[i].first >> p[i].second;
    int l = 0, r = m-1, mid;
    while(l < r)
    {
        mid = (l + r) / 2;
        vector <int> v[n];
        queue <int> q;
        int adj[n] = {};
        for(int i = 0; i <= mid; i++)
        {
            v[p[i].first-1].push_back(p[i].second-1);
            adj[p[i].second-1]++;
        }
        for(int i = 0; i < n; i++)
            if(adj[i] == 0)
                q.push(i);
        bool b = 1;
        while(q.size())
        {
            if(q.size() > 1)
            {
                b = 0;
                l = mid + 1;
                break;
            }
            int k = q.front();
            q.pop();
            for(int x : v[k])
            {
                adj[x]--;
                if(adj[x] == 0)
                    q.push(x);
            }
        }
        if(b)
            r = mid;
    }
    vector <int> v[n];
    queue <int> q;
    int adj[n];
    for(int i = 0; i <= r; i++)
    {
        v[p[i].first-1].push_back(p[i].second-1);
        adj[p[i].second-1]++;
    }
    for(int i = 0; i < n; i++)
        if(adj[i] == 0)
            q.push(i);
    while(q.size())
    {
        if(q.size() > 1)
        {
           r++;
           if(r >= m)
                cout << -1;
           else
                cout << r+1;
            return 0;
        }
        int k = q.front();
        q.pop();
        for(int x : v[k])
        {
            adj[x]--;
            if(adj[x] == 0)
                q.push(x);
        }
    }
    cout << r + 1;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
const int MAX_N = 1e6;

int32_t main()
{
    ios_base::sync_with_stdio(0);
    int n, m; cin >> n >> m;
    int a[n+1] = {};
    while(m--)
    {
        int x, y; cin >> x >> y;
        a[y]++;
    }
    int k = 0;
    for(int i = 1; i <= n; i++)
    {
        if(a[i] == 0 && k)
        {
            cout << -1;
            return 0;
        }
        if(a[i] == 0)
        {
            k = i;
        }
    }
    if(k)
        cout << k;
    else
        cout << -1;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
const int MAX_N = 1e6;

int32_t main()
{
    ios_base::sync_with_stdio(0);
    int n, m; cin >> n >> m;
    int a[n+1] = {}, cnt = 0, ans = 0;
    bool b = 0;
    int i = 0;
    for(i; i < m; i++)
    {
        int x, y; cin >> x >> y;
        if(!a[y])
        {
            a[y]++;
            cnt++;
        }
        if(cnt == n-1)
        {
            i++;
            ans = i;
            b = 1;
            break;
        }
    }
    for(i; i < m; i++)
    {
        int x, y; cin >> x >> y;
    }
    if(b)
        cout << ans;
    else
        cout << -1;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int N, M, lastK = 0;
pair<int, int> A[100002];
vector<int> adj[100002], radj[100002];
bool vis[100002];

bool poss(int K) {
  fill_n(vis, 100002, false);
  if (K > lastK) {
    for (int i = lastK; i < K; ++i) {
      adj[A[i].first].push_back(A[i].second);
      radj[A[i].second].push_back(A[i].first);
    }
  }
  else if (K < lastK) {
    for (int i = lastK-1; i >= K; --i) {
      adj[A[i].first].pop_back();
      radj[A[i].second].pop_back();
    }
  }
  //cout << "wow " << radj[1].size() << '\n';
  lastK = K;
  int pos = 1;
  for (int i = 2; i <= N; ++i)
    if (radj[i].empty()) {
      if (radj[pos].empty())
        return false;
      pos = i;
    }
  while (true) {
    //cout << pos << ' ';
    int next = -1;
    for (int v : adj[pos]) {
      bool ok = true;
      for (int u : adj[pos])
        if (!vis[u]) {
          ok = false;
          break;
        }
      if (ok && next != -1)
        return false;
      else
        next = v;
    }
    if (next == -1)
      return true;
    pos = next;
  }
}

int main(){
	ios_base::sync_with_stdio(0); cin.tie(0);
  cin >> N >> M;
  for (int i = 0; i < M; ++i)
    cin >> A[i].first >> A[i].second;
  int lo = 1, hi = M, mid;
  while (lo < hi) {
    mid = lo + (hi-lo)/2;
    //cout << lo << '\n';
    if (!poss(lo))
      lo = mid+1;
    else
      hi = mid;
    //cout << '\n';
  }
  if (lo == M && !poss(lo))
    cout << "-1\n";
  else
    cout << lo << '\n';
return 0;}


#include <bits/stdc++.h>
using namespace std;

int N, M, lastK = 0;
pair<int, int> A[100002];
vector<int> adj[100002], radj[100002];
bool vis[100002];

bool poss(int K) {
  fill_n(vis, 100002, false);
  if (K > lastK) {
    for (int i = lastK; i < K; ++i) {
      adj[A[i].first].push_back(A[i].second);
      radj[A[i].second].push_back(A[i].first);
    }
  }
  else if (K < lastK) {
    for (int i = lastK-1; i >= K; --i) {
      adj[A[i].first].pop_back();
      radj[A[i].second].pop_back();
    }
  }
  //cout << "wow " << radj[1].size() << '\n';
  lastK = K;
  int pos = 1;
  for (int i = 2; i <= N; ++i)
    if (radj[i].empty()) {
      if (radj[pos].empty())
        return false;
      pos = i;
    }
  //cout << adj[2].size() << ' ' << radj[2].size() << '\n';
  int cnt = 0;
  while (true) {
    //cout << pos << '\n';
    int next = -1;
    ++cnt;
    vis[pos] = true;
    for (int v : adj[pos]) {
      bool ok = true;
      for (int u : radj[v])
        if (!vis[u]) {
          //cout << "! " << u << ' ' << v << '\n';
          ok = false;
          break;
        }
      if (ok && next != -1)
        return false;
      else if (ok)
        next = v;
    }
    if (next == -1) {
      //cout << K << ' ' << cnt << '\n';
      return cnt == N;
    }
    pos = next;
  }
}

int main(){
	ios_base::sync_with_stdio(0); cin.tie(0);
  cin >> N >> M;
  for (int i = 0; i < M; ++i)
    cin >> A[i].first >> A[i].second;
  int lo = 1, hi = M, mid;
  while (lo < hi) {
    mid = lo + (hi-lo)/2;
    //cout << lo << '\n';
    //cout << lo << ' ' << hi << '\n';
    if (!poss(mid))
      lo = mid+1;
    else
      hi = mid;
    //cout << '\n';
  }
  if (lo == M && !poss(lo))
    cout << "-1\n";
  else
    cout << lo << '\n';
return 0;}


#include <bits/stdc++.h>
using namespace std;

int N, M, lastK = 0;
pair<int, int> A[100002];
vector<int> adj[100002], radj[100002];
bool vis[100002];

bool poss(int K) {
  fill_n(vis, 100002, false);
  if (K > lastK) {
    for (int i = lastK; i < K; ++i) {
      adj[A[i].first].push_back(A[i].second);
      radj[A[i].second].push_back(A[i].first);
    }
  }
  else if (K < lastK) {
    for (int i = lastK-1; i >= K; --i) {
      adj[A[i].first].pop_back();
      radj[A[i].second].pop_back();
    }
  }
  //cout << "wow " << radj[1].size() << '\n';
  lastK = K;
  int pos = 1;
  for (int i = 2; i <= N; ++i)
    if (radj[i].empty()) {
      if (radj[pos].empty())
        return false;
      pos = i;
    }
  //cout << adj[2].size() << ' ' << radj[2].size() << '\n';
  int cnt = 0;
  while (true) {
    //cout << pos << '\n';
    int next = -1;
    ++cnt;
    vis[pos] = true;
    for (int v : adj[pos]) {
      bool ok = true;
      for (int u : radj[v])
        if (!vis[u]) {
          //cout << "! " << u << ' ' << v << '\n';
          ok = false;
          break;
        }
      if (ok && next != -1)
        return false;
      else if (ok)
        next = v;
    }
    if (next == -1) {
      //cout << K << ' ' << cnt << '\n';
      return cnt == N;
    }
    pos = next;
  }
}

int main(){
	ios_base::sync_with_stdio(0); cin.tie(0);
  cin >> N >> M;
  for (int i = 0; i < M; ++i)
    cin >> A[i].first >> A[i].second;
  int lo = 1, hi = M, mid;
  while (lo < hi) {
    mid = lo + (hi-lo)/2;
    //cout << lo << '\n';
    if (!poss(lo))
      lo = mid+1;
    else
      hi = mid;
    //cout << '\n';
  }
  if (lo == M && !poss(lo))
    cout << "-1\n";
  else
    cout << lo << '\n';
return 0;}


#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#define MAX 100010
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define f first
#define s second
typedef long long int ll;
using namespace std;
ll dp[MAX+5];
ll ans[MAX+5];
vector< pair <ll,ll> > v[MAX+5];

ll dfs(ll x)
{
	if(dp[x])
	return dp[x];

	dp[x]=1;

	for(ll i=0;i<v[x].size();i++)
	{
		ll y=v[x][i].f;
		if(1+dfs(y)>dp[x])
		{
			ans[x]=max(ans[x],v[x][i].s);
			dp[x]=1+dfs(y);
			ans[x]=max(ans[x],ans[v[x][i].s]);
		}
	}

	return dp[x];

}

int main() {

	ll i,n,m,x,y;
	cin>>n>>m;
	for(i=1;i<=m;i++)
	{
		cin>>x>>y;
		v[y].pb(mp(x,i));
	}

	for(i=1;i<=n;i++)
	{
		if(dfs(i)==n)
		{
			cout<<ans[i]<<endl;
			return 0;
		}
	}

	cout<<"-1"<<endl;

	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#define MAX 100010
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define f first
#define s second
typedef long long int ll;
using namespace std;
ll dp[MAX+5];
ll ans[MAX+5];
vector< pair <ll,ll> > v[MAX+5];

ll dfs(ll x)
{
	if(dp[x])
	return dp[x];

	dp[x]=1;

	for(ll i=0;i<v[x].size();i++)
	{
		ll y=v[x][i].f;
		if(1+dfs(y)>dp[x])
		{
			dp[x]=1+dp[y];
			ans[x]=max(ans[y],v[x][i].s);
		}
	}

	return dp[x];

}

int main() {

	ll i,n,m,x,y;
	cin>>n>>m;
	for(i=1;i<=m;i++)
	{
		cin>>x>>y;
		v[y].pb(mp(x,i));
	}

	for(i=1;i<=n;i++)
	{
		if(dfs(i)==n)
		{
			cout<<ans[i]<<endl;
			return 0;
		}
	}

	cout<<"-1"<<endl;

	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <cstring>
#include <cmath>
#include <map>
#define MAX 100010
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define f first
#define s second
typedef long long int ll;
using namespace std;
ll dp[MAX+5];
ll visited[MAX+5];
vector<ll> v[MAX];
set<ll> s;

ll dfs(ll x)
{
	if(dp[x]==0)
	dp[x]=1;

	visited[x]=1;

	ll maxi=max(ll(0),dp[x]);
	for(ll i=0;i<v[x].size();i++)
	{
		if(!visited[v[x][i]])
		maxi=max(maxi,dp[x]+dfs(v[x][i]));

		else
		maxi=max(maxi,dp[x]+dp[v[x][i]]);
	}

	dp[x]=maxi;
	return dp[x];
}

int main() {
	ll i,n,t,x,y,m;
	cin>>n>>t;
	ll maxi;
	ll flag=0;
	for(i=1;i<=t;i++)
	{
		cin>>x>>y;
		v[y].pb(x);
		s.insert(x);
		s.insert(y);
		if(s.size()==n)
		{
			maxi=0;
			for(ll k=1;k<=n;k++)
			{m=dfs(k);
			maxi=max(maxi,m);
			}
			flag=1;
			break;
		}
	}

	if(flag==1)
	{
		if(maxi==n)
		cout<<i<<endl;

		else
		cout<<"-1"<<endl;

	}

	else
	cout<<"-1"<<endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define si(x) (int)((x).size())
#define rep(i, a, b) for (__typeof(b) i = (a)-((a)>(b)); i != (b)-((a)>(b)); i += 1 - 2 * ((a) > (b)))
#define repi(i, a, b) for (__typeof(b) i = (a); i != (b); i++)
#define MOD 1000000007
typedef long long ll;
typedef unsigned long long ull;
typedef vector <int> vi;
typedef pair <int, int> pi;
typedef vector <vi> vvi;
typedef vector <pi> vpi;
typedef vector <string> vs;

vi w[100001];
int d[100001];
int vis[100001];
int nex[100001];

void dfs(int node) {
    vis[node] = 1;
    repi (i,0,si(w[node])) {
        if (vis[w[node][i]] == 0) dfs(w[node][i]);
        d[node] = max(d[node],1 + d[w[node][i]]);
    }
}

int main() {
    int n,m;
    scanf("%d %d",&n,&m);
    vpi input;
    repi (i,0,m) {
        int p,q;
        scanf("%d %d",&p,&q);
        input.pb(mp(p,q));
        w[p].pb(q);
    }
    repi (i,1,n+1) if (vis[i] == 0) dfs(i);
    vpi s;
    repi (i,1,n+1) s.pb(mp(-d[i],i));
    sort(all(s));
    if (s.begin()->fi != 1-n) {
        printf("-1\n");
        return 0;
    }
    vpi::iterator it = s.begin(),iit;
    iit = it; iit++;
    while (iit != s.end()) {
        nex[it->se] = iit->se;
        it++;iit++;
    }
    int count = n-1;
    repi (i,0,m) {
        if (nex[input[i].fi] == input[i].se) count--;
        if (count == 0) {
            printf("%d\n",i+1);
            return 0;
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define si(x) (int)((x).size())
#define rep(i, a, b) for (__typeof(b) i = (a)-((a)>(b)); i != (b)-((a)>(b)); i += 1 - 2 * ((a) > (b)))
#define repi(i, a, b) for (__typeof(b) i = (a); i != (b); i++)
#define MOD 1000000007
typedef long long ll;
typedef unsigned long long ull;
typedef vector <int> vi;
typedef pair <int, int> pi;
typedef vector <vi> vvi;
typedef vector <pi> vpi;
typedef vector <string> vs;

vi w[100001];
int d[100001];
int vis[100001];

void dfs(int node) {
    vis[node] = 1;
    repi (i,0,si(w[node])) {
        dfs(w[node][i]);
        d[node] = max(d[node],1 + d[w[node][i]]);
    }
}

int main() {
    int n,m;
    scanf("%d %d",&n,&m);
    vpi input;
    repi (i,0,m) {
        int p,q;
        scanf("%d %d",&p,&q);
        input.pb(mp(p,q));
        w[p].pb(q);
    }
    repi (i,1,n+1) if (vis[i] == 0) dfs(i);
    set <pi> s,req;
    repi (i,1,n+1) s.insert(mp(-d[i],i));
    if (s.begin()->fi != 1-n) {
        printf("-1\n");
        return 0;
    }
    set <pi>::iterator it = s.begin(),iit;
    iit = it; iit++;
    while (iit != s.end()) {
        req.insert(mp(it->se,iit->se));
        it++;iit++;
    }
    repi (i,0,m) {
        it = req.find(input[i]);
        if (it == req.end()) continue;
        req.erase(it);
        if (si(req) == 0) {
            printf("%d\n",i+1);
            return 0;
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define si(x) (int)((x).size())
#define rep(i, a, b) for (__typeof(b) i = (a)-((a)>(b)); i != (b)-((a)>(b)); i += 1 - 2 * ((a) > (b)))
#define repi(i, a, b) for (__typeof(b) i = (a); i != (b); i++)
#define MOD 1000000007
typedef long long ll;
typedef unsigned long long ull;
typedef vector <int> vi;
typedef pair <int, int> pi;
typedef vector <vi> vvi;
typedef vector <pi> vpi;
typedef vector <string> vs;

vi w[100001];
int d[100001];
int vis[100001];

void dfs(int node) {
    vis[node] = 1;
    repi (i,0,si(w[node])) {
        dfs(w[node][i]);
        d[node] = max(d[node],1 + d[w[node][i]]);
    }
}

int main() {
    int n,m;
    cin >> n >> m;
    vpi input;
    repi (i,0,m) {
        int p,q;
        cin>>p>>q;
        input.pb(mp(p,q));
        w[p].pb(q);
    }
    repi (i,1,n+1) if (vis[i] == 0) dfs(i);
    set <pi> s,req;
    repi (i,1,n+1) s.insert(mp(-d[i],i));
    if (s.begin()->fi != 1-n) {
        cout << "-1" << endl;
        return 0;
    }
    set <pi>::iterator it = s.begin(),iit;
    iit = it; iit++;
    while (iit != s.end()) {
        req.insert(mp(it->se,iit->se));
        it++;iit++;
    }
    repi (i,0,m) {
        it = req.find(input[i]);
        if (it == req.end()) continue;
        req.erase(it);
        if (si(req) == 0) {
            cout << i + 1 << endl;
            return 0;
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define si(x) (int)((x).size())
#define rep(i, a, b) for (__typeof(b) i = (a)-((a)>(b)); i != (b)-((a)>(b)); i += 1 - 2 * ((a) > (b)))
#define repi(i, a, b) for (__typeof(b) i = (a); i != (b); i++)
#define MOD 1000000007
typedef long long ll;
typedef unsigned long long ull;
typedef vector <int> vi;
typedef pair <int, int> pi;
typedef vector <vi> vvi;
typedef vector <pi> vpi;
typedef vector <string> vs;

vi w[100001];
int d[100001];
int vis[100001];
int nex[100001];

void dfs(int node) {
    vis[node] = 1;
    repi (i,0,si(w[node])) {
        dfs(w[node][i]);
        d[node] = max(d[node],1 + d[w[node][i]]);
    }
}

int main() {
    int n,m;
    scanf("%d %d",&n,&m);
    vpi input;
    repi (i,0,m) {
        int p,q;
        scanf("%d %d",&p,&q);
        input.pb(mp(p,q));
        w[p].pb(q);
    }
    repi (i,1,n+1) if (vis[i] == 0) dfs(i);
    set <pi> s;
    repi (i,1,n+1) s.insert(mp(-d[i],i));
    if (s.begin()->fi != 1-n) {
        printf("-1\n");
        return 0;
    }
    set <pi>::iterator it = s.begin(),iit;
    iit = it; iit++;
    while (iit != s.end()) {
        nex[it->se] = iit->se;
        it++;iit++;
    }
    int count = n-1;
    repi (i,0,m) {
        if (nex[input[i].fi] == input[i].se) count--;
        if (count == 0) {
            printf("%d\n",i+1);
            return 0;
        }
    }
    return 0;
}


#include <cstdio>

#include <cmath>

#include <cstring>

#include <iostream>

#include <algorithm>

#include <queue>

#include <vector>

#include <map>

using namespace std;

typedef long long LL;

int n,m,l,r,mid,ans,x[100005],y[100005];
int d[100005];
vector<int> v[100005];
bool check(int w)
{
    int dq,now;
    for (int i=1;i<=n;++i) v[i].clear();
    for (int i=1;i<=w;++i) v[y[i]].push_back(x[i]);
    memset(d,0,sizeof(d));
    for (int i=1;i<=w;++i) ++d[x[i]];dq=-1;
    for (int i=1;i<=n;++i)
        if (d[i]==0)
        {
            if (dq==-1) dq=i;
            else return false;
        }
    for (int i=1;i<=n;++i)
    {
        now=-1;
        if (dq==-1) return false;
        for (int j=0;j<v[dq].size();++j)
        {
            --d[v[dq][j]];
            if (d[v[dq][j]]==0)
            {
                if (now==-1) now=v[dq][j];
                else return false;
            }
        }
        dq=now;
    }
    return true;
}

int main()

{
    scanf("%d%d",&n,&m);
    for (int i=1;i<=m;++i)
    {
        scanf("%d%d",&x[i],&y[i]);
    }
    l=n-1;r=m;ans=-1;
    check(4);
    while (l<=r)
    {
        mid=(l+r)/2;
        if (check(mid))
        {
            ans=mid;r=mid-1;
        }
        else l=mid+1;
    }
    printf("%d\n",ans);

    return 0;

}


#include <cstdio>

#include <cmath>

#include <cstring>

#include <iostream>

#include <algorithm>

#include <queue>

#include <vector>

#include <map>

using namespace std;

typedef long long LL;

int n,m,l,r,mid,ans,x[100005],y[100005];
int d[100005];
vector<int> v[100005];
bool check(int w)
{
    int dq,now;
    for (int i=1;i<=n;++i) v[i].clear();
    for (int i=1;i<=w;++i) v[y[i]].push_back(x[i]);
    memset(d,0,sizeof(d));
    for (int i=1;i<=w;++i) ++d[x[i]];dq=-1;
    for (int i=1;i<=n;++i)
        if (d[i]==0)
        {
            if (dq==-1) dq=i;
            else return false;
        }
    if (dq==-1) return false;
    for (int i=1;i<=n;++i)
    {
        now=-1;
        for (int j=0;j<v[dq].size();++j)
        {
            --d[v[dq][j]];
            if (d[v[dq][j]]==0)
            {
                if (now==-1) now=d[v[dq][j]];
                else return false;
            }
        }
        dq=now;
        if (now==-1) return false;
    }
    return true;
}

int main()

{
    scanf("%d%d",&n,&m);
    for (int i=1;i<=m;++i)
    {
        scanf("%d%d",&x[i],&y[i]);
    }
    l=n-1;r=m;ans=-1;
    while (l<=r)
    {
        mid=(l+r)/2;
        if (check(mid))
        {
            ans=mid;r=mid-1;
        }
        else l=mid+1;
    }
    printf("%d\n",ans);

    return 0;

}


#include <cstdio>

#include <cmath>

#include <cstring>

#include <iostream>

#include <algorithm>

#include <queue>

#include <vector>

#include <map>

using namespace std;

typedef long long LL;

int n,m,l,r,mid,ans,x[100005],y[100005];
int d[100005];
vector<int> v[100005];
bool check(int w)
{
    int dq,now;
    for (int i=1;i<=n;++i) v[i].clear();
    for (int i=1;i<=w;++i) v[y[i]].push_back(x[i]);
    memset(d,0,sizeof(d));
    for (int i=1;i<=w;++i) ++d[x[i]];dq=-1;
    for (int i=1;i<=n;++i)
        if (d[i]==0)
        {
            if (dq==-1) dq=i;
            else return false;
        }
    for (int i=1;i<=n;++i)
    {
        now=-1;
        for (int j=0;j<v[dq].size();++j)
        {
            --d[v[dq][j]];
            if (d[v[dq][j]]==0)
            {
                if (now==-1) now=d[v[dq][j]];
                else return false;
            }
        }
        dq=now;
    }
    return true;
}

int main()

{
    scanf("%d%d",&n,&m);
    for (int i=1;i<=m;++i)
    {
        scanf("%d%d",&x[i],&y[i]);
    }
    l=n-1;r=m;ans=-1;
    while (l<=r)
    {
        mid=(l+r)/2;
        if (check(mid))
        {
            ans=mid;r=mid-1;
        }
        else l=mid+1;
    }
    printf("%d\n",ans);

    return 0;

}


#include <bits/stdc++.h>
using namespace std;

const int MAXN = 1000005;
int N, M, dp[MAXN], res[MAXN];
vector<pair<int,int>> adj[MAXN];

int check(int v){
  if(dp[v]) return dp[v];
  dp[v] = 1;
  for(auto p : adj[v]){
    int n = p.first;
    if(check(n) + 1 > dp[v]){
      dp[v] = dp[n] + 1;
      res[v] = max(res[n], p.second);
    }
  }
  return dp[v];
}

int main(){
  scanf("%d%d", &N, &M);
  for(int i = 0; i < M; i++){
    int a, b;
    scanf("%d%d", &a, &b);
    a -= 1, b -= 1;
    adj[a].push_back({b, i + 1});
  }
  for(int i = 0; i < N; i++){
    if(check(i) == N){
      printf("%d\n", res[i]);
      return 0;
    }
  }
  printf("-1\n");
}


#include <bits/stdc++.h>
using namespace std;

const int MAXN = 1000005;
int N, M, dp[MAXN], res[MAXN];
vector<pair<int,int>> adj[MAXN];

int check(int v){
  if(dp[v]) return dp[v];
  dp[v] = 1;
  for(auto p : adj[v]){
    int n = p.first;
    if(check(n) + 1 > dp[v]){
      dp[v] = dp[n] + 1;
      res[v] = max(res[n], p.second);
    }
  }
  return dp[v];
}

int main(){
  scanf("%d%d", &N, &M);
  for(int i = 0; i < M; i++){
    int a, b;
    scanf("%d%d", &a, &b);
    a -= 1, b -= 1;
    adj[a].push_back({b, i + 1});
  }
  for(int i = 0; i < N; i++){
    if(check(i) == N){
      printf("%dn", res[i]);
      return 0;
    }
  }
  printf("-1n");
}


#include<bits/stdc++.h>
#define rep(i,a) for(int i=0;i<int(a);++i)
#define all(a) a.begin(),a.end()
#define ii pair<int,int>
#define clr(a) memset(a,0,sizeof(a))
typedef long long ll;
using namespace std;
const int N = 100005;
int n, m;
struct edge {
	int to, time;
};
vector<edge>g[N];
int vis[N];
vector<edge>use;
bool dfs(int u, int sum) {
	if (sum == n)return true;
	for (int i = 0; i < g[u].size(); i++) {
		edge& e = g[u][i];
		use.push_back(e);
		if (dfs(e.to, sum + 1))return true;
		use.pop_back();
	}
	return false;
}
int main() {
	cin >> n >> m;
	int has[N];
	clr(has);
	memset(vis, -1, sizeof(vis));
	rep(i, m) {
		int x, y;
		scanf("%d%d", &x, &y);
		x--, y--;
		has[y] = 1;
		g[x].push_back({ y,i });
	}
	int root = -1;
	for (int i = 0; i < n; i++) {
		if (has[i] == 0) {
			root = i;
			break;
		}
	}
	int ans = -1;
	if (dfs(root, 1)) {
		for (auto x : use)if (x.time>ans)ans = x.time;
		cout << ans + 1 << endl;
	}
	else cout << -1 << endl;
}


#include<bits/stdc++.h>
#define rep(i,a) for(int i=0;i<int(a);++i)
#define all(a) a.begin(),a.end()
#define ii pair<int,int>
#define clr(a) memset(a,0,sizeof(a))
typedef long long ll;
using namespace std;
const int N = 100005;
int n, m;
struct edge {
	int to, time;
};
vector<edge>g[N];
int dp[N];
int t[N];
int ans;
int find(int u) {
	if (dp[u] != -1)return dp[u];
	int ans = 1;
	for (int i = 0; i < g[u].size(); i++) {
		edge&e = g[u][i];
		int tmp = find(e.to);
		if (tmp + 1 > ans) {
			ans = tmp + 1;
			t[u] = max(e.time, t[e.to]);
		}
	}
	return dp[u] = ans;
}
//bool dfs(int u, int sum, int time) {
//	if (sum == n) {
//		ans = time;
//		return true;
//	}
//	for (int i = 0; i < g[u].size(); i++) {
//		edge& e = g[u][i];
//		if (dfs(e.to, sum + 1,max(time,e.time)))return true;
//	}
//	return false;
//}
int main() {
	memset(dp, -1, sizeof(dp));
	ans = -1;
	cin >> n >> m;
	int has[N];
	clr(has);
	rep(i, m) {
		int x, y;
		scanf("%d%d", &x, &y);
		x--, y--;
		has[y] = 1;
		g[x].push_back({ y,i });
	}
	int root = -1;
	for (int i = 0; i < n; i++) {
		if (has[i] == 0) {
			root = i;
			break;
		}
	}
	if (find(root) == n)cout << t[root]+1 << endl;
	else cout << -1 << endl;
}


#include<bits/stdc++.h>
#define rep(i,a) for(int i=0;i<int(a);++i)
#define all(a) a.begin(),a.end()
#define ii pair<int,int>
#define clr(a) memset(a,0,sizeof(a))
typedef long long ll;
using namespace std;
const int N = 100005;
int n, m;
struct edge {
	int to, time;
};
vector<edge>g[N];
int ans;
bool dfs(int u, int sum, int time) {
	if (sum == n) {
		ans = time;
		return true;
	}
	for (int i = 0; i < g[u].size(); i++) {
		edge& e = g[u][i];
		if (dfs(e.to, sum + 1,max(time,e.time)))return true;
	}
	return false;
}
int main() {
	ans = -1;
	cin >> n >> m;
	int has[N];
	clr(has);
	rep(i, m) {
		int x, y;
		scanf("%d%d", &x, &y);
		x--, y--;
		has[y] = 1;
		g[x].push_back({ y,i });
	}
	int root = -1;
	for (int i = 0; i < n; i++) {
		if (has[i] == 0) {
			root = i;
			break;
		}
	}
	if (dfs(root, 1, 0))cout << ans+1 << endl;
	else cout << -1 << endl;
}


//*
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <numeric>
#include <functional>
#define MOD 1000000007
#define MAX 0x3f3f3f3f
#define MAX2 0x3f3f3f3f3f3f3f3fll
#define ERR 1e-10
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#pragma warning(disable:4996)
using namespace std;

typedef long long ll;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;

int n, m;
pii a[200000];
vector<int> edge[200000];
int d[200000];
int ind[200000];

int param(int x)
{
	int i, j, k;
	for(i=0;i<n;i++) edge[i].clear();
	fill(d, d+n, 0);
	fill(ind, ind+n, 0);
	for(i=0;i<=x;i++)
	{
		edge[a[i].first].push_back(a[i].second);
		ind[a[i].second]++;
	}
	vector<int> q;
	for(i=0;i<n;i++) if(!ind[i]) q.push_back(i);
	for(i=0;i<q.size();i++)
	{
		int now=q[i];
		for(auto e : edge[now])
		{
			ind[e]--;
			d[e]=max(d[e], d[now]+1);
			if(!ind[e]) q.push_back(e);
		}
	}
	if(*max_element(d, d+n) == n-1) return 1;
	return 0;
}

int main()
{
	int i, j, k;
	cin>>n>>m;
	for(i=0;i<m;i++) scanf("%d%d", &a[i].first, &a[i].second), a[i].first--, a[i].second--;
	int low=0, high=m-1, mid;
	while(low <= high)
	{
		mid=(low+high)/2;
		if(param(mid)) high=mid-1;
		else low=mid+1;
	}
	if(low == n) low=-2;
	cout<<low+1;
	return 0;
}
//*/


//*
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <numeric>
#include <functional>
#define MOD 1000000007
#define MAX 0x3f3f3f3f
#define MAX2 0x3f3f3f3f3f3f3f3fll
#define ERR 1e-10
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#pragma warning(disable:4996)
using namespace std;

typedef long long ll;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;

int n, m;
pii a[200000];
vector<int> edge[200000];
int d[200000];
int ind[200000];

int param(int x)
{
	int i, j, k;
	for(i=0;i<n;i++) edge[i].clear();
	fill(d, d+n, 0);
	fill(ind, ind+n, 0);
	for(i=0;i<=x;i++)
	{
		edge[a[i].first].push_back(a[i].second);
		ind[a[i].second]++;
	}
	vector<int> q;
	for(i=0;i<n;i++) if(!ind[i]) q.push_back(i);
	for(i=0;i<q.size();i++)
	{
		int now=q[i];
		for(auto e : edge[now])
		{
			ind[e]--;
			d[e]=max(d[e], d[now]+1);
			if(!ind[e]) q.push_back(e);
		}
	}
	if(*max_element(d, d+n) == n-1) return 1;
	return 0;
}

int main()
{
	int i, j, k;
	cin>>n>>m;
	for(i=0;i<m;i++) scanf("%d%d", &a[i].first, &a[i].second), a[i].first--, a[i].second--;
	int low=0, high=n-1, mid;
	while(low <= high)
	{
		mid=(low+high)/2;
		if(param(mid)) high=mid-1;
		else low=mid+1;
	}
	if(low == n) low=-2;
	cout<<low+1;
	return 0;
}
//*/


//*
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <numeric>
#include <functional>
#define MOD 1000000007
#define MAX 0x3f3f3f3f
#define MAX2 0x3f3f3f3f3f3f3f3fll
#define ERR 1e-10
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#pragma warning(disable:4996)
using namespace std;

typedef long long ll;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;

int n, m;
pii a[200000];
vector<int> edge[200000];
int d[200000];
int ind[200000];

int param(int x)
{
	int i, j, k;
	for(i=0;i<n;i++) edge[i].clear();
	fill(d, d+n, 0);
	fill(ind, ind+n, 0);
	for(i=0;i<=x;i++)
	{
		edge[a[i].first].push_back(a[i].second);
		ind[a[i].second]++;
	}
	vector<int> q;
	for(i=0;i<n;i++) if(!ind[i]) q.push_back(i);
	for(i=0;i<q.size();i++)
	{
		int now=q[i];
		for(auto e : edge[now])
		{
			ind[e]--;
			d[e]=max(d[e], d[now]+1);
			if(!ind[e]) q.push_back(e);
		}
	}
	if(*max_element(d, d+n) == n-1) return 1;
	return 0;
}

int main()
{
	int i, j, k;
	cin>>n>>m;
	for(i=0;i<m;i++) scanf("%d%d", &a[i].first, &a[i].second), a[i].first--, a[i].second--;
	int low=0, high=m-1, mid;
	while(low <= high)
	{
		mid=(low+high)/2;
		if(param(mid)) high=mid-1;
		else low=mid+1;
	}
	if(low == m) low=-2;
	cout<<low+1;
	return 0;
}
//*/


#include<bits/stdc++.h>
#define N 100010
using namespace std;
int n,m;
vector<int> e[N];
vector<int> num[N];
int a,b;
int ans;
int d[N];
bool vis[N];
bool ff(int x)
{
	for(int i=1;i<=n;i++)
	{
		for(int j=0;j<e[i].size();j++)
		{
			if(num[i][j]<=x)
			{
				d[e[i][j]]++;
				vis[i]=1;
				vis[e[i][j]]=1;
			}
		}
	}
	queue<int> q;
	for(int i=1;i<=n;i++)
	{
		if(vis[i]==1&&d[i]==0)
		{
			q.push(i);
		}
	}
	while(!q.empty())
	{
		if(q.size()>1) return 0;
		int u=q.front();
		q.pop();
		for(int j=0;j<e[u].size();j++)
		{
			if(num[u][j]<=x)
			{
				d[e[u][j]]--;
				if(d[e[u][j]]==0) q.push(e[u][j]);
			}
		}
	}
	return 1;
}
int main()
{
	cin>>n>>m;
	for(int i=1;i<=m;i++)
	{
		cin>>a>>b;
		e[a].push_back(b);
		num[a].push_back(i);
	}
	int l,r,mid;
	l=1; r=m;
	if(ff(m)==0) ans=-1;
	else
	{
		while(l<r)
		{
			memset(d,0,sizeof(d));
			mid=(l+r)/2;
			if(ff(mid)) r=mid;
			else l=mid+1;
		}
		ans=l;
	}
	cout<<ans;
	return 0;
}


#include <stdio.h>
#include <vector>
using namespace std;

vector< pair<int, int> > way[100005];
int dp[100005];
int ans[100005];

int find(int x){
    if(dp[x]) return dp[x];
    dp[x] = 1;
    for(int i=0; i<way[x].size(); i++){
        if(find(way[x][i].first) + 1 > dp[x]){
            dp[x] = dp[way[x][i].first] + 1;
            ans[x] = ans[way[x][i].first];
            if(way[x][i].second > ans[way[x][i].first]) ans[x] = way[x][i].second;
        }
    }
    return dp[x];
}

int main(){
    int n, m, a, b;
    scanf("%d%d", &n, &m);
    for(int i=1; i<=m; i++){
        scanf("%d%d", &a, &b);
        way[b].push_back({a,i});
    }
    for(int i=1; i<=n; i++){
        if(find(i) == n){
            printf("%d", ans[i]);
            return 0;
        }
    }
    printf("-1");
}


#include <stdio.h>
#include <vector>
using namespace std;

vector< pair<int, int> > way[100005];
int dp[100005];
int ans[100005];

int find(int x){
    if(dp[x]) return dp[x];
    dp[x] = 1;
    for(int i=0; i<way[x].size(); i++){
        if(find(way[x][i].first) + 1 > dp[x]){
            dp[x] = dp[way[x][i].first] + 1;
            if(way[x][i].second > ans[x]) ans[x] = way[x][i].second;
        }
    }
    return dp[x];
}

int main(){
    int n, m, a, b;
    scanf("%d%d", &n, &m);
    for(int i=0; i<m; i++){
        scanf("%d%d", &a, &b);
        way[b].push_back({a,i});
    }
    for(int i=1; i<=n; i++){
        if(find(i) == n){
            printf("%d", find(i));
            return 0;
        }
    }
    printf("-1");
}