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)

/*
Though leaves are many, the root is one;
Through all the lying days of my youth
I swayed my leaves and flowers in the sun,
Now may I wither into the truth.

- William Butler Yeats
*/
#include <iostream>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <cstdio>
#include <vector>
#include <set>
#include <cassert>
#include <cstdlib>
#include <complex>
#include <cctype>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <sstream>
#include <functional>
#include <iomanip>
#include <bitset>
//#include <windows.h>  //Should be deleted when using AtCoder&POJ
using namespace std;

#define ll long long
#define pii pair<int,int>
#define qi ios::sync_with_stdio(0)
/**==Info==
*Program:3
*Problem:Strings of Eternity
*Date:2019-7-28
*Algorithm:My KMP that is still WA
*Status:Unknown*/

bool debug=false;

string a,b,sa;


/*
KMP By XGN HHS 

Time Complexity: O(n+m)

sz=Text Length
 
Usage:

KMP kmp;
kmp.check(<text string>,<model string>);
*/
const int sz=3000005;

bool found[sz];

int f[sz];
struct KMP{
	/*A是文本串，B是模式串*/
	
	/*进行匹配*/
	inline void check(string& a,string& b){
		
		getFail(b); //初始化失配指针
		 
		int j=0; //模式串匹配位置
		
		for(int i=0;i<a.size();i++){
			while(j!=0 && a[i]!=b[j]){
				j=f[j]; //失配 
			}
			
			if(a[i]==b[j]){
				j++; //匹配成功！ 
			}
			
			if(j==b.size()){
				//匹配完成【根据需要修改】
				found[i-int(b.size())+1]=true; 
//				cout<<"Found at:"<<i-int(b.size())+1<<endl; 
			}
		} 
	}
	
	/*初始化失配指针*/
	inline void getFail(string& b){
		f[0]=f[1]=0;
		for(int i=1;i<b.size();i++){
			int j=f[i]; //取出上一次的失配指针
			while(j!=0 && b[i]!=b[j]){
				j=f[j]; //失配 
			} 
			
			f[i+1]=(b[i]==b[j]?j+1:0);
		}
	}
	
	/*[DEBUG]打印失配指针*/
	void __printFail(int n){
		for(int i=0;i<=n;i++){
			cout<<f[i]<<" ";
		}
		cout<<endl;
	}
};

KMP kmp;

int visit[sz]; //0=unvisit 1=visiting 2=visited

int dp[sz];

inline int dfs(int pos){
	if(found[pos]==0){
		return 0;
	}
	if(visit[pos]==1){
		cout<<"-1";
		exit(0);
	}
	if(dp[pos]!=-1){
		return dp[pos];
	}
	
	visit[pos]=1;
	
	int ans=dfs((pos+b.size())%sa.size());
	
	visit[pos]=2;
	return dp[pos]=ans+1;
}

char tmp1[500005],tmp2[500005];

int main(int argc,char* argv[]){
	
	scanf("%s%s",tmp1,tmp2);
	
	sa=tmp1;
	b=tmp2;
	
	a="";
	while(a.size()<b.size()+sa.size()){
		a=a+sa;
	}
	
	kmp.getFail(b);
	
	kmp.check(a,b);
	
	return 0;
	memset(dp,-1,sizeof(dp));
	
	int ans=0;
	for(int i=0;i<sa.size();i++){
		if(!visit[i]){
			ans=max(ans,dfs(i));
		}
		
	}
	
	printf("%d",ans);
	return 0;
}