(let* ((n (read))
       (m (read))
       (lst (loop :repeat m :collect (cons (1- (read)) (read-char)))))
  (if (= n 1)
      (loop :for k :from 0 :upto 9
            :do(print k)
            :if (loop :for (x . y) :in lst
                      :with j = (format nil "~A" k)
                      :always (char= y (aref j x)))
              :do (progn (princ k) (return))
            :finally (princ -1))
      (loop :for k :from (expt 10 (1- n)) :upto (1- (expt 10 n))
            :do(print k)
            :if (loop :for (x . y) :in lst
                      :with j = (format nil "~A" k)
                      :always (char= y (aref j x)))
              :do (progn (princ k) (return))
            :finally (princ -1))))