# N = 15
# M = 9
# S = "dnsusrayukuaiia"
# T = "dujrunuma"

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

p N,M,S,T

lcm = N.lcm(M)

i = 0
while i < lcm
    if i == 0
        if S[i] != T[i] 
            p -1
            exit
        end
    else
        unless S[lcm/M ].nil? || T[lcm/N].nil?
            if S[lcm/M] != T[lcm/N]
                p -1 
                exit
            end
        end
    end

    i = i+1
end

p lcm
