# AGC028

from fractions import gcd





def main():

    n, m = tuple(map(int, input().rstrip().split()))

    S, T = tuple(input().rstrip() for _ in range(2))

    l = n // gcd(n, m) * m

    x = {}

    for i in range(n):  # place S in x

        j = (l // n) * i

        x[j] = S[i]

    for i in range(m):  # check T adapts to x or not

        j = (l // m) * i

        if j in x and x[j] != T[i]:

            print((-1))

            break

    else:

        print(l)





if __name__ == "__main__":

    main()
# AGC028A - Two Abbreviations

from fractions import gcd





def main():

    n, m = tuple(map(int, input().rstrip().split()))

    S, T = tuple(input().rstrip() for _ in range(2))

    g = gcd(n, m)

    flg = S[:: n // g] == T[:: m // g]

    print((n // g * m if flg else -1))





if __name__ == "__main__":

    main()
# AGC028
from fractions import gcd


def main():
    n, m = tuple(map(int, input().rstrip().split()))
    S, T = tuple(input().rstrip() for _ in range(2))
    l = n // gcd(n, m) * m
    x = {}
    for i in range(n):  # place S in x
        j = (l // n) * i
        x[j] = S[i]
    for i in range(m):  # check T adapts to x or not
        j = (l // m) * i
        if j in x and x[j] != T[i]:
            print((-1))
            break
    else:
        print(l)


if __name__ == "__main__":
    main()

# AGC028A - Two Abbreviations
from fractions import gcd


def main():
    n, m = tuple(map(int, input().rstrip().split()))
    S, T = tuple(input().rstrip() for _ in range(2))
    g = gcd(n, m)
    flg = S[:: n // g] == T[:: m // g]
    print((n // g * m if flg else -1))


if __name__ == "__main__":
    main()

-# AGC028 +# AGC028A - Two Abbreviations -    l = n // gcd(n, m) * m -    x = {} -    for i in range(n):  # place S in x -        j = (l // n) * i -        x[j] = S[i] -    for i in range(m):  # check T adapts to x or not -        j = (l // m) * i -        if j in x and x[j] != T[i]: -            print((-1)) -            break -    else: -        print(l) +    g = gcd(n, m) +    flg = S[:: n // g] == T[:: m // g] +    print((n // g * m if flg else -1))