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

using System;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using static System.Math;
using Debug = System.Diagnostics.Debug;
using MethodImplOptions = System.Runtime.CompilerServices.MethodImplOptions;
using MethodImplAttribute = System.Runtime.CompilerServices.MethodImplAttribute;

static class P
{
    static bool inf = false;
    static string s;
    static string t;

    //実はループするから目も入らない TLEしたら消す
    static int[] memo;
    static bool[] calling;
    //頭から何文字
    static ModInt[] sHash;
    //頭から何文字
    static ModInt[] tHash;
    static void Main()
    {
        powMemo[0] = 1;
        for (int i = 1; i < powMemo.Length; i++)
        {
            powMemo[i] = powMemo[i - 1] * 27;
        }
        s = Console.ReadLine();
        t = Console.ReadLine();
        memo = Enumerable.Repeat(-1, s.Length).ToArray();
        calling = new bool[s.Length];
        sHash = CreateHash(s);
        tHash = CreateHash(t);
        //s.length <= t.lengthの場合、i=2で成立する場合は全て成立 ほんとか いいえ シミュレーションができる うん できるね 
        //†rolling hash†
        var res = 0;
        for (int i = 0; i < s.Length; i++)
        {
            res = Max(res, Solve(i));
            if (inf)
            {
                Console.WriteLine(-1);
                return;
            }
        }
        Console.WriteLine(res);
    }

    static ModInt[] powMemo = new ModInt[500001];
    static ModInt Slice(this ModInt[] hash, int begin, int length)
    {
        return hash[begin + length] - hash[begin] * powMemo[length];
    }

    static ModInt[] CreateHash(string s)
    {
        var hash = new ModInt[s.Length + 1];
        for (int i = 0; i < s.Length; i++)
        {
            hash[i + 1] = hash[i] * 27 + (s[i] - 96);
        }
        return hash;
    }

    //sの何番目からtを取り始めたら何個行けるか
    static int Solve(int begin)
    {
        if (calling[begin])
        {
            inf = true;
            return 0;
        }
        calling[begin] = true;
        var tPlace = 0;
        var sPlace = begin;
        var remainLength = t.Length;
        while (remainLength != 0)
        {
            var nextLength = Min(remainLength, Min(t.Length - tPlace, s.Length - sPlace));
            if (sHash.Slice(sPlace, nextLength) != tHash.Slice(tPlace, nextLength)) { calling[begin] = false; return memo[begin] = 0; }
            remainLength -= nextLength;
            sPlace += nextLength;
            tPlace += nextLength;
            if (sPlace == s.Length) sPlace = 0;
            if (tPlace == s.Length) tPlace = 0;
        }
        var res = memo[tPlace] != -1 ? memo[tPlace] : Solve(tPlace) + 1;
        calling[begin] = false;
        return memo[begin] = res;
    }
}


struct ModInt
{
    const int MOD = 745731223;
    long Data;
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public ModInt(long data) { if ((Data = data % MOD) < 0) Data += MOD; }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static implicit operator long(ModInt modInt) => modInt.Data;
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static implicit operator ModInt(long val) => new ModInt(val);
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static ModInt operator +(ModInt a, int b) => a.Data + b;
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static ModInt operator +(ModInt a, ModInt b) { long res = a.Data + b.Data; return new ModInt() { Data = res >= MOD ? res - MOD : res }; }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static ModInt operator +(ModInt a, long b) => a.Data + b;
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static ModInt operator -(ModInt a, ModInt b) { long res = a.Data - b.Data; return new ModInt() { Data = res < 0 ? res + MOD : res }; }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static ModInt operator -(ModInt a, long b) => a.Data - b;
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static ModInt operator *(ModInt a, int b) => new ModInt() { Data = a.Data * b % MOD };
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static ModInt operator *(ModInt a, long b) => a * new ModInt(b);
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static ModInt operator *(ModInt a, ModInt b) => new ModInt() { Data = a.Data * b.Data % MOD };
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static ModInt operator /(ModInt a, ModInt b) => new ModInt() { Data = a.Data * GetInverse(b) % MOD };
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public override string ToString() => Data.ToString();
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    static long GetInverse(long a)
    {
        long div, p = MOD, x1 = 1, y1 = 0, x2 = 0, y2 = 1;
        while (true)
        {
            if (p == 1) return x2 + MOD;
            div = a / p; x1 -= x2 * div; y1 -= y2 * div; a %= p;
            if (a == 1) return x1 + MOD;
            div = p / a; x2 -= x1 * div; y2 -= y1 * div; p %= a;
        }
    }
}