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



NMgcd = math.gcd(N,M)

Leng  = N*M//NMgcd

Flag  = True

SList = [S[Nk] for Nk in range(0,N) if Nk%(N//NMgcd)==0]



for Mk in range(0,len(SList)):

    if SList[Mk]!=T[Mk*(M//NMgcd)]:

        Flag = False

        break

if Flag:

    print(Leng)

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())
NMgcd = math.gcd(N, M)
Leng = N * M // NMgcd
Flag = True
SList = [S[Nk] for Nk in range(0, N) if Nk % (N // NMgcd) == 0]
for Mk in range(0, len(SList)):
    if SList[Mk] != T[Mk * (M // NMgcd)]:
        Flag = False
        break
if Flag:
    print(Leng)
else:
    print((-1))

-NMG = math.gcd(N, M) -Length = N * M // NMG +NMgcd = math.gcd(N, M) +Leng = N * M // NMgcd -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 +SList = [S[Nk] for Nk in range(0, N) if Nk % (N // NMgcd) == 0] +for Mk in range(0, len(SList)): +    if SList[Mk] != T[Mk * (M // NMgcd)]: +        Flag = False +        break -    print(Length) +    print(Leng)