#include<bits/stdc++.h>
using namespace std;
const int maxn=1000005;
char a[maxn],b[maxn];
int q[maxn],head,tail;
int main()
{
    int n;
    scanf("%d",&n);
    scanf("%s%s",a+1,b+1);
    if(strcmp(a+1,b+1)==0)
    {
        puts("0");
        return 0;
    }
    int ans=0;
    head=tail=maxn-2;
    //q[tail++]=n+1;
    int last=n;
    int tag=0;
    for(int l=n,r;l;l--)
    {
        r=l;
        while(b[l]==b[l-1]) l--;
        while(last){
            if(last>l||a[last]!=b[l]) last--;
            else break;
        }
        //printf("%d %d %d\n",l,r,last);
        if(last==0){
            puts("-1");return 0;
        }
        while(head<tail){
            if(q[tail-1]-tag>r) tail--;
            else break;
        }
        if(head==tail){
            tag=0;
            q[--head]=last;
        }
        else{
            tag++;
            q[--head]=last+tag;
        }
        //printf("%d %d %d\n",l,head,tail);
        ans=max(tail-head,ans);
    }
    printf("%d\n",ans);
    return 0;
}

#include "bits/stdc++.h"

#define REP(i,num) for(ll i=0;i<(num);++i)
#define LOOP(i) while(i--)
#define ALL(c) c.begin(),c.end()
#define PRINTALL(c) for(auto pitr=c.begin();pitr!=c.end();++pitr){cout<<*pitr;if(next(pitr,1)!=c.end())cout<<' ';}cout<<endl;
#define PAIRCOMP(c,comp) [](const pair<ll,ll>& lhs,const pair<ll,ll>& rhs){return lhs.c comp rhs.c;}

using namespace std;
using ll = long long;

constexpr ll atcoder_mod = 1e9+7;

template<typename T=ll>
T in(){T x; cin >> x; return (x);}
template<typename T=ll,typename C=vector<T>>
C vecin(int N){C x(N);REP(i,N){x[i]=in<T>();}return move(x);}

void vout(){cout << endl;}
template<typename Head,typename... Tail>
void vout(Head&& h,Tail&&... t){cout << ' ' << h;vout(forward<Tail>(t)...);}
void out(){cout << endl;}
template<typename Head,typename... Tail>
void out(Head&& h,Tail&&... t){cout << h;vout(forward<Tail>(t)...);}



int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout<<fixed<<setprecision(10);

	string S=in<string>();
	map<char,ll> M;
	for(auto& x:S) M[x]++;
	ll N=S.size();
	ll A=N*(N-1LL)/2LL;
	for(auto& x:M){
		A -= x.second*(x.second-1LL)/2LL;
	}
	out(A+1LL);
	return 0;
}
