def lcm(a, b)
  [a, b].inject(:lcm)
end

def gcd(a, b)
  [a, b].inject(:gcd)
end

N, M = gets.split.map(&:to_i)
S = gets.chomp
T = gets.chomp


lcm = lcm(N, M)
gcd = gcd(N, M)

n = N/gcd
m = M/gcd

for i in 0..(gcd-1) do
  unless S[n*i] == T[m*i]
    puts -1
    exit
  end
end

puts lcm