#include <iostream>
#include <fstream>
#include <cstdio>
#include <iomanip>
#include <sstream>
#include <cstring>
#include <string>
#include <cmath>
#include <stack>
#include <list>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <vector>
#include <algorithm>
#include <numeric>
#include <utility>
#include <functional>
#include <limits>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int ui;
typedef pair<int,int> pii;
typedef vector<vector<int> > graph;
const double pi = acos(-1.0);
#define oned(a, x1, x2) { cout << #a << ":"; for(int _i = (x1); _i < (x2); _i++){ cout << " " << a[_i]; } cout << endl; }
#define twod(a, x1, x2, y1, y2) { cout << #a << ":" << endl; for(int _i = (x1); _i < (x2); _i++){ for(int _j = (y1); _j < (y2); _j++){ cout << (_j > y1 ? " " : "") << a[_i][_j]; } cout << endl; } }
#define mp make_pair
#define pb push_back
#define fst first
#define snd second
#define MAXN 300005
int parent[MAXN];
int Group(int a) {
if(parent[a]==a) {
return a;
} else {
return parent[a] = Group(parent[a]);
}
}
int n, m, x[MAXN], y[MAXN];
int adj[MAXN];
void solve() {
for(int i = 1; i <= n; i++) {
parent[i] = i;
}
memset(adj,0,sizeof(adj));
for(int i = 1; i <= m; i++) {
int a = Group(x[i]);
int b = Group(y[i]);
if(a==b) {
cout << i << endl;
return;
} else {
if(adj[a]) {
parent[adj[a]] = b;
}
if(adj[b]) {
parent[adj[b]] = a;
}
adj[a] = b;
adj[b] = a;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
// freopen("input.in", "r", stdin);
while(cin >> n >> m) {
for(int i = 1; i <= m; i++) {
cin >> x[i] >> y[i];
}
solve();
}
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8ZnN0cmVhbT4KI2luY2x1ZGUgPGNzdGRpbz4KI2luY2x1ZGUgPGlvbWFuaXA+CiNpbmNsdWRlIDxzc3RyZWFtPgojaW5jbHVkZSA8Y3N0cmluZz4KI2luY2x1ZGUgPHN0cmluZz4KI2luY2x1ZGUgPGNtYXRo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