#include <bits/stdc++.h>
#define N 1000050
using namespace std;

char a[N],b[N];
int flag,ans,n;
queue<int> q;

int main() {
    scanf("%d%s%s",&n,a+1,b+1);
    for (int _=1;_<=n;_++) if (a[_] != b[_]) flag = 1;
    if (!flag) return puts("0"), 0;

    int p = n;
    for (int _=n;_>=1;_--) {
        if (b[_-1] == b[_]) continue;
        p = min(p, _);
        while (p && a[p] != b[_]) p--;
        if (!p) return puts("-1"), 0;

        #define sz (int)q.size()
        while (!q.empty())
            if (q.front()-sz >= _) q.pop(); else break;
        q.push(p);
        _!=p ? ans = max(ans,sz) :0;
    }
    printf("%d\n",ans+1);
    return 0;
}