(defun get-input-array2 (n m)
  (let ((rst (make-array (list n m))))
    (loop for x from 0 to (- n 1)
          do (loop for y from 0 to (- m 1)
                   ;do (format t "~A ~A ~%" x y)
                   do (setf (aref rst x y) (read))))
    rst))


(defun main (n m)
  (let* ((input-array (get-input-array2 m 2))
         (rst-array (make-array n :initial-element 0))
         (rst 0))
    (loop for x from 0 to (- m 1)
          for si =  (- (aref input-array x 0) 1)
          for ci = (aref input-array x 1)
          if (or (and (not (equal (aref rst-array si) 0)) (not (equal ci (aref rst-array si))))
                 (and (equal si 0) (equal ci 0) (not (equal n 1))))
          do (progn (setf rst -1)
               (return))
          do (setf (aref rst-array si) ci)
          ;do (format t "1: ~A ~A ~A~%" x si ci)
          finally (if (equal (aref rst-array 0) 0)
                      (setf (aref rst-array 0) 1)))
    (loop for x downfrom (- n 1) downto 0
          for y from 0 to n
          for z = (aref rst-array y)
          if (equal rst -1)
          do (return)
          ;do (format t "2: ~A ~A ~A ~%" x y z)
          do (setf rst (+ rst (* z (expt 10 x)))))
    rst))

          
    

(format t "~A~%" (main (read) (read)))

