Cousin Strings

Time limit1sMemory limit128 MB

Summary
Find the smallest n such that x is an n-th cousin of y, where each cousin step requires a common string reachable by deleting at most half of each string, or report that no n exists.
Level

Medium7 of 10

Topics
Graph, BFS, String, Dynamic programming
Solved
No attempts yet

Problem

Two strings aa and bb are called first cousins if they can be made equal by deleting at most half of the characters from each string. For example, abcdef and axcyd are first cousins: deleting 33 of the 66 characters (b, e, f) from the first string and 22 of the 55 characters (x, y) from the second string leaves acd in both cases.

Two strings cc and dd are called (n+1)(n{+}1)-th cousins if there exists a string ee that is a first cousin of cc and an nn-th cousin of dd.

Given two strings xx and yy, find the smallest n≥1n \ge 1 such that xx is an nn-th cousin of yy.

Input

The input contains several test cases. Each test case consists of two lines: the string xx on the first line and the string yy on the second line. Each of xx and yy contains between 11 and 100100 lowercase letters. The input ends with a test case whose two lines each contain a single 0; this terminating case must not be processed.

Output

For each test case, print a single line containing the smallest nn for which xx is an nn-th cousin of yy, or not related if no such nn exists.

Examples3

  1. Example 1

    Input
    a
    b
    abb
    baa
    abcdef
    axcyd
    0
    0
    
    Expected output
    2
    2
    1
    
  2. Example 2

    Input
    abc
    abc
    z
    z
    0
    0
    
    Expected output
    1
    1
    
  3. Example 3

    Input
    x
    y
    0
    0
    
    Expected output
    2