;; -*- coding: utf-8 -*-
(eval-when (:compile-toplevel :load-toplevel :execute)
  (sb-int:defconstant-eqx OPT
    #+swank '(optimize (speed 3) (safety 2))
    #-swank '(optimize (speed 3) (safety 0) (debug 0))
    #'equal)
  #+swank (ql:quickload '(:cl-debug-print :fiveam) :silent t)
  #-swank (set-dispatch-macro-character
           #\# #\> (lambda (s c p) (declare (ignore c p)) (read s nil nil t))))
#+swank (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)
#-swank (disable-debugger) ; for CS Academy

;; BEGIN_INSERTED_CONTENTS
(defstruct (queue (:constructor make-queue
                    (&optional list &aux (tail (last list)))))
  (list nil :type list)
  (tail nil :type (or null (cons t null))))

(declaim (inline enqueue))
(defun enqueue (obj queue)
  "Pushes OBJ to the end of QUEUE."
  (symbol-macrolet ((list (queue-list queue))
                    (tail (queue-tail queue)))
    (if (null list)
        (setf tail (list obj)
              list tail)
        (setf (cdr tail) (list obj)
              tail (cdr tail))))
  queue)

(declaim (inline dequeue))
(defun dequeue (queue)
  "Removes and returns the element at the front of QUEUE. Returns NIL if QUEUE
is empty."
  (pop (queue-list queue)))

(declaim (inline queue-empty-p))
(defun queue-empty-p (queue)
  (null (queue-list queue)))

(declaim (inline enqueue-front))
(defun enqueue-front (obj queue)
  "Pushes OBJ to the front of QUEUE."
  (symbol-macrolet ((list (queue-list queue))
                    (tail (queue-tail queue)))
    (if (null list)
        (setf tail (list obj)
              list tail)
        (push obj list))
    queue))

(declaim (ftype (function * (values fixnum &optional)) read-fixnum))
(defun read-fixnum (&optional (in *standard-input*))
  (declare #.OPT)
  (macrolet ((%read-byte ()
               `(the (unsigned-byte 8)
                     #+swank (char-code (read-char in nil #\Nul))
                     #-swank (sb-impl::ansi-stream-read-byte in nil #.(char-code #\Nul) nil))))
    (let* ((minus nil)
           (result (loop (let ((byte (%read-byte)))
                           (cond ((<= 48 byte 57)
                                  (return (- byte 48)))
                                 ((zerop byte) ; #\Nul
                                  (error "Read EOF or #\Nul."))
                                 ((= byte #.(char-code #\-))
                                  (setf minus t)))))))
      (declare ((integer 0 #.most-positive-fixnum) result))
      (loop
        (let* ((byte (%read-byte)))
          (if (<= 48 byte 57)
              (setq result (+ (- byte 48)
                              (* 10 (the (integer 0 #.(floor most-positive-fixnum 10)) result))))
              (return (if minus (- result) result))))))))

(defmacro dbg (&rest forms)
  #+swank
  (if (= (length forms) 1)
      `(format *error-output* "~A => ~A~%" ',(car forms) ,(car forms))
      `(format *error-output* "~A => ~A~%" ',forms `(,,@forms)))
  #-swank (declare (ignore forms)))

(defmacro define-int-types (&rest bits)
  `(progn
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "UINT~A" b)) () '(unsigned-byte ,b))) bits)
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "INT~A" b)) () '(signed-byte ,b))) bits)))
(define-int-types 2 4 7 8 15 16 31 32 62 63 64)

(declaim (inline println))
(defun println (obj &optional (stream *standard-output*))
  (let ((*read-default-float-format* 'double-float))
    (prog1 (princ obj stream) (terpri stream))))

(defconstant +mod+ 1000000007)

;;;
;;; Body
;;;

(defconstant +inf+ #xffffffff)

(defun calc-min-dists (src n graph)
  (declare #.OPT
           ((simple-array list (*)) graph)
           (uint32 n src))
  (let ((que (make-queue))
        (dists (make-array n :element-type 'uint32 :initial-element +inf+))
        (init t))
    (enqueue src que)
    (loop until (queue-empty-p que)
          for current of-type uint32 = (dequeue que)
          for dist = (if init 0 (aref dists current))
          do (setq init nil)
             (dolist (neighbor (aref graph current))
               (declare (uint32 neighbor))
               (when (= +inf+ (aref dists neighbor))
                 (setf (aref dists neighbor) (+ dist 1))
                 (enqueue neighbor que))))
    dists))

(defun main ()
  (declare #.OPT)
  (let* ((n (read))
         (m (read))
         (graph (make-array n :element-type 'list :initial-element nil))
         (min-len +inf+)
         (min-src 0)
         min-dists)
    (declare (uint32 n m min-len min-src))
    (dotimes (i m)
      (let ((a (- (read-fixnum) 1))
            (b (- (read-fixnum) 1)))
        (declare (uint32 a b))
        (push b (aref graph a))))
    (dotimes (i n)
      (let* ((dists (calc-min-dists i n graph))
             (len (aref dists i)))
        (declare ((simple-array uint32 (*)) dists)
                 (uint32 len))
        (when (< len min-len)
          (setq min-len len
                min-src i
                min-dists dists))))
    (if (= min-len +inf+)
        (println -1)
        (let* ((marked (make-array n :element-type 'bit :initial-element 0))
               (path
                 (block dfs
                   (sb-int:named-let recur ((v min-src) (path nil) (init t))
                     (unless init
                       (when (= v min-src)
                         (return-from dfs (nreverse path)))
                       (setf (aref marked v) 1))
                     (let ((dist (if init 0 (aref min-dists v))))
                       (dolist (neighbor (aref graph v))
                         (when (and (zerop (aref marked neighbor))
                                    (= (aref min-dists neighbor) (+ dist 1)))
                           (recur neighbor (cons neighbor path) nil))))))))
          (declare ((simple-array uint32 (*)) min-dists))
          (println min-len)
          (dolist (v path) (println (+ v 1)))))))

#-swank (main)


Measured memory_bytes: 35432