let n, m, s, t = Scanf.scanf "%d %d %s %s" @@ fun a b c d -> a, b, c, d
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 l = g n m
let n', m' = l / n, l / m
let l', i = g n' m', ref 0
let _ = while !i * l'/n' < n && !i * l'/m' < m do if s.[!i * l'/n'] <> t.[!i * l'/m'] then (print_endline "-1"; exit 0); incr i done; Printf.printf "%d\n" l