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