N, M = gets.split.map(&:to_i).sort
S, T = [gets.chomp, gets.chomp].sort_by(&:length)

ans = N.lcm(M)
a = (ans / N).lcm(ans / M)
U = [ans, N].min.times.select { |v| v % a == 0}.map { |v| S[v] }.join('')
V = [ans, M].min.times.select { |v| v % a == 0}.map { |v| T[v] }.join('')
puts U == V ? N.lcm(M) : -1