let rec f a b = if b = 0 then a else f b (a mod b)
let n, m, s, t, l = Scanf.scanf "%d %d %s %s" @@ fun n m s t -> n, m, s, t, n / f n m * m
let d1, d2 = l / m, l / n
let _ = for i = 0 to min ((n - 1) / d1) ((m - 1) / d2) do if s.[i * d1] <> t.[i * d2] then (print_endline "-1"; exit 0) done; Printf.printf "%d\n" l