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: Wrong Answer)

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)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };

    ($next:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

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, [ $t:tt ]) => {
        {
            let len = read_value!($next, usize);
            (0..len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
        }
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_imports)]
use std::cmp::{min, max};
use std::collections::HashMap;

fn main() {
    input!{
        s: chars,
        t: chars
    }
    let sl = s.len();
    let tl = t.len();
    let s: Vec<char> = s.iter().cycle().take(sl + tl - 1).map(|x| *x).collect();
    let mut m = vec![false; sl];
    kmp(&s, &t, &mut m);
    if sl == 1 && m[0] == true {
        println!("-1");
        return;
    }
    let mut smap: HashMap<usize, ((usize, usize), usize)> = HashMap::new();
    let mut emap: HashMap<usize, ((usize, usize), usize)> = HashMap::new();
    let mut ans: usize = 0;
    for i in 0..sl {
        if m[i] {
            let j = (i + tl) % sl;
            if let Some(&((ss, _i), l1)) = emap.get(&i) {
                if let Some(&((_j, tt), l2)) = smap.get(&j) {
                    if ss == j && tt == i {
                        println!("-1");
                        return;
                    }
                    smap.remove(&j);
                    emap.remove(&i);
                    smap.insert(ss, ((ss, tt), l1 + l2 + 1));
                    emap.insert(tt, ((ss, tt), l1 + l2 + 1));
                    ans = max(ans, l1 + l2 + 1);
                } else {
                    emap.remove(&i);
                    smap.insert(ss, ((ss, j), l1 + 1));
                    emap.insert(j, ((ss, j), l1 + 1));
                    ans = max(ans, l1 + 1);
                }
            } else {
                if let Some(&((_j, tt), l2)) = smap.get(&j) {
                    smap.remove(&j);
                    smap.insert(i, ((i, tt), l2 + 1));
                    emap.insert(tt, ((i, tt), l2 + 1));
                    ans = max(ans, l2 + 1);
                } else {
                    smap.insert(i, ((i, j), 1));
                    emap.insert(j, ((i, j), 1));
                    ans = max(ans, 1);
                }
            }
        }
    }
    println!("{}", ans);
}

fn kmp(s: &[char], sub: &[char], mat: &mut [bool]) {
    let m = sub.len();
    let mut tab: Vec<usize> = vec![0; m + 1];
    let mut i = 2;
    let mut j = 0;
    while i <= m {
        if sub[i - 1] == sub[j] {
            tab[i] = j + 1;
            i += 1;
            j += 1;
        } else if j > 0 {
            j = tab[j];
        } else {
            i += 1;
        }
    }
    i = 0;
    j = 0;
    while i + j < s.len() {
        if sub[j] == s[i + j] {
            j += 1;
            if j == m {
                mat[i] = true;
                i += j - tab[j];
                j = tab[j];
            }
        } else {
            if j == 0 {
                i += 1;
            } else {
                i += j - tab[j];
                j = tab[j];
            }
        }
    }
}