N,M = map(int, input().split(" "))

#print(N,M)

tmp=[]
ans=[]
answer=-1

tmps=[]
tmpc=[]

if N == 3:
  for i in range(100,1000):
    tmp.append(i)
    ans.append(0)
  #print(ans)
elif N == 2:
  for i in range(10,100):
    tmp.append(i)
    ans.append(0)
elif N == 1:
  for i in range(1,10):
    tmp.append(i)
    ans.append(0)

for a in range(M):
  s,c = map(int, input().split(" "))
  tmps.append(s)
  tmpc.append(c)
  if N == 3:
    for i in range(len(tmp)):
      if s == 1:
        if tmp[i] // 100  == c :
          ans[i]+=1
      elif s == 2:
        if (tmp[i] - tmp[i]//100 * 100) // 10 == c :
         ans[i]+= 1
      elif s == 3:
        if (tmp[i] - tmp[i]//10 * 10 )== c:
          ans[i]+= 1
          #print(tmp[i])
  elif N == 2:
    for i in range(len(tmp)):
      if s == 1:
        if tmp[i] // 10 == c :
         ans[i]+= 1
      elif s == 2:
        if (tmp[i] - tmp[i]//10 * 10 )== c:
          ans[i]+= 1
          #print(tmp[i])
  elif N == 1:
    for i in range(len(tmp)):
      if s == 1:
        if tmp[i] == c :
         ans[i]+= 1


#print(tmp)
#print(ans)
ma = max(ans)

for i in range(len(ans)):
  if ans[i] == ma:
    answer = tmp[i]
    break

for a in range(M):
  for b in range(a,M):
    if tmps[a] == tmps[b]:
      if tmpc[a] != tmpc[b]:
        answer=-1
for a in range(M):
  if tmps[a] == 1:
    if tmpc[b] == 0:
      answer=-1
    
print(answer)