// Slower C++ solution:
// LNU_Algotesters

#include <iostream>

#include <sstream>

#include <iomanip>

#include <cstdio>

#include <cstdlib>

#include <cmath>

#include <cctype>

#include <cstring>

#include <vector>

#include <list>

#include <queue>

#include <deque>

#include <stack>

#include <map>

#include <set>

#include <algorithm>

#include <iterator>

#include <bitset>

#include <ctime>

#include <fstream>

#include <complex>

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 = 100 * 10000 + 47;





int W[MAX];

bool U[MAX];

int X[MAX];



int main()

{

    

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

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

    

    int n;

    cin >> n;

    string S, T;

    cin >> S >> T;

    int idx = n - 1;

    FILL(W, -1);

    RFOR(i, n, 0)

    {

        while(idx >= 0 && (idx > i || S[idx] != T[i]))--idx;

        W[i] = idx;

        U[idx] = 1;

        X[idx] = max(X[idx], i);

    }

    

    if (S == T)

    {

        cout << 0 << endl;

        return 0;

    }

    

    FOR(i, 0, n)

        if (W[i] == -1)

        {

            cout << -1 << endl;

            return 0;

        }

    

    vector<PII> V;

    

    FOR(i, 0, n)

        if (U[i])V.PB(MP(i, X[i]));

    

    int ans = 0;

    

    FOR(i, 0, SZ(V))

    {

        int L = i, R = SZ(V);

        while(R - L > 1)

        {

            int M = (R + L) / 2;

            if (V[M].first + i >= V[i].second + M)

                R = M;

            else L = M;

        }

        ans = max(ans, R - i);

    }

    cout << ans << endl;

    

    

}








// Faster C++ solution for the same problem (measured speedup: 4.959964239809959x):
///****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";

}