N,M=map(int,input().split())
A=[]
for i in range(M):
  s,c=list(map(int,input().split()))
for j in range(N):
  if all(j[s[i]-1]==c[i] for i in range(M))
    A.append(i)
if len(A)==0:
  print("-1")
else:
  print(min(A))