import sys

input=sys.stdin.readline

import bisect



import fractions

def lcm(x, y):

    return (x * y) // fractions.gcd(x, y)



def main():

    N,M = list(map(int,input().split()))

    S = input().strip()

    T = input().strip()

    m = lcm(N,M)

    for i in range(M):

        si = i*m//M

        ti = i*m//N

        if si < N and ti < M and S[si] != T[ti]:

            print("-1")

            return

    print(m)



if __name__ == '__main__':

    main()

import sys

input=sys.stdin.readline



import fractions

def lcm(x, y):

    return (x * y) // fractions.gcd(x, y)



def main():

    N,M = list(map(int,input().split()))

    S = input().strip()

    T = input().strip()

    L = lcm(N,M)

    for i in range(fractions.gcd(N,M)):

        if S[i*L//M] != T[i*L//N]:

            print("-1")

            return

    print(L)



if __name__ == '__main__':

    main()

import sys

input = sys.stdin.readline
import bisect
import fractions


def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)


def main():
    N, M = list(map(int, input().split()))
    S = input().strip()
    T = input().strip()
    m = lcm(N, M)
    for i in range(M):
        si = i * m // M
        ti = i * m // N
        if si < N and ti < M and S[si] != T[ti]:
            print("-1")
            return
    print(m)


if __name__ == "__main__":
    main()

import sys

input = sys.stdin.readline
import fractions


def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)


def main():
    N, M = list(map(int, input().split()))
    S = input().strip()
    T = input().strip()
    L = lcm(N, M)
    for i in range(fractions.gcd(N, M)):
        if S[i * L // M] != T[i * L // N]:
            print("-1")
            return
    print(L)


if __name__ == "__main__":
    main()

-import bisect -    m = lcm(N, M) -    for i in range(M): -        si = i * m // M -        ti = i * m // N -        if si < N and ti < M and S[si] != T[ti]: +    L = lcm(N, M) +    for i in range(fractions.gcd(N, M)): +        if S[i * L // M] != T[i * L // N]: -    print(m) +    print(L)