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)

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

#[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, usize1) => {
        read_value!($next, usize) - 1
    };

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

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        #[cfg(debug_assertions)]
        writeln!(&mut std::io::stderr(), concat!("[DEBUG] ", $(stringify!($a), "={:?} "),*), $($a),*);
    }
}

#[allow(unused_imports)]
use std::cmp::{min, max};
#[allow(unused_imports)]
use std::io::{stdout, stdin, BufWriter, Write};

fn make_table(s: &Vec<char>) -> Vec<i64> {
    let n = s.len();
    let mut t = vec![0; n];
    t[0] = -1;

    let mut pos = 0;
    for i in 2..n {
        let prev = s[i-1];
        if prev == s[pos] {
            pos += 1;
            t[i] = pos as i64;
        } else {
            pos = 0;
            t[i] = 0;
        }
    }
    t
}

fn main() {
    let out = std::io::stdout();
    let mut out = BufWriter::new(out.lock());
    macro_rules! puts {
        ($($format:tt)*) => (write!(out,$($format)*).unwrap());
    }

    input!{
      s: chars,
      t: chars,
    }
    // check if the string contains only one type of letter.
    {
        let mut s = s.clone();
        let mut t = t.clone();
        s.sort();
        s.dedup();
        t.sort();
        t.dedup();
        if s.len() == 1 && t.len() == 1 && s[0] == t[0] {
            return puts!("{}\n", -1);
        }
    }

    let n = s.len();
    let m = t.len();
    let tbl = make_table(&t);

    let mut s_idx = 0;
    let mut ans = 0;
    let mut cur_ans = 0;
    let mut offset = 0;
    let mut partial = vec![false; n];
    while s_idx < n {
        let mut t_idx = offset;
        let mut ok = true;
        while t_idx < m {
            if s[(s_idx + t_idx) % n] != t[t_idx] {
                ok = false;
                break;
            }
            t_idx += 1;
        }
        if ok {
            partial[s_idx] = true;
            cur_ans += 1;
            ans = max(ans, cur_ans);
            s_idx += t_idx;
            offset = 0;
        } else {
            cur_ans = 0;
            s_idx = ((s_idx + t_idx) as i64 - tbl[t_idx]) as usize;
            offset = max(tbl[t_idx], 0) as usize;
        }
    }

    use std::collections::VecDeque;
    let mut q = VecDeque::new();
    let mut cnts = vec![0; n];
    let mut cur = 0;
    let mut infinite = true;
    loop {
        if partial[cur] {
            q.push_back(cur);
            cur += t.len();

            if cur >= n {
                let mut cnt = cnts[cur%n];
                if cnt == 0 {
                    infinite = false;
                }
                while let Some(v) = q.pop_back() {
                    cnt += 1;
                    cnts[v] = cnt;
                }
                break;
            }
        } else {
            infinite = false;
            let mut cnt = 0;
            while let Some(v) = q.pop_back() {
                cnt += 1;
                cnts[v] = cnt;
            }
            cur += 1;
            if cur >= n {
                break;
            }
        }
    }

    if infinite {
        puts!("{}\n", -1);
    } else {
        let ans = cnts.iter().max().unwrap();
        puts!("{}\n", ans);
    }
}