(defun splitat (c line)
  (labels ((rec (line acc)
				(let ((pos (position-if (lambda(cc) (char= c cc)) line)))
				  (if pos
					  (rec (subseq line (1+ pos)) (cons (subseq line 0 pos) acc))
					(nreverse (cons line acc))))))
	(rec line nil)))
(defun f(n m lst notfoundcallback foundcallback)
  (let ((num (loop for i from 1 to n collect 0)))
    (labels ((rec (num lst)
                  (if (null lst)
                    (funcall foundcallback (reduce #'+ (mapcar #'* num (loop for i from 1 to n collect (expt 10 (- n i))))))
                    (if (or (zerop (nth (1- (car (car lst))) num))
                            (= (nth (1- (car (car lst))) num) (cadr (car lst))))
                      (progn
                        (setf (nth (1- (car (car lst))) num) (cadr (car lst)))
                        (rec num (cdr lst)))
                      (funcall notfoundcallback))
                      )))
      (rec num lst))))
(let* ((nm (mapcar #'parse-integer (splitat #\space (read-line nil nil))))
       (lst nil))
  (dotimes (i (cadr nm))
    (setq lst (cons (mapcar #'parse-integer (splitat #\space (read-line nil nil))) lst)))
  (f (car nm) (cadr nm) lst (lambda() (format t "-1~%")) (lambda(n) (format t "~A~%" n))))
