import math

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

S = eval(input())

T = eval(input())

g = math.gcd(N,M)

l = N*M//g

print((l if S[::l//M] == T[::l//N] else -1))

import math

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

S = eval(input())

T = eval(input())

g = math.gcd(N,M)

l = N*M//g

print((l if S[::N//g] == T[::M//g] else -1))

import math

N, M = list(map(int, input().split()))
S = eval(input())
T = eval(input())
g = math.gcd(N, M)
l = N * M // g
print((l if S[:: l // M] == T[:: l // N] else -1))

import math

N, M = list(map(int, input().split()))
S = eval(input())
T = eval(input())
g = math.gcd(N, M)
l = N * M // g
print((l if S[:: N // g] == T[:: M // g] else -1))

-print((l if S[:: l // M] == T[:: l // N] else -1)) +print((l if S[:: N // g] == T[:: M // g] else -1))