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 C++14 (GCC 5.4.1) (judge verdict: Accepted)

#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

#define F first
#define S second
#define int long long
#define ll long long
//#define int  unsigned long long
#define pb push_back
//#define double long double

using namespace std;
using namespace __gnu_pbds;
typedef  tree< int , null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
const int mod = 1e9 + 7;
const int N = 5000000;
const int K = 350;
int z[N], dp[N], used[N], in[N];
vector < int > g[N];
void zcalc(vector < int > a){
    int l = 0;
    int r = 0;
    int n = a.size();
    for (int i = 1; i < n; i++){
        if (i <= r){
            z[i] = min(z[i - l], r - i + 1);
        }
        while(i + z[i] < n && a[i + z[i]] == a[z[i]]){
                z[i]++;
            }
        if (i + z[i] - 1 > r){
            l = i;
            r = i + z[i] - 1;
        }
    }
}
void dfs(int v){
    in[v] = 1;
    used[v] = 1;
    for (auto u: g[v]){
        if (used[u]){
            if (in[u]){
                cout << -1;
                exit(0);
            }
            dp[v] = max(dp[v], dp[u] + 1);
        }
        dfs(u);
        dp[v] = max(dp[v], dp[u] + 1);
    }
    in[v] = 0;
}
main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    //freopen("input.txt", "r", stdin);
    string s;
    string t;
    cin >> s;
    cin >> t;
    string q = s;
    while(s.length() < t.length()){
        s += q;
    }
    int n = s.length();
    int m = t.length();
    vector < int > a;
    for (int i = 0; i < m; i++){
        a.pb(t[i] - 'a');
    }
    a.pb(50);
    for (int i = 0; i < n + n; i++){
        a.pb(s[i % n] - 'a');
    }
    zcalc(a);
    for (int i = m + 1; i < m + n + n + 1; i++){
        if (z[i] == m){
            int u = i - (m + 1);
            int v = i + m - (m + 1);
            if (v >= n) v -=n;
            g[u].pb(v);
        }
    }
    for (int i = 0; i < n; i++){
        if (!used[i]) dfs(i);
    }
    int ans = 0;
    for (int i = 0; i < n; i++){
        ans = max(ans, dp[i]);
    }
    cout << ans;
}