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)

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

template<typename T> T rng(T left, T right) {
  static mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());
  return uniform_int_distribution<T>(left, right - 1)(mt);
}

class RollingHash {
private:
  static constexpr int mod_1 = 1000000007;
  static constexpr int mod_2 = 1000000009;
  static long long base_1;
  static long long base_2;
  static vector<long long> pow_1;
  static vector<long long> pow_2;
  vector<long long> hash_1;
  vector<long long> hash_2;
  const int siz;
  template<typename Itr> void build(Itr first, Itr last) {
    hash_1.resize(siz + 1);
    hash_2.resize(siz + 1);
    for (int i = 0; i < siz; ++i, ++first) {
      hash_1[i + 1] = (hash_1[i] * base_1 % mod_1 + *first) % mod_1;
      hash_2[i + 1] = (hash_2[i] * base_2 % mod_2 + *first) % mod_2;
    }
    while ((int) pow_1.size() <= siz) {
      pow_1.push_back(pow_1.back() * base_1 % mod_1);
      pow_2.push_back(pow_2.back() * base_2 % mod_2);
    }
  }
  long long get_1(int left, int right) { return (hash_1[right] + (mod_1 - hash_1[left]) * pow_1[right - left] % mod_1) % mod_1; }
  long long get_2(int left, int right) { return (hash_2[right] + (mod_2 - hash_2[left]) * pow_2[right - left] % mod_2) % mod_2; }
public:
  template<typename Itr> RollingHash(Itr first, Itr last) : siz(distance(first, last)) { build(first, last); }
  template<typename T> RollingHash(const T in) : siz(distance(in.begin(), in.end())) { build(in.begin(), in.end()); }

  pair<long long, long long> get(int left, int right) { return make_pair(get_1(left, right), get_2(left, right)); }
  bool is_same(int left_1, int right_1, int left_2, int right_2) { return get(left_1, right_1) == get(left_1, right_2); }
  bool is_same(int left_1, int left_2, int len) { return is_same(left_1, left_1 + len, left_2, left_2 + len); }
  bool is_same(int left, int right, pair<long long, long long> hash) { return get(left, right) == hash; }

  pair<long long, long long> const show_base() { return make_pair(base_1, base_2); }
  void show_hashes() {
    for (int i = 0; i < (int) hash_1.size(); i++) {
      cerr << "i = " << i << " " << hash_1[i] << " " << hash_2[i] << endl;
    }
  }
  void show_pows() {
    for (int i = 0; i < (int) pow_1.size(); i++) {
      cerr << "i = " << i << " " << pow_1[i] << " " << pow_2[i] << endl;
    }
  }
};

long long RollingHash::base_1 = rng<unsigned>(1, RollingHash::mod_1);
long long RollingHash::base_2 = rng<unsigned>(1, RollingHash::mod_2);
vector<long long> RollingHash::pow_1{1};
vector<long long> RollingHash::pow_2{1};
using RH = RollingHash;


class UnionFind {
private:
  int siz;
  vector<int> a;

public:
  UnionFind(int x) : siz(x), a(x, -1) {}

  int root(int x) {
    return a[x] < 0 ? x : a[x] = root(a[x]);
  }

  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y) return false;
    siz--;
    if (a[x] > a[y]) swap(x, y);
    a[x] += a[y];
    a[y] =x;
    return true;
  }

  bool same(int x, int y) {
    return root(x) == root(y);
  }

  int size(int x) {
    return -a[root(x)];
  }
  
  int conneted_component() {
  	return siz;
  }
};

signed main() { 
  ios::sync_with_stdio(false); cin.tie(0);
  string s, t;
  cin >> s >> t;
  int n = (int) s.size(), m = (int) t.size();
  s.append(s);
  s.append(s);
  while ((int) t.size() * 2 > (int) s.size()) s.append(s);
  RH rh_1(s), rh_2(t);
  UnionFind uf(n);
  for (int i = 0; i < n; i++) {
    if (rh_1.is_same(i, i + m, rh_2.get(0, m)) && rh_1.is_same(i + m, i + m + m, rh_2.get(0, m))) {
      if (!uf.unite(i, (i + m) % n)) {
        cout << -1 << endl;
        return 0;
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (rh_1.is_same(i, i + m, rh_2.get(0, m))) ans = max(ans, uf.size(i));
  }
  cout << ans << endl;
  return 0;
}