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

i=j=0
while i<L && j<L
    if i == j
        if S[i/(L/N)] != T[j/(L/M)]
            puts -1
            exit 0
        end
        i += L/N
        j += L/M
    elsif i < j
        i += L/N
    else
        j += L/M
    end
end

puts L
