def gcd(a, b)
  a, b = b, a if a > b
  until a == 0
    a, b = b % a, a
  end
  return b
end

def lcm(a, b)
  a*b/gcd(a, b)
end

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

if N % M == 0 || M % N == 0
  puts -1
else
  puts lcm(N, M)
end
