// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;



#define FOR(i,a,b) for (int i = (a); i < (b); i++)

#define RFOR(i,b,a) for (int i = (b) - 1; i >= (a); i--)

#define ITER(it,a) for (__typeof(a.begin()) it = a.begin(); it != a.end(); it++)

#define FILL(a,value) memset(a, value, sizeof(a))



#define SZ(a) (int)a.size()

#define ALL(a) a.begin(), a.end()

#define PB push_back

#define MP make_pair



typedef long long LL;

typedef vector<int> VI;

typedef pair<int, int> PII;



const double PI = acos(-1.0);

const int INF = 1000 * 1000 * 1000 + 7;

const LL LINF = INF * (LL) INF;



const int MAX = 1001000;



char S[MAX];

char T[MAX];



int A[MAX];



int main()

{

	//freopen("in.txt", "r", stdin);

	//ios::sync_with_stdio(false); cin.tie(0);



	int n;

	scanf("%d", &n);

	scanf("%s", S);

	scanf("%s", T);



	if ((string)(S) ==(string(T)))

	{

		cout<<0<<endl;

		return 0;

	}



	FILL(A, -1);

	int ind = n-1;

	RFOR(i, n, 0)

	{

		while (ind > i) ind--;

		while(ind >= 0 && S[ind] != T[i])

		{

			ind--;

		}



		if (ind == -1)

		{

			cout<<-1<<endl;

			return 0;

		}



		if (A[ind] == -1) A[ind] = i;

	}



	vector<PII> a;

	FOR (i, 0, n)

	{

		if (A[i] != -1) a.PB(MP(i, A[i]));

	}



	/*FOR (i, 0, SZ(a))

	{

		cout<<a[i].first<<' '<<a[i].second<<endl;

	}

	cout<<endl;*/



	int res = 0;



	ind = 0;

	int ind2 = 0;

	FOR (i, 0, SZ(a))

	{

		while(true)

		{

			if (ind2 == SZ(a)) break;

			if (a[ind2].first <= a[i].second)

			{

				ind2++;

				continue;

			}

			break;

		}

		while(true)

		{

			if (ind == SZ(a)) break;

			if (a[ind].first - (ind - i - 1) <= a[i].second)

			{

				ind++;

				continue;

			}

			break;

		}



		res = max(res, ind - i);

	}



	cout<<res<<endl;



}




// Faster C++ solution for the same problem (measured speedup: 4.885880144166931x):
///****In the name of ALLAH, most Gracious, most Compassionate****//



#include <bits/stdc++.h>



using namespace std;



typedef long long ll;

typedef pair <int, int> pii;



#define ALL(a) a.begin(), a.end()

#define FastIO ios::sync_with_stdio(false); cin.tie(0);cout.tie(0)

#define IN freopen("input.txt","r+",stdin)

#define OUT freopen("output.txt","w+",stdout)



#define DBG(a) cerr<< "line "<<__LINE__ <<" : "<< #a <<" --> "<<(a)<<endl

#define NL cout<<endl



template < class T1,class T2>

ostream &operator <<(ostream &os,const pair < T1,T2 > &p)

{

    os<<"{"<<p.first<<","<<p.second<<"}";

    return os;

}



const int N=2e6+5;

const int oo=1e9+7;



int n;

string s,t;



int h[N];





int Tree [N+10];



void update(int idx, ll x,int n)

{

    // Let, n is the number of elements and our queries are

    // of the form query(n)-query(l-1), i.e range queries

    // Then, we should never put N or MAX in place of n here.

    assert(idx>=1);

    while(idx<=n)

    {

        Tree[idx]+=x;

        idx+=(idx&-idx);

    }

}



int query(int idx)

{

    ll sum=0;

    while(idx>0)

    {

        sum+=Tree[idx];

        idx-=(idx&-idx);

    }

    return sum;

}



int val(int idx)

{

    return query(N ) - query(idx-1);

}



int now  = 1 ;





int solve()

{

    if(s==t)

        return 0;

    s="#"+s;

    t="#"+t;



    vector<int > x;

    for(int i=1; i<=n; i++)

    {

        if(i>1 and t[i]==t[i-1])

            continue ;

        x.push_back(i);

    }

    reverse(ALL(x));







    int ss = n;

    int ret =1;



    for(int tt: x)

    {

        while(ss >=1 )

        {

            if(ss<=tt and s[ss] == t[tt])

                break;

            else

            {

                ss--;

                now++;

            }

        }

        if(ss>=1)

        {

            if(ss!=tt)

            {

//                for(int i= ss; i<tt; i++)

//                    h[i] =h[i+1] + 1;

//                h[tt]++;

//                ret = max(ret,  h[tt] + 1);



                int old = val(now-(tt-ss));

                now--;

                update(now , 1 , N);

//                assign_index( now - (tt-ss), old + 1 );

                ret =max(ret, old + 2);



//                assert(old+2 == h[tt]+1);



            }

            ss--;

            now++;

        }

        else

            return -1;

    }

    return ret;

}



int32_t main()

{

    FastIO;

    cin>>n>>s>>t;

    cout<<solve()<<"\n";

}