(set! *unchecked-math* true)
(defn input []
  (let [[N M] (->> (clojure.string/split (read-line) #"\s") (map #(Integer/parseInt %)))]
    (vector N M
      (char-array N  (read-line))
      (char-array M (read-line)))))

(defn gcd [A B]
  (.gcd (BigInteger. (str A)) (BigInteger. (str B))))

(defn lcm [a b]
  (quot (* (bigint a) (bigint b)) (gcd a b)))

(defn make-idx [N M L]
  (vector
    (apply (partial vector-of :int) (range 0 L (quot L N)))
    (apply (partial vector-of :int) (range 0 L (quot L M)))))

(defn judge [N ^"[C" S ^"[C" T S-idxs T-idxs]
  (loop [i (int 0)]
    (if (< i N)
     (let [sidx (S-idxs i)
           tidx (.indexOf T-idxs sidx)]
       (if (neg? tidx)
         (recur (inc i))
         (if (= (aget ^"[C" S i) (aget ^"[C" T tidx))
           (recur (inc i))
           false)))
     true)))


(defn TwoAbbreviation [[N M S T]]
  (let [L (lcm N M)
        [S-Idxs T-Idxs] (make-idx N M L)]
    (if (judge N  S T S-Idxs T-Idxs)
      (str L)
      -1)))

(->> (input)
  TwoAbbreviation
  println)