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>
using namespace std;
#pragma GCC optimize("Ofast")
typedef long long lint;
typedef unsigned int uint;
typedef pair<int, int> pii;
typedef pair<lint, lint> pll;
typedef unsigned long long ulint;
#define endl '\n'
#define fst first
#define sed second
#define pb push_back
#define mp make_pair
#define rint register int
#define newline putchar('\n')
#define SZ(x) (int((x).size()))
#define leave_space putchar(' ')
#define all(x) (x).begin(), (x).end()
#define reveal(x) cerr << #x << " = " << (x) << endl
#define rep(it, f, e) for (rint it = (f); it <= (e); ++it)
#define per(it, f, e) for (rint it = (f); it >= (e); --it)
#define repe(it, x) for (auto it = (x).begin(); it != (x).end(); ++it)
const int MAXN = 2e6 + 20;
const int MOD = 1e9 + 7;
const int MUL = 32323;
string s, t, T;
vector<int> edge[MAXN >> 2];
int dp[MAXN >> 2], OK[MAXN >> 2], que[MAXN >> 2], deg[MAXN >> 2];
lint Hash[2][MAXN], mul[MAXN];
inline void DoHash(lint HHH[], string & SSS) {
	#define s SSS
	#define Hash HHH
	Hash[0] = s[0] - 'a' + 1;
	rep (i, 1, SZ(s) - 1) {
		if (i) Hash[i] = (Hash[i - 1] * MUL + s[i] - 'a' + 1) % MOD;
	}
	#undef s
	#undef Hash
}
inline ulint getHash(lint HHH[], int l, int r) {
	if (!l) return HHH[r];
	return (HHH[r] - HHH[l - 1] * mul[r - l + 1] % MOD + MOD) % MOD;
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL), cout.tie(NULL);
	cin >> T >> s;
	while (SZ(t) < SZ(s)) t += T; t += t;
	mul[0] = 1;
	rep (i, 1, MAXN - 1) {
		mul[i] = mul[i - 1] * MUL % MOD;
	}
	DoHash(Hash[0], t);
	DoHash(Hash[1], s);
	rep (i, 0, SZ(t) - SZ(s)) {
		OK[i % SZ(T)] |= (getHash(Hash[0], i, i + SZ(s) - 1) == getHash(Hash[1], 0, SZ(s) - 1));
	}
	rep (i, 0, SZ(T) - 1) {
		if (OK[i] && OK[(i + SZ(s)) % SZ(T)]) {
			edge[i].pb((i + SZ(s)) % SZ(T)), ++deg[(i + SZ(s)) % SZ(T)];
		}
	}
	int l = 1, r = 0;
	rep (i, 0, SZ(T) - 1) {
		if (OK[i] && deg[i] == 0) {
			que[++r] = i;
		}
	}
	while (l <= r) {
		int u = que[l++];
		for (auto v : edge[u]) {
			if ((--deg[v]) == 0) {
				que[++r] = v;
			}
		}
	}
	rep (i, 0, SZ(T) - 1) if (deg[i]) {
		cout << -1 << endl; return 0;
	}
	int ans = 0;
	per (i, r, 1) {
		int now = que[i];
		if (edge[now].size() == 0) dp[now] = 1;
		else dp[now] = dp[(now + SZ(s)) % SZ(T)] + 1;
		ans = max(ans, dp[now]);
	}
	cout << ans << endl;
	return 0;
}