{-# OPTIONS_GHC -O2 #-}
{-# LANGUAGE BangPatterns          #-}
{-# LANGUAGE CPP                   #-}
{-# LANGUAGE LambdaCase            #-}
{-# LANGUAGE MagicHash             #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE MultiWayIf            #-}
{-# LANGUAGE OverloadedStrings     #-}
{-# LANGUAGE TupleSections         #-}
{-# LANGUAGE TypeFamilies          #-}
{-# LANGUAGE ViewPatterns          #-}

import           Control.Applicative
import           Control.Exception
import           Control.Monad
import           Control.Monad.Primitive
import           Control.Monad.ST
import           Control.Monad.State.Strict
import           Data.Bool
import qualified Data.ByteString             as B
import qualified Data.ByteString.Builder     as B
import qualified Data.ByteString.Char8       as C
import qualified Data.ByteString.Internal    as B
import qualified Data.ByteString.Unsafe      as B
import           Data.Char
import qualified Data.Foldable               as F
import           Data.Function
import qualified Data.IntMap.Strict          as IM
import qualified Data.IntSet                 as IS
import qualified Data.List                   as L
import qualified Data.Map.Strict             as M
import           Data.Monoid
import           Data.Ord
import qualified Data.Primitive.ByteArray    as BA
import           Data.Primitive.MutVar
import qualified Data.Primitive.Types        as P
import qualified Data.Set                    as S
import           Data.Tuple
import qualified Data.Vector                 as V
import qualified Data.Vector.Generic         as G
import qualified Data.Vector.Generic.Mutable as GM
import qualified Data.Vector.Primitive       as PV
import qualified Data.Vector.Unboxed         as U
import qualified Data.Vector.Unboxed.Base    as U
import qualified Data.Vector.Unboxed.Mutable as UM
import           Debug.Trace
import           Foreign                     hiding (void)
import           GHC.Exts
import qualified System.IO                   as IO
import           Unsafe.Coerce

main :: IO ()
main = do
    s <- C.getLine
    t <- C.getLine
    print $ solve t s

solve :: C.ByteString -> C.ByteString -> Int
solve pat text
    | lenT < lenS = solve pat $ C.concat $ replicate (div (lenS + lenT - 1) lenT) text
    | otherwise = solve' lenS lenT $ zAlgorithm $ C.concat [pat, "$", text, text]
  where
    !lenS = C.length pat
    !lenT = C.length text

solve' lenS lenT z = runST $ do
    uf <- newUnionFind lenT
    let go ((src,dst):es) = do
          merged <- uniteM uf src dst
          if merged
          then go es
          else return (-1)
        go [] = do
            max 0.subtract 1.U.maximum <$> U.mapM (sizeM uf) (U.generate lenT id)
    go $ U.toList edges
  where
    n = lenT
    edges = U.map (\(i, _) -> (i, rem (i + lenS) n))
        . U.filter ((>=lenS).snd)
        . U.indexed
        $ U.slice (lenS + 1) lenT z

rep :: Monad m => Int -> (Int -> m ()) -> m ()
rep n = U.forM_ (U.generate n id)
{-# INLINE rep #-}

zAlgorithm :: B.ByteString -> U.Vector Int
zAlgorithm bs = runST $ do
    z <-  BA.newByteArray (sizeOf (undefined :: Int) * n)
    BA.fillByteArray z 0 (BA.sizeofMutableByteArray z) 0
    lr <- BA.newByteArray (sizeOf (undefined :: Int) * 2)
    BA.writeByteArray lr 0 (0 :: Int)
    BA.writeByteArray lr 1 (0 :: Int)
    U.forM_ (U.generate (n-1) (+1)) $ \i -> do
        r <- BA.readByteArray lr 1
        if i > r
        then do
            let r' = searchR i i
            BA.writeByteArray z i (r' - i)
            BA.writeByteArray lr 0 i
            BA.writeByteArray lr 1 (r' - 1)
        else do
            l <- BA.readByteArray lr 0
            let k = i - l
            zk <- BA.readByteArray z k
            if zk < r - i + 1
            then do
                BA.writeByteArray z i zk
            else do
                let r' = searchR i r
                BA.writeByteArray z i (r' - i)
                BA.writeByteArray lr 0 i
                BA.writeByteArray lr 1 (r' - 1)
    ba <- BA.unsafeFreezeByteArray z
    return $ coerce $ PV.Vector 0 n ba
  where
    !n = B.length bs
    searchR :: Int -> Int -> Int
    searchR l = go
      where
        go !r
            | r < n, B.unsafeIndex bs (r - l) == B.unsafeIndex bs r = go (r + 1)
            | otherwise = r

-------------------------------------------------------------------------------

newtype UnionFind m = UF { parent :: UM.MVector m Int }

newUnionFind :: PrimMonad m => Int -> m (UnionFind (PrimState m))
newUnionFind n = UF <$> UM.replicate n (-1)
{-# INLINE newUnionFind #-}

findM :: PrimMonad m => UnionFind (PrimState m) -> Int -> m Int
findM uf x = go x return
  where
    go !x k = do
        px <- UM.unsafeRead (parent uf) x
        if px < 0
        then k x
        else go px $ \ppx -> do
            UM.unsafeWrite (parent uf) x ppx
            k ppx
{-# INLINE findM #-}

sizeM :: PrimMonad m => UnionFind (PrimState m) -> Int -> m Int
sizeM uf = fix $ \loop x -> do
    px <- UM.unsafeRead (parent uf) x
    if px < 0
    then return $! negate px
    else loop px
{-# INLINE sizeM #-}

uniteM :: PrimMonad m => UnionFind (PrimState m) -> Int -> Int -> m Bool
uniteM uf x y = do
    px <- findM uf x
    py <- findM uf y
    if px == py
    then return False
    else do
        rx <- UM.unsafeRead (parent uf) px
        ry <- UM.unsafeRead (parent uf) py
        if rx < ry
        then do
            UM.unsafeModify (parent uf) (+ry) px
            UM.unsafeWrite (parent uf) py px
        else do
            UM.unsafeModify (parent uf) (+rx) py
            UM.unsafeWrite (parent uf) px py
        return True
{-# INLINE uniteM #-}

equivM :: PrimMonad m => UnionFind (PrimState m) -> Int -> Int -> m Bool
equivM uf x y = (==) `liftM` findM uf x `ap` findM uf y
{-# INLINE equivM #-}

-- | O(n)
countGroupM :: PrimMonad m => UnionFind (PrimState m) -> m Int
countGroupM uf = U.length . U.filter (<0) <$> U.unsafeFreeze (parent uf)
{-# INLINE countGroupM #-}

type Parser a = C.ByteString -> Maybe (a, C.ByteString)

parseInt :: Parser Int
parseInt = C.readInt . C.dropWhile isSpace

parseInt2 :: Parser (Int, Int)
parseInt2 = runStateT $
    (,) <$> StateT (C.readInt . C.dropWhile isSpace)
        <*> StateT (C.readInt . B.unsafeTail)

parseInt3 :: Parser (Int, Int, Int)
parseInt3 = runStateT $
    (,,) <$> StateT (C.readInt . C.dropWhile isSpace)
        <*> StateT (C.readInt . B.unsafeTail)
        <*> StateT (C.readInt . B.unsafeTail)

parseInt4 :: Parser (Int, Int, Int, Int)
parseInt4 = runStateT $
    (,,,) <$> StateT (C.readInt . C.dropWhile isSpace)
        <*> StateT (C.readInt . B.unsafeTail)
        <*> StateT (C.readInt . B.unsafeTail)
        <*> StateT (C.readInt . B.unsafeTail)

Measured memory_bytes: 42364