(defun reads (num)
  (loop as i 
        below num 
        collect (list (read) (read))))

(defun set-num (n l ht)
    (loop as point ; (rank value)
          in l ; (list (rank value))
          do (cond ((< n (car point)) (setf (gethash 'check ht) T)); nより大きい桁に入れようとするとアウト
                   ((and (= n (car point))
                         (= 0 (cadr point))) (setf (gethash 'check ht) T)); nに0を入れようとしたらアウト
                   ((not (gethash (car point) ht)) (setf (gethash (car point) ht) (cadr point))); まだ入っていないのでOK
                   ((not (= (gethash (car point) ht) (cadr point))) (setf (gethash 'check ht) T))))); 入っていて値が異なるのでアウト

(defun get-number (n ht)
  (loop as i
        below n
        collect (* (expt 10 (- n i 1))
                   (if (gethash (+ i 1) ht)
                       (gethash (+ i 1) ht)
                       (if (= i 0) 1 0)))))


(let ((n (read))
      (c (reads (read)))
      (ht (make-hash-table)))
  (set-num n c ht)
  (if (gethash 'check ht)
      (princ (- 0 1))
      (princ (reduce (lambda (m x) (+ m x)) (get-number n ht)))))
