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