type
point=^node;
node=record
g:longint;
next:point;
end;
var
i,j,k,l,m,n:longint;
a,b:array[0..2000] of point;
f,c:array[0..2000] of longint;
procedure add(x,y:longint);
var p:point;
begin
new(p);
p^.g:=y;
p^.next:=a[x];
a[x]:=p;
new(p);
p^.g:=x;
p^.next:=b[y];
b[y]:=p;
end;
function check(x:longint):boolean;
var p:point;
begin
p:=a[x];
while p<>nil do
begin
if f[p^.g]=0 then
begin
f[p^.g]:=1;
if c[p^.g]=0 then
begin
c[p^.g]:=x;
exit(true);
end
else if check(c[p^.g]) then
begin
c[p^.g]:=x;
exit(true);
end;
end;
p:=p^.next;
end;
exit(false);
end;
begin
readln(m,n);
for i:=1 to n do a[i]:=nil;
for i:=1 to m do b[i]:=nil;
for i:=1 to n do
begin
readln(j,k);add(i,j+1);
if k<>j then add(i,k+1);
end;
for i:=1 to n do
begin
fillchar(f,sizeof(f),0);
if not(check(i)) then
begin
writeln(i-1);
halt;
end;
end;
writeln(n);
end.