import java.io.*;
import java.math.BigInteger;
import java.util.*;

class CyclicHashes implements Runnable {

    private BufferedReader in;
    private PrintWriter out;

    void init() throws FileNotFoundException {
        in = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);
    }

    public static void main(String[] args) {
        new Thread(null, new CyclicHashes(), "", 1l * 200 * 1024 * 1024).start();
    }

    public void run() {
        try {
            init();
            solve();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(-1);
        }
    }

    int n;
    int prime1, prime2;
    int mod1, mod2;
    int[] pow1, pow2;
    int[] h1, h2;

    void init(char[] s) {
        Random rnd = new Random();
        prime1 = BigInteger.valueOf(3000 + rnd.nextInt(500))
                .nextProbablePrime().intValue();
        prime2 = BigInteger.valueOf(4000 + rnd.nextInt(500))
                .nextProbablePrime().intValue();
        mod1 = BigInteger.valueOf((int) 1e9 + rnd.nextInt(500))
                .nextProbablePrime().intValue();
        mod2 = BigInteger.valueOf((int) 1e9 + (int) 1e6 + rnd.nextInt(500))
                .nextProbablePrime().intValue();
        n = s.length;
        pow1 = new int[n];
        pow2 = new int[n];
        pow1[0] = pow2[0] = 1;
        for (int i = 1; i < n; i++) {
            pow1[i] = (int) ((long) pow1[i - 1] * prime1 % mod1);
            pow2[i] = (int) ((long) pow2[i - 1] * prime2 % mod2);
        }
        h1 = new int[n];
        h2 = new int[n];
        h1[0] = h2[0] = s[0];
        for (int i = 1; i < n; i++) {
            h1[i] = (int) ((h1[i - 1] + (long) pow1[i] * s[i]) % mod1);
            h2[i] = (int) ((h2[i - 1] + (long) pow2[i] * s[i]) % mod2);
        }
    }

    long getHash(int L, int R) {
        int hash1 = getHash(L, R, h1, pow1, mod1);
        int hash2 = getHash(L, R, h2, pow2, mod2);
        return ((long) hash1 << 32) | hash2;
    }

    int getHash(int L, int R, int[] h, int[] pow, int mod) {
        int hash = h[R];
        if (L > 0)
            hash -= h[L - 1];
        if (hash < 0)
            hash += mod;
        return (int) ((long) hash * pow[n - R - 1] % mod);
    }

    boolean equals(int L1, int R1, int L2, int R2) {
        long hash1 = getHash(L1, R1);
        long hash2 = getHash(L2, R2);
        return hash1 == hash2;
    }

    void solve() throws IOException {
        int n = Integer.parseInt(in.readLine());
        String s = in.readLine();
        String t = in.readLine();
        StringBuilder sb = new StringBuilder();
        sb.append(s);
        sb.append(t);
        sb.append(t);
        char[] res = sb.toString().toCharArray();

        init(res);

        for (int shift = 0; shift < n; shift++) {
            if (equals(0, n - 1, n + shift, 2 * n - 1 + shift)) {
                out.println(shift);
                return;
            }
        }
        out.println(-1);

    }

}