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

S = eval(input())

T = eval(input())



from fractions import gcd



def solve(N,M,S,T):

    g = gcd(N,M)

    for i in range(g):

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

            return -1

    ans = N*M//g

    return ans

print((solve(N,M,S,T)))
from math import gcd

def solve():

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

  S = eval(input())

  T = eval(input())

  g = gcd(N,M)

  ans = N*M//g

  for i in range(g):

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

      return -1

  return ans

print((solve()))
N, M = list(map(int, input().split()))
S = eval(input())
T = eval(input())
from fractions import gcd


def solve(N, M, S, T):
    g = gcd(N, M)
    for i in range(g):
        if S[N // g * i] != T[M // g * i]:
            return -1
    ans = N * M // g
    return ans


print((solve(N, M, S, T)))

from math import gcd


def solve():
    N, M = list(map(int, input().split()))
    S = eval(input())
    T = eval(input())
    g = gcd(N, M)
    ans = N * M // g
    for i in range(g):
        if S[N // g * i] != T[M // g * i]:
            return -1
    return ans


print((solve()))

-N, M = list(map(int, input().split())) -S = eval(input()) -T = eval(input()) -from fractions import gcd +from math import gcd -def solve(N, M, S, T): +def solve(): +    N, M = list(map(int, input().split())) +    S = eval(input()) +    T = eval(input()) +    ans = N * M // g -    ans = N * M // g -print((solve(N, M, S, T))) +print((solve()))