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

m.times do |i|
  k = n.quo(m)*i
  check << k.to_i if k == k.to_i
end
check.each do |idx|
  flag = false if s[len/m*idx] != t[len/n*idx]
end

if flag
  p len
else
  p -1
end
