String Equivalence: Counting Substrings Background

Two strings s and t are called k-Abelian equivalent (shortened to k-equivalent in the following) for a positive integer k if the following conditions hold:


The length-k-1 prefixes of s and t are equal.
The length-k-1 suffixes of s and t are equal.
The strings s and t have the same multisets of length-k contiguous substrings.
In other words, every string of length k occurs an equal number of times in s and t.
Overlapping occurrences are counted as distinct.


Note that k-equivalent strings are always k-1-equivalent if k &gt; 1, and the third condition implies that s and t have the same length.
It is also known, and possibly useful, that the three conditions above are equivalent to the following one:


Every non-empty string of length k or k-1 occurs an equal number of times in s and t.


An Example

For example, consider the strings s = "abbaaabb" and t = "aabbaabb".
Because the strings have the same number of each character, 4 of a and 4 of b, they are 1-equivalent.
Consider then 2-equivalence.
Their first and last characters are the same (both strings begin with a and end with b), so the first two conditions are satisfied.
The length-2 substrings of s are aa (occurs twice), ab (twice), ba (once), and bb (twice), and those of t are exactly the same.
This means that the strings are 2-equivalent.
However, since their second letters are different, the strings are not 3-equivalent.

Input

Two alphanumeric strings s and t of the same length n &gt; 0.

Output

The largest integer k between 1 and n (inclusive) such that s and t are k-equivalent.
If no such k exists, the output is 0.
In the above example, the correct output would be 2.

Rules

You can write a full program or a function.
The lowest byte count wins, and standard loopholes are disallowed.
Crashing on malformed input is perfectly fine.

Test Cases

"abc" "def" -&gt; 0
"abbaabbb" "aabbbaab" -&gt; 0
"abbaaabb" "aabbbaab" -&gt; 2
"abbaaabb" "aabbaabb" -&gt; 2
"aaabbaabb" "aabbaaabb" -&gt; 3
"aaabbaabb" "aaabbaabb" -&gt; 9
"abbabaabbaababbabaababbaabbabaab" "abbabaabbaababbaabbabaababbabaab" -&gt; 9
"yzxyxx" "yxyzxx" -&gt; 2
"xxxxxyxxxx" "xxxxyxxxxx" -&gt; 5
CJam, 51 49 47 41 34 bytes
WqN/:Q0=,,{Qf{_,,\f&gt;\)f&lt;$}~=},+W=)

Just to get something started. This can surely be golfed a lot. Looking at a different algorithm too.
Input goes as the two strings without quotes on separate lines.
Example:
aaabbaabb
aabbaaabb

Output:
3

Try it online here