import math

N,M = (int(T) for T in input().split())

S = eval(input())

T = eval(input())

Length = N*M//math.gcd(N,M)

Flag   = True

SIndex = [Nk*(Length//N) for Nk in range(0,N)]

SList  = list(S)

    

for Mk in range(0,M):

    if Mk%(Length//N)==0:

        MInd = SIndex.index(Mk*(Length//M))

        if SList[MInd]!=T[Mk]:

            Flag = False

            break

        

if Flag:

    print(Length)

else:

    print((-1))
import math

N,M = (int(T) for T in input().split())

S = eval(input())

T = eval(input())



NMG = math.gcd(N,M)

Length = N*M//NMG

Flag   = True

SIndex = [Nk*(M//NMG) for Nk in range(0,N)]

SList  = list(S)



Count = 0

for Mk in range(0,M):

    if Mk%(M//NMG)==0:

        if SList[Count*(N//NMG)]!=T[Mk]:

            Flag = False

            break

        else:

            Count += 1

if Flag:

    print(Length)

else:

    print((-1))
import math

N, M = (int(T) for T in input().split())
S = eval(input())
T = eval(input())
Length = N * M // math.gcd(N, M)
Flag = True
SIndex = [Nk * (Length // N) for Nk in range(0, N)]
SList = list(S)
for Mk in range(0, M):
    if Mk % (Length // N) == 0:
        MInd = SIndex.index(Mk * (Length // M))
        if SList[MInd] != T[Mk]:
            Flag = False
            break
if Flag:
    print(Length)
else:
    print((-1))

import math

N, M = (int(T) for T in input().split())
S = eval(input())
T = eval(input())
NMG = math.gcd(N, M)
Length = N * M // NMG
Flag = True
SIndex = [Nk * (M // NMG) for Nk in range(0, N)]
SList = list(S)
Count = 0
for Mk in range(0, M):
    if Mk % (M // NMG) == 0:
        if SList[Count * (N // NMG)] != T[Mk]:
            Flag = False
            break
        else:
            Count += 1
if Flag:
    print(Length)
else:
    print((-1))

-Length = N * M // math.gcd(N, M) +NMG = math.gcd(N, M) +Length = N * M // NMG -SIndex = [Nk * (Length // N) for Nk in range(0, N)] +SIndex = [Nk * (M // NMG) for Nk in range(0, N)] +Count = 0 -    if Mk % (Length // N) == 0: -        MInd = SIndex.index(Mk * (Length // M)) -        if SList[MInd] != T[Mk]: +    if Mk % (M // NMG) == 0: +        if SList[Count * (N // NMG)] != T[Mk]: +        else: +            Count += 1