Problem:
Score : 600 points

Problem Statement

Given are two strings s and t consisting of lowercase English letters. Determine if the number of non-negative integers i satisfying the following condition is finite, and find the maximum value of such i if the number is finite.

There exists a non-negative integer j such that the concatenation of i copies of t is a substring of the concatenation of j copies of s.

Notes

A string a is a substring of another string b if and only if there exists an integer x (0 \leq x \leq |b| - |a|) such that, for any y (1 \leq y \leq |a|), a_y = b_{x+y} holds.

We assume that the concatenation of zero copies of any string is the empty string. From the definition above, the empty string is a substring of any string. Thus, for any two strings s and t, i = 0 satisfies the condition in the problem statement.

Constraints

1 \leq |s| \leq 5 \times 10^5

1 \leq |t| \leq 5 \times 10^5

s and t consist of lowercase English letters.

Input

Input is given from Standard Input in the following format:

s
t

Output

If the number of non-negative integers i satisfying the following condition is finite, print the maximum value of such i; if the number is infinite, print -1.

Sample Input 1

abcabab
ab

Sample Output 1

3

The concatenation of three copies of t, ababab, is a substring of the concatenation of two copies of s, abcabababcabab, so i = 3 satisfies the condition.

On the other hand, the concatenation of four copies of t, abababab, is not a substring of the concatenation of any number of copies of s, so i = 4 does not satisfy the condition.

Similarly, any integer greater than 4 does not satisfy the condition, either. Thus, the number of non-negative integers i satisfying the condition is finite, and the maximum value of such i is 3.

Sample Input 2

aa
aaaaaaa

Sample Output 2

-1

For any non-negative integer i, the concatenation of i copies of t is a substring of the concatenation of 4i copies of s. Thus, there are infinitely many non-negative integers i that satisfy the condition.

Sample Input 3

aba
baaab

Sample Output 3

0

As stated in Notes, i = 0 always satisfies the condition.

Solution in Haskell (GHC 7.10.3) (judge verdict: Time Limit Exceeded)


-- https://atcoder.jp/contests/abc135/tasks/abc135_f

junkanstr :: Int -> String -> String
junkanstr i s =
    s1 ++ s2
    where 
        l = length s
        i2 = mod i l
        s1 = drop i2 s
        s2 = take i2 s

junkancomp :: String -> String -> Bool
junkancomp s1 s2 =
    if l1 == l2
    then
        any (\i -> s1 == (junkanstr i s2)) [0..(l1-1)]
    else
        False
    where 
        l1 = length s1
        l2 = length s2

func3 :: String -> String
func3 s =

    func2 1

    where 
        sl = length s

        func2 l =
            if q2 == 0 
            then 
                if s2 == s
                then
                    s3
                else
                    func2 (1 + l)
            else 
                func2 (1 + l)
            where 
                (q1,q2) = quotRem sl l
                s3 = take l s 
                s2 = concat $ replicate q1 s3

bubun :: String -> String  -> Bool
bubun s1 s2 =
    if ld < 0
    then
        False
    else
        any (\i -> s1 == (take l1 $ drop i s2)) [0..ld]
    where
        l1 = length s1
        l2 = length s2
        ld = l2 - l1

bubun2 :: String -> String  -> Int
bubun2 s1 s2 =
    inner $ div (length s2) (length s1)
    where
        inner 0 = 0
        inner n = 
            if result
            then
                n
            else
                inner (n-1)
            where 
                s = concat $ replicate n s1
                result = bubun s s2

myround :: [a] -> [[a]]
myround [] = [[]]
myround arr =
    map (\i -> (take (l-i) $ drop i arr) ++ take i arr) [0..(l-1)]
    where 
        l = length arr

main :: IO ()
main = do
    str_s <- getLine    
    str_t <- getLine    

    let len_s = length str_s
    let len_t = length str_t

    let l_s = func3 str_s
    let l_t = func3 str_t

    let count = div len_t len_s

    if junkancomp l_s l_t 
    then 
        print (-1)
    else
        print $ bubun2 str_t (concat $ replicate (2+count) str_s )