#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MAXN = 1e6 + 10, MAX = 30;
int n;
string s, t;
vector <int> indx[MAX];
int nxt[MAXN];
bitset <MAXN> b;

int32_t main () {
    ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    
    cin >> n >> s >> t;
    
    for (int i = 0; i < n; ++i)
        indx[s[i] - 'a'].push_back(i);
    
    int point = n - 1;
    for (int i = n - 1; i >= 0; --i) {
        while (!indx[t[i] - 'a'].empty() && (indx[t[i] - 'a'].back() > point || indx[t[i] - 'a'].back() > i))
            indx[t[i] - 'a'].pop_back();
        if (indx[t[i] - 'a'].empty())
            return cout << -1, 0;
        int k = indx[t[i] - 'a'].back();
        point = k;
        if (!b[k])
            nxt[k] = i; 
        b[k] = true;
    }

    vector <pair <int, int>> vctr;
    for (int i = 0; i < n; ++i)
        if (b[i] && i != nxt[i])
            vctr.push_back({i, nxt[i]});
    // for (auto i : vctr)
    //     cout << i.first << ';' << i.second << ' ';
    int res = 0;
    int sz = vctr.size();
    point = sz - 1;
    for (int i = sz - 1; i >= 0; --i) {
        while (point > i && vctr[point].first - point + i >= vctr[i].second)
            --point;
        res = max(res, point - i + 1);
    }
    cout << res;
    return 0;
}
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;

int n,ans;
char a[1000005],t[1000005];
int pre;

queue<int>Q;

int same(){
	for(int i=1;i<=n;i++){
		if(a[i]!=t[i])return 0;
	}return 1;
}

int main(){
	scanf("%d",&n);
	scanf("%s",a+1);
	for(int i=1;i<=n;i++)a[i]-='a'-1;
	scanf("%s",t+1);
	for(int i=1;i<=n;i++)t[i]-='a'-1;
	if(same()){
		printf("0\n");
		return 0;
	}int i=n,pre=0;
	for(int j=n;j;j--){
		i=min(i,j);
		while(i&&a[i]!=t[j])i--;
		if(i==0){
			printf("-1");
			return 0;
		}if(i==pre)continue;
		pre=i;
		while((!Q.empty())&&j<=Q.front()-Q.size())Q.pop();
		Q.push(pre);
		ans=max(ans,(int)Q.size());
	}printf("%d\n",ans);
	return 0;
}