(declaim (optimize (speed 3) (safety 0) (debug 0)))

(defconstant +loop-count+ 1000000)
(defconstant +expected-checksum+ 3875011)

(defstruct array-record
  (digits #(3 1 4 1 5 9 2 6) :type simple-vector))

(declaim (ftype (function () fixnum) configured-loop-count))
(defun configured-loop-count ()
  (handler-case
      (let ((line (read-line *standard-input* nil nil)))
        (if line
            (let ((value (parse-integer line :junk-allowed t)))
              (if (> value 0) value +loop-count+))
            +loop-count+))
    (error () +loop-count+)))

(declaim (ftype (function (fixnum) fixnum) array-struct-field-loop))
(defun array-struct-field-loop (limit)
  (declare (type fixnum limit))
  (let ((record (make-array-record)))
    (loop with i of-type fixnum = 0
          with acc of-type fixnum = 11
          while (< i limit)
          do (let* ((digits (array-record-digits record))
                    (next (+ acc (svref digits (rem i 8))))
                    (bounded (if (> next 1000000000)
                                 (- next 1000000000)
                                 next)))
               (declare (type simple-vector digits)
                        (type fixnum next bounded))
               (setf acc bounded
                     i (+ i 1)))
          finally (return acc))))

(let ((result (array-struct-field-loop (configured-loop-count))))
  (format t "~D~%" result)
  (sb-ext:exit :code (if (= result +expected-checksum+) 0 1)))
