#include <cstdio>
#include <algorithm>
using namespace std;
int n, ans, tot, to[1000005], u[1000005], v[1000005];
char S[1000005], T[1000005];
bool check(int x) {
	for (int i = 1; i <= tot - x; ++i)
		if (u[i + x] - x < v[i])
			return false;
	return true;
}
int main() {
    scanf("%d", &n);
    scanf("%s", S + 1);
    scanf("%s", T + 1);
	int ptr = n;
	for (int i = n; i >= 1; --i) {
		ptr = min(ptr, i);
		while (ptr > 0 && S[ptr] != T[i]) --ptr;
		if (ptr == 0) {
			puts("-1");
			return 0;
		}
		if (to[ptr] == 0) to[ptr] = i;
	}
	for (int i = 1; i <= n; ++i)
		if (to[i] != 0) {
			++tot;
			u[tot] = i;
			v[tot] = to[i];
		}
	int L = 0, R = n, mid;
	while (L < R) {
		mid = (L + R) >> 1;
		if (check(mid)) R = mid;
		else L = mid + 1;
	}
	printf("%d\n", R);
    return 0;
}

#include<bits/stdc++.h>

constexpr auto MAXN = 1000000+500;

std::string s,t;
std::queue<int> que;

signed main(void)
{
  int len,down,up; size_t ans = 0;

  std::cin>>len>>s>>t;

  if(s == t){ puts("0"); return 0; }
  
  down = up = len-1;

  while(down >= 0){
    while(down && t[down-1]==t[down]) --down;
    while(up>=0 && (up>down || s[up]!=t[down])) --up;
    if(up < 0){ puts("-1"); return 0; }

    while(!que.empty() && que.front()-que.size() >= down) que.pop();
    if(up != down) que.push(up);
    ans = std::max(ans,que.size()+1);
    --down;
  }

  std::cout<<ans<<std::endl;
  
  return 0;
}
