;; -*- 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
;;;
;;; Rolling hash (31-bit)
;;; Use 62-bit version instead. I leave it just for my reference.
;;;

(defstruct (rhash (:constructor %make-rhash (modulus cumul powers)))
  (modulus 1000000007 :type (unsigned-byte 32))
  (cumul nil :type (simple-array (unsigned-byte 32) (*)))
  (powers nil :type (simple-array (unsigned-byte 32) (*))))

(declaim (inline make-rhash))
(defun make-rhash (vector modulus &key (key #'char-code) base)
  "Returns the table of rolling-hash of VECTOR modulo MODULUS. KEY is applied to
  each element of VECTOR prior to computing the hash value.

MODULUS := unsigned 32-bit prime number
BASE := 1 | 2 | ... | MODULUS - 1
KEY := function returning FIXNUM"
  (declare (vector vector)
           ((unsigned-byte 32) modulus)
           ((or null (unsigned-byte 32)) base)
           (function key))
  (assert (sb-int:positive-primep modulus))
  (let* ((base (or base (+ 1 (random (- modulus 1)))))
         (size (length vector))
         (cumul (make-array (+ 1 size) :element-type '(unsigned-byte 32)))
         (powers (make-array (+ 1 size) :element-type '(unsigned-byte 32))))
    (assert (<= 1 base (- modulus 1)))
    (setf (aref powers 0) 1)
    (dotimes (i size)
      (setf (aref powers (+ i 1))
            (mod (* (aref powers i) base) modulus))
      (let ((sum (+ (mod (* (aref cumul i) base) modulus)
                    (mod (the fixnum (funcall key (aref vector i))) modulus))))
        (setf (aref cumul (+ i 1))
              (if (> sum modulus)
                  (- sum modulus)
                  sum))))
    (%make-rhash modulus cumul powers)))

(declaim (inline rhash-query)
         (ftype (function * (values (unsigned-byte 32) &optional)) rhash-query))
(defun rhash-query (rhash l r)
  "Returns the hash value of the interval [L, R)."
  (declare ((integer 0 #.most-positive-fixnum) l r))
  (assert (<= l r))
  (let ((cumul (rhash-cumul rhash))
        (powers (rhash-powers rhash))
        (modulus (rhash-modulus rhash)))
    (let ((res (+ (aref cumul r)
                  (- modulus (mod (* (aref cumul l) (aref powers (- r l))) modulus)))))
      (if (> res modulus)
          (- res modulus)
          res))))

(declaim (inline rhash-concat))
(defun rhash-concat (rhash hash1 hash2 hash2-length)
  (declare ((unsigned-byte 32) hash1 hash2)
           ((integer 0 #.most-positive-fixnum) hash2-length))
  (let* ((modulus (rhash-modulus rhash)))
    (mod (+ hash2
            (* hash1 (aref (rhash-powers rhash) hash2-length)))
         modulus)))

(declaim (ftype (function * (values (unsigned-byte 62) &optional)) rhash-vector-hash)
         (inline rhash-vector-hash))
(defun rhash-vector-hash (rhash vector &key (key #'char-code))
  "Returns the hash code of VECTOR w.r.t. the moduli and bases of RHASH."
  (declare (optimize (speed 3))
           (vector vector)
           (function key))
  (let* ((mod (rhash-modulus rhash))
         (base (aref (rhash-powers rhash) 1))
         (size (length vector))
         (lower 0))
    (declare ((unsigned-byte 32) lower))
    (dotimes (i size)
      (setf lower (+ (mod (* base lower) mod)
                     (mod (the fixnum (funcall key (aref vector i))) mod))))
    lower))

(defun rhash-get-lcp (rhash1 start1 rhash2 start2)
  (declare (optimize (speed 3))
           ((integer 0 #.most-positive-fixnum) start1 start2))
  (assert (= (rhash-modulus rhash1) (rhash-modulus rhash2)))
  (assert (and (< start1 (length (rhash-cumul rhash1)))
               (< start2 (length (rhash-cumul rhash2)))))
  (let ((max-length (min (- (length (rhash-cumul rhash1)) start1 1)
                         (- (length (rhash-cumul rhash2)) start2 1))))
    (declare (optimize (safety 0)))
    (if (= (rhash-query rhash1 start1 (+ start1 max-length))
           (rhash-query rhash2 start2 (+ start2 max-length)))
        max-length
        (labels ((bisect (ok ng)
                   (declare ((integer 0 #.most-positive-fixnum) ok ng))
                   (if (<= (- ng ok) 1)
                       ok
                       (let ((mid (ash (+ ng ok) -1)))
                         (if (= (rhash-query rhash1 start1 (+ start1 mid))
                                (rhash-query rhash2 start2 (+ start2 mid)))
                             (bisect mid ng)
                             (bisect ok mid))))))
          (bisect 0 max-length)))))

(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 ()
  (declare #.OPT)
  (let* ((ss (coerce (read-line) 'simple-base-string))
         (ts (coerce (read-line) 'simple-base-string))
         (slen (length ss))
         (tlen (length ts))
         (snum (max 3 (ceiling (* tlen 3) slen)))
         (ex-ss (make-string (* snum slen) :element-type 'base-char))
         (tnum (floor (* snum slen) tlen))
         (ex-ts (make-string (* tnum tlen) :element-type 'base-char)))
    (declare (uint31 slen tlen snum tnum))
    (dotimes (i slen)
      (dotimes (fac snum)
        (setf (aref ex-ss (+ i (* fac slen)))
              (aref ss i))))
    (dotimes (i tlen)
      (dotimes (fac tnum)
        (setf (aref ex-ts (+ i (* fac tlen)))
              (aref ts i))))
    (let* ((rhash (make-rhash ex-ss 4294967291 :base 2095716802))
           (total-len (* snum slen))
           (thash (rhash-vector-hash rhash ts))
           (base 0))
      (block maybe-infinite
        (loop (unless (<= (+ base tlen) total-len)
                (return))
              (when (= thash (rhash-query rhash base (+ base tlen)))
                ;; possibly infinite
                (loop for i from base by tlen
                      while (<= (+ i tlen) total-len)
                      do (unless (= thash (rhash-query rhash i (+ i tlen)))
                           (return-from maybe-infinite))
                      finally (let* ((l base)
                                     (r i)
                                     (dif (+ l (- total-len r))))
                                (if (<= dif (* 2 (- tlen 1)))
                                    (progn (println -1)
                                           (return-from main))
                                    (return-from maybe-infinite)))))
              (incf base)))
      #>total-len
      ;; not infinite
      (let ((res 0)
            (rhash2 (make-rhash ex-ts 4294967291 :base 2095716802)))
        (declare (uint31 res))
        (dotimes (pos total-len)
          (let ((length (rhash-get-lcp rhash pos rhash2 0)))
            (setf res (max res (floor length tlen)))))
        (println res)))))

#-swank (main)


Measured memory_bytes: 72296