#include <iostream>
#include <cstdlib>
#include <vector>
#include <cstdio>
#include <ios>
#include <map>
#include <set>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
#define N 100005
int par[N];
int last[N];
int getRoot(int x){
if(par[x] == x) return x;
return par[x] = getRoot(par[x]);
}
bool areSameComp(int a, int b){
return getRoot(a) == getRoot(b);
}
void mergee(int a, int b){
par[getRoot(a)] = getRoot(b);
}
int main() {
int n, m, a, b;
scanf("%d %d", &n, &m);
for(int i = 1; i <= n; i++){
par[i] = i;
}
int tot = 0;
for(int i = 0; i < m; i++){
++tot;
scanf("%d %d", &a, &b);
if(areSameComp(a, b)) break;
if(last[a] != 0) mergee(last[a], b);
if(last[b] != 0) mergee(last[b], a);
if(areSameComp(a, b)) break;
last[a] = b;
last[b] = a;
}
cout << tot << endl;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8Y3N0ZGxpYj4KI2luY2x1ZGUgPHZlY3Rvcj4KI2luY2x1ZGUgPGNzdGRpbz4KI2luY2x1ZGUgPGlvcz4KI2luY2x1ZGUgPG1hcD4KI2luY2x1ZGUgPHNldD4KI2luY2x1ZGUgPGFsZ29yaXRobT4KI2luY2x1ZGUgPGNzdHJpbmc+CiAKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKIAp0eXBlZGVmIGxvbmcgbG9uZyBsbDsKIAojZGVmaW5lIE4gMTAwMDA1CiAKaW50IHBhcltOXTsKaW50IGxhc3RbTl07CiAKaW50IGdldFJvb3QoaW50IHgpewogICAgaWYocGFyW3hdID09IHgpIHJldHVybiB4OwogICAgcmV0dXJuIHBhclt4XSA9IGdldFJvb3QocGFyW3hdKTsKfQogCmJvb2wgYXJlU2FtZUNvbXAoaW50IGEsIGludCBiKXsKICAgIHJldHVybiBnZXRSb290KGEpID09IGdldFJvb3QoYik7Cn0KIAp2b2lkIG1lcmdlZShpbnQgYSwgaW50IGIpewogICAgcGFyW2dldFJvb3QoYSldID0gZ2V0Um9vdChiKTsKfQogCmludCBtYWluKCkgewogICAgaW50IG4sIG0sIGEsIGI7CiAgICBzY2FuZigiJWQgJWQiLCAmbiwgJm0pOwogICAgZm9yKGludCBpID0gMTsgaSA8PSBuOyBpKyspewogICAgICAgIHBhcltpXSA9IGk7CiAgICB9CiAgICBpbnQgdG90ID0gMDsKICAgIGZvcihpbnQgaSA9IDA7IGkgPCBtOyBpKyspewogICAgICAgICsrdG90OwogICAgICAgIHNjYW5mKCIlZCAlZCIsICZhLCAmYik7CiAgICAgICAgaWYoYXJlU2FtZUNvbXAoYSwgYikpIGJyZWFrOwogICAgICAgIGlmKGxhc3RbYV0gIT0gMCkgbWVyZ2VlKGxhc3RbYV0sIGIpOwogICAgICAgIGlmKGxhc3RbYl0gIT0gMCkgbWVyZ2VlKGxhc3RbYl0sIGEpOwogICAgICAgIGlmKGFyZVNhbWVDb21wKGEsIGIpKSBicmVhazsKICAgICAgICBsYXN0W2FdID0gYjsKICAgICAgICBsYXN0W2JdID0gYTsKICAgIH0KICAgIAogICAgY291dCA8PCB0b3QgPDwgZW5kbDsKICAgIAogICAgcmV0dXJuIDA7Cn0=