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
          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) ) )
            if ( j == 0 ) then
              if ( num(1,1) == 1 ) then 
                print *, -1
                stop
              else
                j = 9
                do k = 1, num(1,1)
                  if ( num(1,k+1) == 0 ) cycle
                  if ( j >= num(1,k+1)) then
                    j = num(1,k+1)
                  end if
                end do
              end if
            end if
            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) ) )
            if ( j == 0 ) then
              if ( num(1,1) == 1 ) then 
                print *, -1
                stop
              else
                j = 9
                do k = 1, num(1,1)
                  if ( num(1,k+1) == 0 ) cycle
                  if ( j >= num(1,k+1)) then
                    j = num(1,k+1)
                  end if
                end do
              end if
            end if
            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
