#include <cstdio>
#include <iostream>
using namespace std;
#define ref(i,x,y)for(int i=x;i<=y;++i)
#define def(i,x,y)for(int i=x;i>=y;--i)
const int N=1000010;
int n,a[N],ans=1;
char s[N],t[N];
int main()
{
	scanf("%d",&n);
	scanf("%s%s",s+1,t+1);
	bool flag=0;
	ref(i,1,n)if(s[i]!=t[i])flag=1;
	if(!flag)
	{
		printf("0\n");
		return 0;
	}
	int R=n,L=R,atop=n+1,cnt=n,acnt=n,times=0;
	while(R>0)
	{
		L=R;
		while(L>1&&t[L-1]==t[R])L--;
		if(L<cnt)cnt=L;
		while(cnt>0&&s[cnt]!=t[L])cnt--;
		if(!cnt){
			printf("-1\n");
			return 0;
		}
		if(cnt==L){
			atop=n+1;acnt=n;R=L-1;
			times=0;
			continue;
		}
		if(atop>acnt){
			times++;
			a[--atop]=cnt+times;
			ans=max(ans,2);
			R=L-1;
			continue;
		}
		while(acnt>=atop&&a[acnt]-times>L)acnt--;
		ans=max(ans,acnt-atop+3);
		times++;
		a[--atop]=cnt+times;
		R=L-1;
	}
	printf("%d\n",ans);
}