program main
  implicit none
  integer :: n, i, j, m, res
  integer, allocatable :: s(:)
  character(4) :: si
  character(1), allocatable :: c(:)
  read (*, *) n, m
  allocate (s(m), c(m))
  do i = 1, m
    read (*, *) s(i), c(i)
  end do

  res = -1
  iloop: do i = 1, 999
    write (si, "(i0)") i
    if (len(trim(si)) == n) then
      do j = 1, m
        if (si(s(j):s(j)) /= c(j)) cycle iloop
      end do
      res = i
      exit
    end if
  end do iloop
  write (*, "(i0)") res
end program main
