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

#include<bits/stdc++.h>
using namespace std;
//万合以致，向阳而生——背叛时代，忠于时间//
#pragma GCC optimize(3,"Ofast","inline")
typedef long long ll;
typedef pair<int,int> pi;
typedef vector<int> vi;
typedef unsigned long long ull;

#define f first
#define s second
#define pb push_back
#define mp make_pair
#define forn(i, n) for(register int i=0; i<n; i++)
#define Forn(i, n) for(register int i=1; i<=n; i++)
#define foreach(it, a) for(__typeof((a).begin()) it=(a).begin(); it!=(a).end(); it++)
#define Foreach(it, a) for(__typeof((a).rbegin()) it=(a).rbegin(); it!=(a).rend(); it++)
#define mid l+r>>1
#define lid id<<1|1
#define rid (id<<1)+2
#define lson lid, l, mid
#define rson rid, mid, r

inline int in()
{
    char c=getchar();
    int neg=1, x=0;
    while(!isdigit(c)) (c=='-')?neg=-1, c=getchar():c=getchar();
    while(isdigit(c)) x=(x<<3)+(x<<1)+(c^48), c=getchar();
    return neg*x;
}
inline int lcm(int a, int b){return a*b/__gcd(a, b);}

const int MAX=5e5*4+5;

int N, M, ret, ptr, mark[MAX], v[MAX];
char S[MAX], s[MAX], t[MAX];
set<int> tmp;

struct Hash
{
        int val, mod, seed;
        Hash(int _m=1e9+7, int _s=61):val(0), mod(_m), seed(_s){}
        inline int update(int x)
        { 
            val=(1LL*val*seed+x)%mod; 
            return val;
        }
        inline int v(){ return val; }
}h;

struct Hash_suff// const int MAX=???
{
        Hash a;
        int ptr, mod, hash[MAX], base[MAX];
        Hash_suff() {}
        inline void init(int _m, int _p)
        {
            ptr=0;
            mod=a.mod=_m, a.seed=_p, a.val=0;
            base[0]=1;
            Forn(i, MAX-1)
                base[i]=1LL*base[i-1]*_p%_m;
        }
        inline void update(int x)
        { hash[++ptr]=a.update(x); }
        inline int get(int l, int r)// (l,r]
        { return (hash[r]-1LL*hash[l]*base[r-l]%mod+mod)%mod; }
        inline int getl(int l1, int r1, int l2, int r2)
        {
            int t1=get(l1, r1), t2=get(l2, r2);
            return (1LL*t1*base[r2-l2]+t2)%mod;
        }
}H;

int main()
{
    memset(mark, -1, sizeof(mark));
    H.init(1e9+7, 61);
    scanf("%s%s", S, t);
    M=strlen(t);
    int n=strlen(S);
    while(N<M) strcat(s, S), N+=n;
    strcat(s, S);
    forn(i, N+n) H.update(s[i]-'a'+1);
    forn(i, M) h.update(t[i]-'a'+1);
//    forn(i, N-M+1) cout<<H.get(i, i+M)<<" "<<h.v()<<endl;
    forn(i, N-M) if(mark[i]==-1)
    {
        int p=i, c=0;
        tmp.clear();
//        cout<<" "<<i<<endl;
        while(H.get(p, p+M)==h.v())
        {
//            cout<<p<<endl;
            if(tmp.count(p))
            {
                puts("-1");
                return 0;
            }
            if(~mark[p])
            {
                c=mark[p];
                break;
            }
            v[ptr++]=p, tmp.insert(p);
            p=(p+M)%N;
        }
        if(!ptr) mark[i]=0;
        else
        {
            c++;
            while(ptr>0)
                mark[v[ptr-1]]=c,
                c++, ptr--;
            ret=max(ret, c-1);
        }
    }
    printf("%d\n", ret); 
    return 0;
}
// baaab
//abaaba