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 Java7 (OpenJDK 1.7.0) (judge verdict: Time Limit Exceeded)

import java.util.*;
class Main{
	public static String s , t;
	public static int n , m;
	public static Scanner input = new Scanner(System.in);
	
	public static void main(String[] args){
		s = input.nextLine();
		t = input.nextLine();
		n = s.length() ;
		m = t.length();
		int eff = (2*n+m)/n;
		eff *= n;

		graph G = new graph(n);
		
		kmp.computeLPSArray(m , t);
		kmp.kmp_search(eff , n , m , s , t , G);
		
		System.out.println(G.longest_path(n));
	}
}
class kmp{
	public static int lps[];
	static void computeLPSArray(int m , String t){
		lps = new int[m];
		int length = 0;
		int i = 1;
		while(i < m){
			if(t.charAt(i) == t.charAt(length)){
				length++;
				lps[i] = length;
				i++;
			}
			else{
				if(length != 0)
					length = lps[i-1];
				else
					i++;
			}
		}
	}
	static void kmp_search(int eff , int n , int m , String s , String t , graph G){
		int i = 0 , j = 0;
		while(i < eff){
			if(s.charAt(i%n) == t.charAt(j)){
				i++;
				j++;
			}
			if(j == m){
				G.add_edge((i-m)%n , i%n);
				j = lps[j-1];
			}
			else if(i < eff && s.charAt(i%n) != t.charAt(j)){
				if(j != 0)
					j = lps[j-1];
				else
					i++;
			}
			
		}
	}
}
class graph{
	int v;
	LinkedList<Integer> G[];
	int indeg[];
	boolean visited[];
	int max_dis[];
	graph(int V){
		v = V;
		indeg = new int[V];
		visited = new boolean[V];
		max_dis = new int[V];
		G = new LinkedList[V];
		for(int i = 0 ;i < V ; i++)
			G[i] = new LinkedList<>();
	} 
	void add_edge(int a, int b){
		G[a].add(b);
		indeg[b]++;
	}
	
	int longest_path(int n){
		Queue<Integer> q = new LinkedList<>();
		int mx = 0 , cnt = 0;
		for(int i = 0 ;i < n; i++){
			if(indeg[i] == 0)
				q.add(i);
		}
		while(q.size() > 0){
			int u = q.peek();
			
			mx = Math.max(mx , max_dis[u]);
			q.remove();
			cnt++;
			visited[u] = true;
			for(int v : G[u]){
				if(!visited[v]){
					indeg[v]--;
					max_dis[v] = Math.max(max_dis[v] , max_dis[u] + 1);
					if(indeg[v] == 0)
						q.add(v);
				}
				else
					return -1;
			}
		}
		if(cnt < n)
			return -1;
		return mx;
	}	
}