def solve(n,m,s,t)
  lcm = n.lcm(m)
  1.upto(Float::INFINITY){|p|
    l = p * lcm
    ar=Array.new(l)

    # S
    0.upto(n-1){|i|
      idx = i*l/n+1-1
      ar[idx] = s[i]
    }

    # T
    0.upto(m-1){|i|
      idx = i*l/m+1-1
      if ar[idx] == nil
        ar[idx] = t[i]
      elsif ar[idx] != t[i]
        return -1
      end
    }

    return l
  }
end


# p solve 15,9,"dnsusrayukuaiia","dujrunuma"
# p solve 6,3,"abcdef","abc"
# p solve 3,2,"acp","ae"
a,s,t = readlines.map(&:chomp)
n,m=a.split.map(&:to_i)
p solve(n,m,s,t)
