program main
 implicit none
 integer N, M
 integer,allocatable :: s(:),c(:)
 integer i
 integer :: kurai(3)=-1

 read *,N, M
 allocate(s(M))
 allocate(c(M))
 do i=1,M
  read *,s(i),c(i)
 end do

 do i=1,M
  if ( kurai(s(i)) == -1 ) then
   kurai(s(i))=c(i)
  else
   if ( kurai(s(i)) /= c(i) ) then
    print '(i0)', -1
    stop
   end if
  end if
! if ( kurai(1) == 0 ) then
!   print '(i0)', -1
!   stop
! end if
 end do

 do i=1,3
  if (kurai(i) == -1) kurai(i)=0
 end do

 if ( N==1 ) then
  if ( kurai(1) /= 0 .or. kurai(2) /= 0 ) then
    print '(i0)', -1
    stop
  end if
 end if 

 if ( N==2 ) then
! if ( kurai(1) /= 0 .or. kurai(2) == 0 ) then
  if ( kurai(1) /= 0 ) then
    print '(i0)', -1
    stop
  end if
  if ( kurai(2) == 0 ) then
    print '(i0)', -1
    stop
  end if
 end if

 if ( N==3 ) then
  if ( kurai(1) == 0 .or. kurai(2) == 0 ) then
    print '(i0)', -1
    stop
  end if
 end if

 if (N==1) print '(i0)', kurai(3)
 if (N==2) print '(i0)', kurai(2)*10+kurai(3)
 if (N==3) print '(i0)', kurai(1)*100+kurai(2)*10+kurai(3)

!print *,''
!print *,kurai(1),kurai(2),kurai(3)
 endprogram main







