# 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()
# AGC028A - Two Abbreviations

def gcd(a, b):

    while b > 0:

        a, b = b, a % b

    return a





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()
# 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()

# AGC028A - Two Abbreviations
def gcd(a, b):
    while b > 0:
        a, b = b, a % b
    return a


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()

-from fractions import gcd +def gcd(a, b): +    while b > 0: +        a, b = b, a % b +    return a