(defn input []
  (vector
    (->> (clojure.string/split (read-line) #"\s")
      (map #(Integer/parseInt %)))
    (apply vector (read-line))
    (apply vector (read-line))))

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

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

(defn first-inner [N L]
  (apply (partial vector-of :int) (range 0 L (quot L N))))

(defn second-inner [M T L S Sidx]
  (let [Number (quot L M)]
    (loop [i 0]
       (if (<= M i)
         true
         (let [id (.indexOf Sidx (* i Number))]
           (cond
             (neg? id) (recur (inc i))
             (= (T i) (S id)) (recur (inc i))
             :else false))))))


(defn TwoAbbreviation [[[N M] S T]]
  (let [L (int (lcm N M))
        Sidx (first-inner N L)]
    (if (second-inner M T L S Sidx)
      L
      -1)))


(->> (input)
  TwoAbbreviation
  println)