Problem:
Score : 1500 points

Problem Statement

#nck {
width: 30px;
height: auto;
}

Shik's job is very boring. At day 0, his boss gives him a string S_0 of length N which consists of only lowercase English letters. In the i-th day after day 0, Shik's job is to copy the string S_{i-1} to a string S_i. We denote the j-th letter of S_i as S_i[j].

Shik is inexperienced in this job. In each day, when he is copying letters one by one from the first letter to the last letter, he would make mistakes. That is, he sometimes accidentally writes down the same letter that he wrote previously instead of the correct one. More specifically, S_i[j] is equal to either S_{i-1}[j] or S_{i}[j-1]. (Note that S_i[1] always equals to S_{i-1}[1].)

You are given the string S_0 and another string T.
Please determine the smallest integer i such that S_i could be equal to T. If no such i exists, please print -1.

Constraints

1 \leq N \leq 1,000,000

The lengths of S_0 and T are both N.

Both S_0 and T consist of lowercase English letters.

Input

The input is given from Standard Input in the following format:

N
S_0
T

Output

Print the smallest integer i such that S_i could be equal to T. If no such i exists, print -1 instead.

Sample Input 1

5
abcde
aaacc

Sample Output 1

2

S_0 = abcde, S_1 = aaccc and S_2 = aaacc is a possible sequence such that S_2 = T.

Sample Input 2

5
abcde
abcde

Sample Output 2

0

Sample Input 3

4
acaa
aaca

Sample Output 3

2

Sample Input 4

5
abcde
bbbbb

Sample Output 4

-1

Solution in C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Text;
using System.Collections.Generic;
class Solve{
    public Solve(){}
    StringBuilder sb;
    public static int Main(){
        new Solve().Run();
        return 0;
    }
    void Run(){
        sb = new StringBuilder();
        Calc();
        Console.Write(sb.ToString());
    }
    void Calc(){
        int N = int.Parse(Console.ReadLine());
        string s1 = Console.ReadLine();
        string s2 = Console.ReadLine();
        if(s1 == s2){
            sb.Append("0\n");
        }
        else{
            List<int> stacki = new List<int>();
            List<int> stacko = new List<int>();
            {
                char c = s2[N-1];
                int p = N-1;
                bool b = true;
                for(int i=N-1;i>=-1;i--){
                    if(i == -1 || s2[i] != c){
                        stacko.Add(i+1);
                        p = Math.Min(p,i+1);
                        while(p >= 0 && s1[p] != c){
                            p--;
                        }
                        if(p == -1){
                            b = false;
                            break;
                        }
                        stacki.Add(p);
                        if(i != -1){
                            c = s2[i];
                        }
                    }
                }
                if(!b){
                    sb.Append("-1\n");
                    return;
                }
            }
            int max = 0;
            int d = 0;
            int spi = stacki.Count-1;
            int spo = stacko.Count-1;
            for(int i=0;i<N;i++){
                if(spi >= 0 && i == stacki[spi]){
                    spi--;
                    d++;
                }
                while(spo >= 0 && i == stacko[spo] + d - 1){
                    spo--;
                    d--;
                }
                max = Math.Max(max,d);
            }
            sb.Append(max+1+"\n");
        }
    }
}