(defun main (&rest argv) (declare (ignorable argv)) (let* ((n (read)) (dist (make-array 10001 :initial-element 0)) (visited (make-array 10001 :initial-element nil)) (deque (make-array 400000 :element-type 'integer)) (front-index 200000) (rear-index 200000)) (setf (aref visited 1) t) (setf (aref deque rear-index) 1 rear-index (1+ rear-index)) (loop while (< front-index rear-index) do (let* ((p 0) (x (aref deque front-index)) (m x)) (incf front-index) (loop while (/= m 0) do (incf p (logand m 1)) (setf m (ash m -1))) (let ((ip (+ x p)) (im (- x p))) (when (and (<= ip n) (not (aref visited ip))) (setf (aref visited ip) t (aref dist ip) (1+ (aref dist x)) (aref deque rear-index) ip rear-index (1+ rear-index))) (when (and (> im 0) (not (aref visited im))) (setf (aref visited im) t (aref dist im) (1+ (aref dist x)) (aref deque rear-index) im rear-index (1+ rear-index)))))) (format t "~d~%" (if (aref visited n) (1+ (aref dist n)) -1)))) (main)