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)

#include<bits/stdc++.h>

#define ll long long
#define fornum(A,B,C) for(A=B;A<C;A++)
#define mp make_pair
#define pii pair<int,int>
#define pll pair<ll,ll>

using namespace std;

/////////////////////////////////////////////////////
#define MOD (ll)(1e9+7)



char S[505050], T[505050];
ll TZ[505050];
ll mt[505050], mk2[505050];
bool mk[60][505050];
ll i,j,k,ans;

void ZA(char *st){
    ll len=strlen(st);
    TZ[0] = len;
    ll i = 1, l = 0,r=1;
    //printf("%lld ",TZ[0]);
    fornum(i,1,len){
        if(i+TZ[i-l]<r){
            TZ[i] = TZ[i - l];
        }else{
            //printf("%lld:%lld ", i, r);
            l = i;
            while (r< len && st[r - l] == st[r])
                r++;
            TZ[i] = r - l;
            if(r==i){
                r++;
            }
        }
        //printf("%lld:%lld ", TZ[i],r);
    }
}

int main(){
    scanf("%s%s", S,T);
    ZA(T);
    ll Slen = strlen(S);
    ll Tlen = strlen(T);
    
    ll l=0,r=0;
    
    fornum(i,0,Slen){
        if(i+TZ[i-l]<r){
            mt[i] = TZ[i - l];
        }else{
            l = i;
            while(r-l<Tlen&&T[r-l]==S[r%Slen])
                r++;
            mt[i] = r - l;
            if(i==r){
                r++;
            }
        }
    }
    
    
    queue<ll> q;
    fornum(i,0,Slen){
        if(mt[i]==Tlen){
            mk[0][i] = true;
            mk2[i] = 1;
            q.push(i);
        }
    }
    if(q.size()==Slen){
        printf("-1");
        return 0;
    }
    
    fornum(i,0,20){
        ll a = q.size();
        fornum(j,0,a){
            ll qa = q.front();
            q.pop();
            if(mk[i][qa]&&mk[i][(qa+(1ll<<i)*Tlen)%Slen]){
                mk[i + 1][qa] = true;
                mk2[qa] *= 2;
                q.push(qa);
            }
        }
    }

    fornum(i,0,Slen){
        if(mk2[i]==(1ll<<20)){
            printf("-1");
            return 0;
        }
        ll a = mk2[i];
        ll b = (i+mk2[i] * Tlen) % Slen;
        while(mk2[b]){
            //printf("%lld ", b);
            a += mk2[b];
            b = (b + mk2[b] * Tlen) % Slen;
        }
        if(ans<a)
            ans = a;
        //printf(":%lld,%lld\n", i, ans);
    }
    printf("%lld", ans);

    return 0;
}