def gcd(x,y)
  x,y = y,x if x < y
  if y == 0
    return x
  else
    return gcd(y,x%y)
  end
end

n,m = gets.split.map(&:to_i)
s = gets.chomp!
t = gets.chomp!
# if n > m
#  n,m,s,t = m,n,t,s
#end
len = n * m / gcd(n,m)

check = []
flag = true

n.times do |i|
  k = len/n * i
  check << [i,k/(len/m)] if k % (len/m) == 0
end
check.each do |a|
  flag = false if s[a[0]] != t[a[1]]
end

if flag
  p len
else
  p -1
end
