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# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class TEST{
	static void Main(){
		Sol mySol =new Sol();
		mySol.Solve();
	}
}

class Sol{
	public void Solve(){
		
		int NN = (int)1e6;
		int MM = (int)1e6;
		var ca = new char[NN + MM];
		for(int i=0;i<NN;i++) ca[i] = S[i % S.Length];
		for(int i=0;i<MM;i++) ca[NN+i] = T[i % T.Length];
		
		N = NN;
		M = T.Length;
		var U = new String(ca);
//Console.WriteLine(U);
		var RH1 = new RollingHash(U,2525,(long)1e9 + 7);
		var RH2 = new RollingHash(U,5252,(long)1e9 + 9);
		
		if(!isOK(1, RH1, RH2)){
			Console.WriteLine(0);
			return;
		}
		if(isOK(N / M, RH1, RH2)){
			Console.WriteLine(-1);
			return;
		}
		
		int l = 1; int r = N / 3 * 2;
		while(r - l > 1){
			int c = (r + l) / 2;
			if(isOK(c, RH1, RH2)){
				l = c;
			} else {
				r = c;
			}
		}
		
		Console.WriteLine(l);
		
		
	}
	int N;
	int M;
	bool isOK(int cnt, RollingHash RH1, RollingHash RH2){
		if((long) M * (long) cnt > (long) N) return false;
		int len = M * cnt;
//Console.WriteLine("{0} {1}",N,len);
		var h1 = RH1.GetHash(N, len);
		var h2 = RH2.GetHash(N, len);
		
		for(int i=0;i+len<=N;i++){
			var hash1 = RH1.GetHash(i, len);
			var hash2 = RH2.GetHash(i, len);
			if((h1 == hash1) && (h2 == hash2)) return true;
		}
		return false;
	}
	
	String S;
	String T;
	public Sol(){
		S = rs();
		T = rs();
	}

	static String rs(){return Console.ReadLine();}
	static int ri(){return int.Parse(Console.ReadLine());}
	static long rl(){return long.Parse(Console.ReadLine());}
	static double rd(){return double.Parse(Console.ReadLine());}
	static String[] rsa(char sep=' '){return Console.ReadLine().Split(sep);}
	static int[] ria(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>int.Parse(e));}
	static long[] rla(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>long.Parse(e));}
	static double[] rda(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>double.Parse(e));}
}


// 2つ以上のRHで確認する時はGetHashの積かなにかをつかう（xor，pairはダメ!!!）
class RollingHash{
	
	long[] Hash;
	long[] Base;
	long[] BaseInv;
	String S;
	long mod;
	int N;
	
	public RollingHash(String s_,long base_=2525,long mod_=(long)1e9+7){
		mod=mod_;
		S=s_;
		N=S.Length;
		Hash=new long[N+1];
		
		Base=new long[N+1];
		Base[0]=1;
		Base[1]=base_;
		
		BaseInv=new long[N+1];
		BaseInv[0]=1;
		BaseInv[1]=ModInv(Base[1]);
	
		for(int i=1;i<N;i++){
			Base[i+1]=Base[i]*Base[1];
			if(Base[i+1]>=mod)Base[i+1]%=mod;
			BaseInv[i+1]=BaseInv[i]*BaseInv[1];
			if(BaseInv[i+1]>=mod)BaseInv[i+1]%=mod;
		}
		
		Hash[0]=0;
		Hash[1]=(long)S[0];
		for(int i=1;i<N;i++){
			Hash[i+1]=Hash[i]*Base[1]+(long)S[i];
			if(Hash[i+1]>=mod)Hash[i+1]%=mod;
		}
//for(int i=0;i<=N;i++)Console.WriteLine("{0}:{1}",i,Hash[i]);
	}
	
	public long GetHash(int strt,int len){
		//return hash of S.Substring(strt,len);
		if(len==0)return 0;
		if(strt+len>=N)len=N-strt;
		long ret=Hash[strt+len]+mod-(Hash[strt]*Base[len]>=mod?Hash[strt]*Base[len]%mod:Hash[strt]*Base[len]);
		return ret>=mod?ret%mod:ret;
	}
	
	
	
	
	public long ModInv(long x){
		//Mod-Inverse of x by Ext-Euclid
		// solve (x,mod)-->(a,b,c) s.t. a * x + b * mod = c 
		//  if(x==0 || c!=1) return 0;
		//  return a (since a * x == 1 modulo mod)
		long a=0,b=0,c=0;
		if(x==0)return 0;
		ExtGCD(x,mod,ref a,ref b,ref c);
		if(c!=1)return 0;
		return (a+mod)%mod;
	}
	
	public static void ExtGCD(long x,long y,ref long a,ref long b,ref long c){
		long r0=x;long r1=y;
		long a0=1;long a1=0;
		long b0=0;long b1=1;
		long q1,r2,a2,b2;
		while(r1>0){
			q1=r0/r1;
			r2=r0%r1;
			a2=a0-q1*a1;
			b2=b0-q1*b1;
			r0=r1;r1=r2;
			a0=a1;a1=a2;
			b0=b1;b1=b2;
		}
		c=r0;
		a=a0;
		b=b0;
	}




}