program main
      implicit none
      integer(4) :: n, m, i, j, k, l
      integer(4), allocatable :: s(:), c(:)
      integer(4), allocatable :: num(:,:)

      read *, n, m
      allocate( s(m) )
      allocate( c(m) )
      do i = 1, m
        read *, s(i), c(i)
      end do

      if ( n==1 ) then
        print *, minval(c)
      else
        allocate( num(n,6) )
        num(:,:) = 0
        do i = 1, m
          if ( c(i) == 0 ) cycle
          num( s(i),1 ) = num( s(i),1 ) + 1
          num( s(i), num(s(i),1)+1) = c(i)
        end do
        if ( num( 1, 1 ) == 0 ) then
          print *, -1
        else
            if ( n == 2 ) then
              j = minval( num( 1, 2:(num(1,1)+1) ) )
              k = minval( num( 2, 2:(num(2,1)+1) ) )
              if ( num(2,1) == 0 ) k = 0
              write(6,'(i1,i1)') j, k
            else
              j = minval( num( 1, 2:(num(1,1)+1) ) )
              k = minval( num( 2, 2:(num(2,1)+1) ) )
              if ( num(2,1) == 0 ) k = 0
              l = minval( num( 3, 2:(num(3,1)+1) ) )
              if ( num(3,1) == 0 ) l = 0
              write(6,'(i1,i1,i1)') j, k, l
            end if
        end if 
      end if

end program main
