#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
  
  char st1[1000001],st2[1000001];
  int n,des[1000001],pos[1000001],cnt;

  int main(){
//    freopen("game.in","r",stdin);
	  
  	scanf("%d",&n);
  	scanf("%s",st1+1);scanf("%s",st2+1);
  	if(strcmp(st1+1,st2+1)==0) {printf("0\n"); return 0;}
  	int po=n;
  	for (int i=n,j=n;i;i=j-1,j=i){
  	  while (st2[j-1]==st2[i]) j--;
  	  po=min(po,j);
	  while (po&&st1[po]!=st2[i]) po--;
	  if (!po){printf("-1\n");return(0);}
	  des[po]=i;
	}
	
	int ans=0,lastans=0;
	for (int i=n;i>=1;i--) if (des[i]){
	  	lastans++;
	  	pos[++cnt]=i;
	  	while (lastans){
	  	  int t=cnt-(lastans-1),tpo;
	  	  if (t<=0) tpo=n-(lastans-1);else tpo=pos[t]-(lastans-1);
	  	  if (tpo<des[i]) break;
	  	  lastans--;
		}
		ans=max(ans,lastans);
	  }
	printf("%d\n",ans);
  }