N, M = gets.split.map(&:to_i).sort
S, T = [gets.chomp, gets.chomp].sort{|a, b| a.length <=> b.length}

if M % N != 0
    puts N.lcm(M)
    exit
end

U = (1..T.length).select{|v| v % N == 1}.map{|v| T[v-1]}.join("")
puts S[0..N-1] == U ? M : -1
