program ABC157_C
  integer::X,Y,Z,A
  integer,dimension(:),allocatable::S,C
  read(5,*)N,M
  allocate(S(M),C(M))
  X=0
  Y=0
  Z=0
  if(N==2) Y=1
  if(N==3) X=1
  K=0
  do I=1,M
     read(5,*)S(I),C(I)
  end do
  if(M>1) then
     do I=1,M-1
        if(N>1 .and. (S(I)==1 .and. C(I)==0) ) then
           K=1
           go to 10
        end if
        do J=2,M
           if(S(I)==S(J) .and. C(I)/=C(J)) then
              K=1
              go to 10
           end if
        end do
     end do
  end if
  do I=1,M
     if(N==3 .and. S(I)==1) then
        X=C(I)
     else if( (N==3 .and. S(I)==2) .or. (N==2 .and. S(I)==1)) then
        Y=C(I)
     else
        Z=C(I)
     end if
  end do
  A=100*X+10*Y+Z
  if(N==3 .and. A<100) K=1
  if(N==2 .and. A<10) K=1
10 if(K==1) then
     write(6,*)'-1'
  else
     write(6,*)A
  end if
end program ABC157_C