#include <algorithm>
#include <cstring>
#include <cctype>
#include <cstdio>
using namespace std;
inline int read(int f = 1, int x = 0, char ch = ' ')
{
    while(!isdigit(ch = getchar())) if(ch == '-') f = -1;
    while(isdigit(ch)) x = x*10+ch-'0', ch = getchar();
    return f*x;
}
const int N = 1e6+5;
int n, ans, d = 1, q[N], l, r;
char s[N], t[N];
int main()
{  
    n = read(); scanf("%s%s", s+1, t+1);
    for(int i = n, p = n, l = 1, r = 0; i; --i)
    {
        d &= (s[i] == t[i]);
        if(t[i] == t[i-1]) continue;
        p = min(p, i);
        while(p&&s[p] != t[i]) --p;
        if(!p) return puts("-1"), 0;
        while(l <= r&&q[l]-(r-l+1)+1 > i) ++l; 
        q[++r] = p, ans = max(ans, (i != p)*(r-l+1));
    }
    printf("%d\n", ans+(d == 0));
    return 0;
}
/* 
4
bcad
*/