import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;

T gcd(T)(T a, T b) {
    if (b == 0) return a;
    return gcd(b, a % b);
}

T lcm(T)(T a, T b) {
    return a * (b / gcd(a, b));
}

long calc(string s, string t) {
    int slen = cast(int) s.length;
    int tlen = cast(int) t.length;
    long l = cast(long) lcm(s.length, t.length);

    char[long] d;
    for (int i = 0; i < s.length; i++) {
        long p = i * (l / slen);
        d[p] = s[i];
    }

    for (int i = 0; i < t.length; i++) {
        long p = i * (l / tlen);
        if (p in d) {
            if (d[p] != t[i]) return -1;
        }
    }

    return l;
}

void main() {
    read!string;
    auto s = read!string;
    auto t = read!string;
    writeln(calc(s, t));
}
