#include <bits/stdc++.h>


#define gc getchar
#define pc putchar

using namespace std;

/*#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/assoc_container.hpp>

using namespace __gnu_pbds;
*/

/*
        two functions for policy based data structure. it is

                find_by_order() and order_of_key().

        The first returns an iterator to the k-th largest element (counting from zero),
        the second returns the number of items in a set that are strictly smaller than our item

*/

#define vi vector<int>
#define si set<int>
#define vs vector<string>
#define pii pair<int,int>
#define vpi vector<pii>
#define pri priority_queue<int>
#define rev_pri priority_queue<int,vector<int>,greater<int> >
#define mpi map<int,int>
#define i64 long long int
#define endl '\n'
#define pi acos(-1)
#define all(v) v.begin(),v.end()
#define pb push_back
#define mp make_pair
#define For(i,n) for(int i=0;i<n;i++)
#define Rep(i,x,y) for(int i=x;i<=y;i++)
#define eps 1e-8
#define ff first
#define ss second
#define mem(a,b) memset(a,b,sizeof(a))
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define READ freopen("input.txt", "r", stdin)
#define WRITE freopen("output.txt","w", stdout)
#define sz size()
#define dbg(x) printf("yo is %d!\n",x)
#define dbg2(x,y) printf("yo is %d! and %d!\n",x,y)
#define foreach(i,c) for(__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define sqr(a) (a) * (a)
#define clr clear()
#define CASE(a) printf("Case %d: ",a)
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)

//int dx[] = {0,1,0,-1};
//int dy[] = {1,0,-1,0};
//int dx[] = { -1, -1,  0, 1, 1, 1,  0, -1 };
//int dy[] = {  0, -1, -1, -1,  0, 1, 1, 1 };
//int dxK[] = { -2, -2, -1, 1, 2, 2, 1, -1 };
//int dyK[] = { -1, 1, 2, 2, 1, -1, -2, -2 };

//functions

//i64 gcd(i64 a,i64 b){if(!b)return a;return gcd(b,a%b);}

//inline void fastRead(i64 *a){register char c=0;while(c<33)c=gc();*a=0;while(c>33){*a=*a*10+c-'0';c=gc();}}

//inline void fastWrite(int a){char snum[20];int i=0;do{snum[i++]=a%10+48;a=a/10;}while(a!=0);i=i-1;while(i>=0)pc(snum[i--]);pc('\n');}

//i64 bigmod(i64 num,i64 n){if(!n)return 1;i64 x=(bigmod(num,n/2)*bigmod(num,n/2))%mod;if(n%2)x=(x*num)%mod;return x;}

//i64 modinverse(i64 num){return bigmod(num,mod-2);}

//void combination(int pos,int last){if(pos==k+1){for(int i=1;i<=k;i++)cout << tem[i];cout << endl;return;}
//for(int i=last+1;i<=n-k+pos;i++){tem[pos] = num[i-1];combination(pos+1,i);}}
//i64 power(i64 value, i64 base){i64 result = 1;For(i,base)result *= value;return result;}
//int Set(int N,int pos){return N = (1<<pos);}
//int reset(int N,int pos){return N &= (~(1<<pos));}
//bool check(int N,int pos){return (bool)(N & (1<<pos));}

//typedef tree< int, null_type, less < int >, rb_tree_tag, tree_order_statistics_node_update > Set;

#define MAX 250005
#define p1 131
#define p2 999683
#define p3 169
#define mod1 1000000007
#define mod2 1000000009
#define mod3 1000000021

i64 mod;

i64 bigmod(i64 num,i64 n)   // used to calculate num^n
{
    if(n==0)
        return 1;
    i64 x = bigmod(num,n/2);
    x = x*x%mod;
    if(n%2==1)
        x = x*num % mod;
    return x;
}

i64 modinverse(i64 num)
{
    return bigmod(num,mod-2) % mod;
}

i64 pr1[MAX], pr2[MAX], pr3[MAX];

i64 h1, h2, h3, h4, h5, h6;

void prime_calc()
{
    pr1[0] = 1;
    pr2[0] = 1;
    pr3[0] = 1;

    Rep(i,1,MAX-1)
    {
        pr1[i] = (pr1[i-1] * p1) % mod1;
        pr2[i] = (pr2[i-1] * p2) % mod2;
        pr3[i] = (pr3[i-1] * p3) % mod3;
    }
}

int main()
{
    prime_calc();
    int n;
    cin >> n;

    string s1, s2;
    cin >> s1 >> s2;

    if(s1==s2)
    {
        cout << 0 << endl;
        return 0;
    }

    int cnt = 0;

    for(int i = s1.sz-1; i>=0; i--)
    {
        h1 += (s1[i]*pr1[cnt])%mod1;
        h1 %= mod1;
        h2 += (s1[i]*pr2[cnt])%mod2;
        h2 %= mod2;
        h3 += (s1[i]*pr3[cnt])%mod3;
        h3 %= mod3;
        h4 += (s2[i]*pr1[cnt])%mod1;
        h4 %= mod1;
        h5 += (s2[i]*pr2[cnt])%mod2;
        h5 %= mod2;
        h6 += (s2[i]*pr3[cnt])%mod3;
        h6 %= mod3;
        cnt++;
    }

    //cout << h1 << " " << h2 << " " << h3 << " " << h4 << " " << h5 << " " << h6 << endl;

    Rep(i,1,n-1)
    {
        h1 = (h1-s1[n-i]+mod1)%mod1;
        mod = mod1;
        h1 = h1*modinverse(p1);
        h1 %= mod1;

        h2 = (h2-s1[n-i]+mod2)%mod2;
        mod = mod2;
        h2 = h2*modinverse(p2);
        h2 %= mod2;

        h3 = (h3-s1[n-i]+mod3)%mod3;
        mod = mod3;
        h3 = h3*modinverse(p3);
        h3 %= mod3;
        //cout << h1 << " " << h2 << " " << h3 << " " << h4 << " " << h5 << " " << h6 << endl;
        h1 += (s1[n-i]*pr1[n-1])%mod1;
        h1 %= mod1;
        h2 += (s1[n-i]*pr2[n-1])%mod2;
        h2 %= mod2;
        h3+= (s1[n-i]*pr3[n-1])%mod3;
        h3 %= mod3;

        //cout << h1 << " " << h2 << " " << h3 << " " << h4 << " " << h5 << " " << h6 << endl;

        if(h1==h4 && h2==h5 && h3==h6)
        {
            cout << i << endl;
            return 0;
        }
    }

    cout << -1 << endl;

    return 0;
}
