(defun split (str)
  (labels ((inner (rest-str-list stack-str-list temp-out)
                  (if rest-str-list
                      (let ((now-char (car rest-str-list)))
                        (cond ((eq #\space now-char) (inner (cdr rest-str-list)
                                                            '()
                                                            (cons (concatenate 'string (reverse stack-str-list))
                                                                  temp-out)))
                              (t (inner (cdr rest-str-list)
                                        (cons now-char stack-str-list)
                                        temp-out))))
                      (reverse (cons (concatenate 'string (reverse stack-str-list))
                                     temp-out)))))
          (inner (concatenate 'list str)
                 '()
                 '())))

(defun input-to-list (str)
  (mapcar #'parse-integer (split str)))

(defvar nm (input-to-list (read-line)))
(defvar sc (loop for i from 1 to (cadr nm)
                 collect (input-to-list (read-line))))

(defun setnumlst (num-l index num)
  (if (and (zerop num)
           (eq 1 index)
           (not (eq 1 (length num-l))))
      nil
      (if (nth (1- index) num-l)
          (if (eq num (nth (1- index) num-l))
              num-l
              nil)
          (concatenate 'list (subseq num-l 0 (1- index))
                       `(,num)
                       (subseq num-l (1+ (1- index)))))))


(defun fill-first (num-l)
  (if (car num-l)
      num-l
      (concatenate 'list '(,(if (eq 1 (length num-l)) 0 1)) (subseq num-l 1))))

(defun fill-rest (num-l)
  (loop for num in num-l
        collect (if num
                    num
                    0)))

(defun setup (num-l set-l)
  (if set-l
      (let ((next-num-l (setnumlst num-l (caar set-l) (cadar set-l))))
        (if next-num-l
            (setup next-num-l (cdr set-l))
            nil))
      num-l))

(princ (let ((set-num-l (setup (loop for i from 1 to (car nm) collect nil) sc)))
         (if set-num-l
             (parse-integer (apply #'concatenate (cons 'string (loop for num in (fill-rest (fill-first set-num-l))
                                                                     collect (princ-to-string num)))))
             -1)))