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 Rust (1.15.1) (judge verdict: Runtime Error)

#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}
 
#[allow(unused_macros)]
macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};
 
    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}
 
#[allow(unused_macros)]
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };
 
    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };
 
    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };
 
    ($next:expr, bytes) => {
        read_value!($next, String).into_bytes()
    };
 
    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };
 
    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}
 
use std::cmp::Ordering;
use std::cmp;
use std::cmp::min;
use std::collections::BTreeMap;
use std::process;
use std::cmp::Ord;
use std::collections::HashMap;
use std::collections::HashSet;
use std::collections::VecDeque;
use std::collections::BTreeSet;
use std::mem;
use std::collections::BinaryHeap;
use std::hash::{Hash, Hasher};
fn modpow(x:usize, n:usize, p:usize) -> usize{
        let mut ans = 1;
        let mut n = n;
        let mut x = x;
        while(n != 0){
            if (n&1 == 1){ans = ans*x%p;}
            x = x*x%p;
            n = n>>1;
        }
        ans
}
fn comb(a:usize, b:usize,  fac:&Vec<usize>, ifac:&Vec<usize>, p:usize)->usize{
        let mut a = a;
        let mut b = b;
        if a == 0 && b == 0{return 1;}
        if a<b || a<0{return 0;}
        let mut tmp = ifac[a-b]*ifac[b] % p;
        return tmp * fac[a] % p;
}

fn invs(p:usize)->(Vec<usize>, Vec<usize>){
    let mut ma = 3000001;
    let mut fac = vec![0;300001];
    let mut ifac = vec![0;300001];
    fac[0] = 1;
    ifac[0] = 1;
    for i in 0..ma{
        fac[i+1] = fac[i] * (i+1) % p;
        ifac[i+1] = ifac[i] * modpow(i+1, p - 2, p) % p;
    }
    (fac, ifac)
}
fn kmp_make_table(s:&Vec<char>)->Vec<i64>{
    let mut n = s.len();
    let mut ret = vec![0;n+1];
    ret[0] = -1;
    let mut j = -1;
    for i in 0..n{
        while(j>=0 && s[i] != s[j as usize]){
            j = ret[j as usize];
        }
        j+=1;
        ret[i+1] = j;
    }
    return ret;
}
fn kmp(s:&Vec<char>, word:&Vec<char>)->Vec<usize>{
    let mut table = kmp_make_table(word);
    let mut ret = vec![];
    let mut m = 0;
    let mut i = 0;
    let mut n = s.len() as i64;
    while(m+i<n){
        if word[i as usize] == s[m as usize+i as usize]{
            i+=1;
            if i == word.len() as i64{
                ret.push(m as usize);
                m = m+i-table[i as usize];
                i = table[i as usize];
            }
        }
        else{
            m = m+i-table[i as usize];
            if i>0{
                i = table[i as usize];
            }
        }
    }
    return ret;
}
#[macro_use]
fn solve(){
    //let sssss = std::io::stdin();
    //let mut sc = Scanner { stdin: sssss.lock() };
    input!{
        s:chars,
        t:chars,
    }
    let mut n = s.len();
    while(s.len()<t.len()*2){
        for i in 0..n{
            let c = s[i];
            s.push(c);
        }
    }
    let mut n = s.len();
    for i in 0..n{
            let c = s[i];
            s.push(c);
    }
    let f = kmp(&s, &t);
    let mut dp = vec![-1;s.len()];
    let mut res = -1;
    let mut from = vec![-1;s.len()];
    let mut g= vec![vec![];s.len()];
    for i in 0..f.len(){
        let mut l = f[i];
        let mut nxt = (l+t.len())%s.len();
        g[l].push(nxt);
        from[nxt] = l as i64;
        if dp[l] == -1{
            dp[l] = 0;
            dp[nxt] = dp[l]+1;
            res = cmp::max(res, dp[nxt]);
        }
        else{
            dp[nxt] = dp[l]+1;
            res = cmp::max(res, dp[nxt]);
        }
    }
    let mut used = HashSet::new();
    fn dfs(s:usize, g:&Vec<Vec<usize>>, used:&mut HashSet<usize>)->bool{
        if used.contains(&s){
            return true;
        }
        used.insert(s);
        let mut res = false;
        for i in 0..g[s].len(){
            if dfs(g[s][i], g, used){
                res = true;
            }
        }
        return res;
    }
    if f.len() != 0{
        if dfs(f[0], &g, &mut used){
            println!("-1");
            return;
        }
    }
    if res == -1{
        res = 0;
    }
    println!("{}", res);

}

fn main(){
    solve();
}
const PI:f64 = std::f64::consts::PI;

pub static MOD:i64 = 998244353;
pub static MODu:usize = 998244353;
pub static eps:f64 = 1e-6;
const INF: i64 = 1 << 60;