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# (.NET Core 3.1.201) (judge verdict: Accepted)

using System;
using System.Linq;
using System.Text;
using CompLib.Mathematics;
using CompLib.Util;

public class Program
{
    private string S;
    private string T;

    private const long P1 = 1000000007;
    private const long P2 = 998244353;

    public void Solve()
    {
        var sc = new Scanner();
        S = sc.Next();
        T = sc.Next();

        // ローリングハッシュ
        long hashT1 = 0;
        long hashT2 = 0;
        for (int i = 0; i < T.Length; i++)
        {
            hashT1 *= 26;
            hashT1 += T[i] - 'a';
            hashT1 %= P1;

            hashT2 *= 26;
            hashT2 += T[i] - 'a';
            hashT2 %= P2;
        }

        long[] hashS1 = new long[S.Length];
        long[] hashS2 = new long[S.Length];


        // 26^|T|
        long m1 = 1;
        long m2 = 1;

        for (int i = 0; i < T.Length; i++)
        {
            hashS1[0] *= 26;
            hashS1[0] += S[i % S.Length] - 'a';
            hashS1[0] %= P1;
            m1 *= 26;
            m1 %= P1;

            hashS2[0] *= 26;
            hashS2[0] += S[i % S.Length] - 'a';
            hashS2[0] %= P2;

            m2 *= 26;
            m2 %= P2;
        }


        for (int i = 0; i < S.Length - 1; i++)
        {
            hashS1[i + 1] = hashS1[i] * 26;
            hashS1[i + 1] += S[(i + T.Length) % S.Length] - 'a';
            hashS1[i + 1] -= (S[i] - 'a') * m1;
            hashS1[i + 1] %= P1;
            if (hashS1[i + 1] < 0) hashS1[i + 1] += P1;

            hashS2[i + 1] = hashS2[i] * 26;
            hashS2[i + 1] += S[(i + T.Length) % S.Length] - 'a';
            hashS2[i + 1] -= (S[i] - 'a') * m2;
            hashS2[i + 1] %= P2;
            if (hashS2[i + 1] < 0) hashS2[i + 1] += P2;
        }

        bool[] f = new bool[S.Length];
        for (int i = 0; i < S.Length; i++)
        {
            f[i] = hashS1[i] == hashT1 && hashS2[i] == hashT2;
        }

        // f_b, f_{b+|t|} ... f_{b+(i-1)|t|}がtrue

        // 一致する位置が無い
        if (!f.Any(b => b))
        {
            Console.WriteLine("0");
            return;
        }

        long gcd = MathEx.GCD(S.Length, T.Length);
        bool[] a = new bool[gcd];
        for (int i = 0; i < S.Length; i++)
        {
            if (!f[i]) a[i % gcd] = true;
        }

        if (a.Any(b => !b))
        {
            Console.WriteLine("-1");
            return;
        }
        
        long ans = long.MinValue;
        for (int b = 0; b < S.Length; b++)
        {
            // 前は一致しない
            int prev = (b - T.Length) % S.Length;
            if (prev < 0) prev += S.Length;
            if (f[prev]) continue;

            int cur = b;
            long cnt = 0;
            while (f[cur])
            {
                cnt++;
                cur += T.Length;
                cur %= S.Length;
            }

            ans = Math.Max(ans, cnt);
        }

        Console.WriteLine(ans);
    }

    public static void Main(string[] args) => new Program().Solve();
}

// https://bitbucket.org/camypaper/complib
namespace CompLib.Mathematics
{
    using System;
    using System.Collections.Generic;

    #region GCD LCM

    /// <summary>
    /// 様々な数学的関数の静的メソッドを提供します．
    /// </summary>
    public static partial class MathEx
    {
        /// <summary>
        /// 2 つの整数の最大公約数を求めます．
        /// </summary>
        /// <param name="n">最初の値</param>
        /// <param name="m">2 番目の値</param>
        /// <returns>2 つの整数の最大公約数</returns>
        /// <remarks>ユークリッドの互除法に基づき最悪計算量 O(log N) で実行されます．</remarks>
        public static int GCD(int n, int m)
        {
            return (int) GCD((long) n, m);
        }


        /// <summary>
        /// 2 つの整数の最大公約数を求めます．
        /// </summary>
        /// <param name="n">最初の値</param>
        /// <param name="m">2 番目の値</param>
        /// <returns>2 つの整数の最大公約数</returns>
        /// <remarks>ユークリッドの互除法に基づき最悪計算量 O(log N) で実行されます．</remarks>
        public static long GCD(long n, long m)
        {
            n = Math.Abs(n);
            m = Math.Abs(m);
            while (n != 0)
            {
                m %= n;
                if (m == 0) return n;
                n %= m;
            }

            return m;
        }


        /// <summary>
        /// 2 つの整数の最小公倍数を求めます．
        /// </summary>
        /// <param name="n">最初の値</param>
        /// <param name="m">2 番目の値</param>
        /// <returns>2 つの整数の最小公倍数</returns>
        /// <remarks>最悪計算量 O(log N) で実行されます．</remarks>
        public static long LCM(long n, long m)
        {
            return (n / GCD(n, m)) * m;
        }
    }

    #endregion

    #region PrimeSieve

    public static partial class MathEx
    {
        /// <summary>
        /// ある値までに素数表を構築します．
        /// </summary>
        /// <param name="max">最大の値</param>
        /// <param name="primes">素数のみを入れた数列が返される</param>
        /// <returns>0 から max までの素数表</returns>
        /// <remarks>エラトステネスの篩に基づき，最悪計算量 O(N loglog N) で実行されます．</remarks>
        public static bool[] Sieve(int max, List<int> primes = null)
        {
            var isPrime = new bool[max + 1];
            for (int i = 2; i < isPrime.Length; i++) isPrime[i] = true;
            for (int i = 2; i * i <= max; i++)
                if (!isPrime[i]) continue;
                else
                    for (int j = i * i; j <= max; j += i)
                        isPrime[j] = false;
            if (primes != null)
                for (int i = 0; i <= max; i++)
                    if (isPrime[i])
                        primes.Add(i);

            return isPrime;
        }
    }

    #endregion
}

namespace CompLib.Util
{
    using System;
    using System.Linq;

    class Scanner
    {
        private string[] _line;
        private int _index;
        private const char Separator = ' ';

        public Scanner()
        {
            _line = new string[0];
            _index = 0;
        }

        public string Next()
        {
            if (_index >= _line.Length)
            {
                string s;
                do
                {
                    s = Console.ReadLine();
                } while (s.Length == 0);

                _line = s.Split(Separator);
                _index = 0;
            }

            return _line[_index++];
        }

        public string ReadLine()
        {
            _index = _line.Length;
            return Console.ReadLine();
        }

        public int NextInt() => int.Parse(Next());
        public long NextLong() => long.Parse(Next());
        public double NextDouble() => double.Parse(Next());
        public decimal NextDecimal() => decimal.Parse(Next());
        public char NextChar() => Next()[0];
        public char[] NextCharArray() => Next().ToCharArray();

        public string[] Array()
        {
            string s = Console.ReadLine();
            _line = s.Length == 0 ? new string[0] : s.Split(Separator);
            _index = _line.Length;
            return _line;
        }

        public int[] IntArray() => Array().Select(int.Parse).ToArray();
        public long[] LongArray() => Array().Select(long.Parse).ToArray();
        public double[] DoubleArray() => Array().Select(double.Parse).ToArray();
        public decimal[] DecimalArray() => Array().Select(decimal.Parse).ToArray();
    }
}