#include<cstdio>
#include<cstring>
#include<algorithm>
#define For(i,a,b) for(int i=a;i<=b;++i)
#define Fdn(i,b,a) for(int i=b;i>=a;--i)
#define Printno {puts("-1"); return 0;}
using namespace std;
const int MAXN=1000005;

int n,ans,p[MAXN],ed[MAXN],tot;
char a[MAXN],b[MAXN],ch[MAXN];

int main(){
	scanf("%d%s%s",&n,a+1,b+1);
	int j=n+1, pr=n;
	bool bo=1;
	Fdn(i,n,1){
		if(b[i-1]!=b[i]){
			j=min(j-1,i);
			while(j>0&&a[j]!=b[i]) j--;
			//printf("%d :find %d\n",i,j);
			if(!j) Printno;
			p[++tot]=j;
			ed[tot]=pr; pr=i-1;
			ch[tot]=b[i];
		}
		if(a[i]!=b[i]) bo=0;
	}
	if(bo){puts("0"); return 0;}
	//Fdn(i,tot,1) printf("test: %d %d\n",p[i],ed[i]);
	p[0]=n+1;
	For(i,1,tot){
		while(p[i-ans]-ans<ed[i]) ans++;
	}
	printf("%d\n",ans);
	
	return 0;
}