;; -*- coding: utf-8 -*-
(eval-when (:compile-toplevel :load-toplevel :execute)
  (defparameter OPT
    #+swank '(optimize (speed 3) (safety 2))
    #-swank '(optimize (speed 3) (safety 0) (debug 0)))
  #+swank (ql:quickload '(:cl-debug-print :fiveam))
  #-swank (set-dispatch-macro-character #\# #\> (lambda (s c p) (declare (ignore c p)) (read s nil (values) 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

(defun main ()
  (let* ((n (read))
         (m (read))
         ;; (index . dist)
         (que (make-queue))
         (graph (make-array (+ n (* 2 m)) :element-type 'list :initial-element nil))
         (marked (make-array (+ n (* 2 m)) :element-type 'bit :initial-element 0))
         (indexer (make-hash-table :test #'equal :size (* 4 m)))
         (transfers (make-hash-table :test #'eq))
         (base 0))
    (declare (uint31 n m base))
    (dotimes (i m)
      (let* ((p (- (read-fixnum) 1))
             (q (- (read-fixnum) 1))
             (c (read-fixnum))
             (pnode (cons p c))
             (qnode (cons q c)))
        (unless (gethash pnode indexer)
          (setf (gethash pnode indexer) base)
          (incf base))
        (unless (gethash qnode indexer)
          (setf (gethash qnode indexer) base)
          (incf base))
        (unless (gethash p indexer)
          (setf (gethash p indexer) base)
          (incf base))
        (unless (gethash q indexer)
          (setf (gethash q indexer) base)
          (incf base))
        (let ((pindex (gethash pnode indexer))
              (qindex (gethash qnode indexer))
              (ptindex (gethash p indexer)) ; transfer for p
              (qtindex (gethash q indexer))); transfer for q
          (push pindex (aref graph qindex))
          (push qindex (aref graph pindex))
          (push ptindex (aref graph pindex))
          (push pindex (aref graph ptindex))
          (push qtindex (aref graph qindex))
          (push qindex (aref graph qtindex))
          (setf (gethash ptindex transfers) t)
          (setf (gethash qtindex transfers) t))))
    (unless (and (gethash 0 indexer) (gethash (- n 1) indexer))
      (println -1)
      (return-from main))
    (let ((src (gethash 0 indexer))
          (dest (gethash (- n 1) indexer)))
      (declare (uint32 src dest))
      (enqueue (cons src 0) que)
      (loop until (queue-empty-p que)
            for (index . dist) of-type (uint32 . uint32) = (dequeue que)
            do (when (= index dest)
                 (println dist)
                 (return-from main))
               (when (zerop (aref marked index))
                 (setf (aref marked index) 1)
                 (dolist (neighbor (aref graph index))
                   (when (zerop (aref marked neighbor))
                     (if (gethash index transfers)
                         (enqueue (cons neighbor (+ 1 dist)) que)
                         (enqueue-front (cons neighbor dist) que)))))))
    (println -1)))

#-swank (main)


Measured memory_bytes: 96868