#include<bits/stdc++.h>

using namespace std;

#define fo(i,a,b) for (int i=a;i<=b;i++)
#define fd(i,a,b) for (int i=a;i>=b;i--)
#define fi first
#define se second	
#define mkp make_pair

typedef long long ll;

const int N=1100000;

int n,ans,flag,s[N],t[N],pos[N],a[N],b[N];

int read()
{
	int ret=0,sig=1;
	char c;
	while (c=getchar(),(c<'0'||c>'9')&&c!='-');
	if (c=='-') sig=-1;
	else ret=c-'0';
	while (c=getchar(),c>='0'&&c<='9') ret=ret*10+c-'0';
	return ret*sig;
}

int main()
{
	n=read();
	fo(i,1,n)
	{
		char c;
		while (c=getchar(),c<'a'||c>'z');
		s[i]=c-'a'+1;
	}
	fo(i,1,n)
	{
		char c;
		while (c=getchar(),c<'a'||c>'z');
		t[i]=c-'a'+1;
		if (s[i]!=t[i]) flag=1;
	}
	if (!flag) 
	{
		printf("0\n");
		return 0;
	}
	int w=n+1;
	b[0]=1;
	fd(i,n+1,1)
		if (t[i]!=t[i-1])
		{
			b[++b[0]]=i-1;
			while (w&&(w>i||s[w]!=t[i])) w--;
			if (!w)
			{
				printf("-1\n");
				return 0;
			}
			a[++a[0]]=w;
		}
	int tail=1;
	fo(i,2,a[0])
	{
		while (tail<a[0]&&a[tail+1]-(i-tail-1)>=b[i]) tail++;
		ans=max(ans,i-tail);
	}
	printf("%d\n",ans);
	return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define N 110
#define INF 0x3f3f3f3f
using namespace std;
typedef struct {
    int w;
    int s;
    int e;
}Edge;
Edge edge[N*N];
int m[N][N];
int father[N];
int n;
int k = 0;  //edge的下标
int result = 0;
int findfather(int x){
    return x == father[x] ? x : findfather(father[x]);
}
int cmp(Edge a, Edge b){
    return a.w < b.w;
}
void Kruscal(){
    for(int u = 0; u < k; u++){
        int s = edge[u].s;
        int e = edge[u].e;
        if(findfather(s) != findfather(e)){
            father[findfather(e)] = s;
            result += edge[u].w;
        }
    }
    printf("%d\n", result);
}
int main(){

    scanf("%d", &n);
    for(int i = 1; i <= n; i++) father[i] = i;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            int b;
            scanf("%d", &b);
            m[i][j] = b;
        }
    }
    for(int i = 1; i <= n; i++){
        for(int j = i; j <= n ;j++){
            if(m[i][j] != -1) {
                edge[k].w = m[i][j];
                edge[k].s = i;
                edge[k].e = j;
                k++;
            }
        }
    }
    sort(edge, edge+k, cmp);
    Kruscal();
    return 0;
}

