#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define File(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)
#define maxn 1000010

using namespace std;
typedef long long llg;

struct data{
	int l,r;
	data(int a=0,int b=0):l(a),r(b){}
}s[maxn];
int *q[26],lq[26],ct[26],n;
int R[maxn],ls,d[maxn];
char S[maxn],T[maxn];

int erfen(int *a,int r,int x){
	int l=0,mid;
	while(l!=r){
		mid=(l+r)>>1;
		if(a[mid]>=x) r=mid;
		else l=mid+1;
	}
	return l;
}

int main(){
	scanf("%d",&n);
	scanf("%s\n%s",S+1,T+1);
	for(int i=1;i<=n;i++) ct[S[i]-'a']++;
	for(int i=0;i<26;i++) q[i]=new int[ct[i]+10];
	for(int i=1,x;i<=n;i++)
		x=S[i]-'a',q[x][++lq[x]]=i;
	for(int i=0;i<26;i++) q[i][++lq[i]]=n+1,q[i][0]=0;
	for(int i=n,x,y,l=n;i;i--){
		x=T[i]-'a'; l=min(l,i);
		y=q[x][erfen(q[x],lq[x],l+1)-1];
		if(!y) return printf("-1"),0;
		if(!R[y]) R[y]=i; if(y<l) l=y;
	}
	for(int i=1;i<=n;i++)
		if(R[i]) s[++ls]=data(i,R[i]),d[ls]=i-ls;
	s[++ls]=data(n*2,n*2),d[ls]=n*2-ls;
	int ans=0;
	for(int i=1,j=1;i<=ls;i++){
		while(d[j]<s[i].r-i) j++;
		ans=max(ans,j-i);
	}
	printf("%d",ans);
	return 0;
}
#include<bits/stdc++.h>
using namespace std;

typedef long long lint;
typedef long double louble;

template<typename T1,typename T2> inline T1 max(T1 a,T2 b){return a<b?b:a;}
template<typename T1,typename T2> inline T1 min(T1 a,T2 b){return a<b?a:b;}

namespace ae86
{
	const int bufl = 1<<15;
	
	char buf[bufl],*s=buf,*t=buf;
	
	inline int fetch()
	{
		if(s==t){t=(s=buf)+fread(buf,1,bufl,stdin);if(s==t)return EOF;}
		return *s++;
	}
	
	inline int ty()
	{
		int a=0,b=1,c=fetch();
		while(!isdigit(c))b^=c=='-',c=fetch();
		while(isdigit(c))a=a*10+c-48,c=fetch();
		return b?a:-a;
	}

	inline int ts(char *s)
	{
		int a=0,c=fetch();
		while(c<=32 && c!=EOF)c=fetch();
		while(c>32 && c!=EOF)s[a++]=c,c=fetch();
		s[a]=0;
		return a;
	}
}
using ae86::ty;
using ae86::ts;

const int _ = 1000007;

int n,que[_],he,ta;char s[_]={0},t[_]={0};

int main()
{
	n=ty(),ts(s+1),ts(t+1);

	int got=0;
	for(int i=1;i<=n;i++)if(s[i]!=t[i]){got=1;break;}
	if(!got){puts("0");return 0;}
	
	he=1,ta=0;
	int ans=0;
	for(int i=n,j=n;i>=1;i--)
	{
		if(t[i]==t[i-1])continue;
		j=min(j,i);
		while(j>0 && s[j]!=t[i])j--;
		if(j<=0){puts("-1");return 0;}
		while(he<=ta && que[he]-(ta-he+1)+1>i)he++;
		que[++ta]=j;
		if(i!=j)ans=max(ans,ta-he+1);
	}
	printf("%d\n",ans+1);

	return 0;
}
