This page is still under construction.

Parts of this page are still being built. What you see may change.

Escape Sequences

Time limit1sMemory limit512 MB

Summary
For a morphism f that replaces a with aa and b with ab, find the smallest k such that t is a substring of the k-fold iterate f^k(s).
Level

Hard8 of 10

Topics
String, Divide and conquer, Math, Binary search
Solved
No attempts yet

Problem

For a string ss consisting of only 'a' and 'b', let f(s)f(s) be the string obtained by replacing every 'a' in ss with 'aa' and every 'b' with 'ab'. For example, f(f("aba")=) = "aaabaa"$.

Given strings ss and tt, find the smallest non-negative integer kk such that tt is a contiguous substring of fk(s)f^k(s).

fkf^k is defined as follows.

  • f0(s)=sf^0(s) = s
  • fk(s)=fk−1(f(s))f^k(s) = f^{k - 1}(f(s))

Input

The first line and the second line contain string ss and string tt respectively (1≤∣s∣,∣t∣≤2⋅1051 \leq |s|, |t| \leq 2 \cdot 10^5).

Strings ss and tt consist of only the characters 'a' and 'b'.

Output

Print a single integer, the minimum kk.

If kk does not exist, print "-1" instead.

Examples3

  1. Example 1

    Input
    b
    ab
    
    Expected output
    1
    
  2. Example 2

    Input
    ababa
    bab
    
    Expected output
    0
    
  3. Example 3

    Input
    a
    b
    
    Expected output
    -1