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: Runtime Error)

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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pullull;
typedef pair<ll, int> plli;
typedef pair<int, pii> pipii;
typedef vector<vector<int> > mati;
typedef vector<vector<double> > matd;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef vector<vector<vector<ll>>> vvvll;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;
typedef vector<vector<vector<bool>>> vvvb;
typedef vector<pll> vpll;

#define FOR(i,x,y) for(ll i=(ll)x; i<(ll)y; ++i)
#define REP(i,y) FOR(i, 0, y)
#define RFOR(i,x,y) for(ll i=(ll)x; i>=(ll)y; --i)
#define RREP(i,x) RFOR(i, x, 0)
#define ALL(a) a.begin(), a.end()
#define pb push_back

inline void IN(void){
  return;
}

template <typename First, typename... Rest>
void IN(First& first, Rest&... rest){
  cin >> first;
  IN(rest...);
  return;
}

inline void OUT(void){
  cout << "\n";
  return;
}

template <typename First, typename... Rest>
void OUT(First first, Rest... rest){
  cout << first << " ";
  OUT(rest...);
  return;
}

constexpr int INF = (1<<30);
constexpr ll INFLL = 1LL<<62;
constexpr long double EPS = 1e-12;
constexpr ll MOD = (ll)((1E+9)+7);

/*
class KMP{
  private:
    string S;
    string W;
    vector<ll> T;
    vector<bool> flag;

  public:
    void make_table(void){
      T.resize(W.size());
      ll i = 2;
      ll j = 0;
      T[0] = -1;
      T[1] = 0;

      while(i<W.size()){
        if(W[i-1]==W[j]){
          T[i] = j+1;
          i++;
          j++;
        }else if(j>0){
          j = T[j];
        }else{
          T[i] = 0;
          i++;
        }
      }
      return;
    }

    KMP(string s, string w){
      S = s;
      W = w;
      make_table();
      flag.resize(S.size(), false);
      return;
    };

    ~KMP(void){
      return;
    };

    ll local_search(ll start){
      ll m = 0; // the start position in S
      ll i = start; // the start position in W

      while(m+i<S.size()){
        if(W[i] == S[m+i]){
          i++;
          if(i==W.size()) return m;
        }else{
          m+=i-T[i];
          if(i>0) i=T[i];
        }
      }
      return -1;
    };

    void total_search(void){
      ll m = 0; // the start position in S
      ll i = 0; // the start position in W

      while(m+i<S.size()){
        if(W[i] == S[m+i]){
          i++;
          if(i==W.size()){
            flag[m] = true;
            m++;
            i=0;
          }
        }else{
          m+=i-T[i];
          if(i>0) i=T[i];
        }
      }
      return;
    };

    bool start_check(ll start){
      return flag[start];
    };
};
*/

class Z_algorithm{
  public:
    string S;
    vector<ll> A;
    Z_algorithm(string s){
      S = s;
      A.resize(S.size());
      return;
    };

    ~Z_algorithm(void){
      return;
    };

    void calc(void){
      A[0] = S.size();
      ll i=1, j=0;
      while(i<S.size()){
        while(i+j < S.size() && S[j] == S[i+j]) ++j;
        A[i] = j;
        if(j==0){
          ++i;
          continue;
        }
        ll k=1;
        while(i+k<S.size() && k+A[k]<j){
          A[i+k] = A[k];
          ++k;
        }
        i += k;
        j -= k;
      }
      return;
    };

    void calc(string W){
      S = W+S;
      calc();
      return;
    };
};

vll dp;
vll jump;
bool total_flag = true;

ll calc(ll pos, const ll init){
  ll n_pos = jump[pos];
  if(n_pos<0){
    return dp[pos] = 0;
  }
  if(n_pos==init){
    total_flag = false;
    return -2;
  }
  if(dp[pos]>=0) return dp[pos];
  return dp[pos] = calc(n_pos, init) + 1;
}

int main(){
  string s_base, t_base;
  IN(s_base);
  IN(t_base);

  string s = s_base;
  string t = t_base;

  while(s.size()<2*t.size()){
    s += s_base;
  }

  jump.resize(s_base.size());
  REP(i,s_base.size()) jump[i] = -1;

  /*
  REP(s_start, s_base.size()){
    bool flag = true;
    REP(t_diff,t.size()){
      if(s[t_diff+s_start] != t[t_diff]){
        flag = false;
        break;
      }
    }
    if(!flag) continue;

    jump[s_start] = (s_start + t.size())%s_base.size();
  }
  */

  /*
  KMP kmp(s, t);
  kmp.total_search();
  REP(i,s_base.size()){
    if(!kmp.start_check(i)) jump[i] = -1;
    else jump[i] = (i+t.size())%s_base.size();
  }
  */

  Z_algorithm ZA(s);
  ZA.calc(t);
  REP(i,s_base.size()){
    if(ZA.A[t.size()+i] < t.size()) continue;
    jump[i] = (i+t.size())%s_base.size();
  }

  REP(i,s_base.size()){
    //printf("%lld %lld\n", i, jump[i]);
  }

  dp.resize(jump.size());
  REP(i,dp.size()){
    dp[i] = -1;
  }

  REP(i,dp.size()){
    calc(i, i);
    if(!total_flag) break;
  }

  ll ans = 0;
  REP(i,dp.size()){
    ans = max(ans, dp[i]);
    //printf("%lld %lld\n", i, dp[i]);
  }

  if(total_flag){
    printf("%lld\n", ans);
  }else{
    printf("-1\n");
  }


  return 0;
}