#include <cstdio>
const int N=2000000;
char s[N+1],t[N+1],q[N+1];
int n,i,l,r=-1,tmp,ans;
int main() {
	scanf("%d%s%s",&n,s,t);
	for (i=n-1;i>=0;i--) {
		if (l>r&&s[i]==t[i]) continue;
		if (l>r||t[i]!=q[r]) q[++r]=t[i],++tmp;
		if (s[i]==q[l]) ++l;
		if (l>r) {if (ans<tmp) ans=tmp;tmp=0;}
	}
	printf("%d\n",tmp?-1:ans);
} 