#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
const int N=1e5+5,mod=1e9+7;
typedef long long ll;
vector<int> v[N];
int d[N],n,m,ans[N],cnt,a[N],b[N],s;
bool topsort()
{
queue<int> q;
for(int i=1;i<=n;i++) if(!d[i]) q.push(i);
while(q.size())
{
if(q.size()>1) return false;
int x=q.front();
q.pop();
for(int i=0;i<v[x].size();i++)
{
int j=v[x][i];
if(--d[j]==0)
{
q.push(j);
ans[j]=x;
s++;
}
}
}
return true;
}
void solve()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
scanf("%d%d",&a[i],&b[i]);
v[a[i]].push_back(b[i]);
d[b[i]]++;
}
if(!topsort()) printf("-1\n");
else
{
for(int i=1;i<=m;i++)
{
if(ans[b[i]]==a[i])
{
s--;
if(!s)
{
printf("%d\n",i);
return;
}
}
}
}
}
int main()
{
solve();
return 0;
}
1044. Longest Duplicate Substring | 1032. Stream of Characters |
987. Vertical Order Traversal of a Binary Tree | 952. Largest Component Size by Common Factor |
212. Word Search II | 174. Dungeon Game |
127. Word Ladder | 123. Best Time to Buy and Sell Stock III |
85. Maximal Rectangle | 84. Largest Rectangle in Histogram |
60. Permutation Sequence | 42. Trapping Rain Water |
32. Longest Valid Parentheses | Cutting a material |
Bubble Sort | Number of triangles |
AND path in a binary tree | Factorial equations |
Removal of vertices | Happy segments |
Cyclic shifts | Zoos |
Build a graph | Almost correct bracket sequence |
Count of integers | Differences of the permutations |
Doctor's Secret | Back to School |
I am Easy | Teddy and Tweety |