import Data.Maybe
import Data.List

solve :: Int -> Int -> String -> String -> Int
solve n m s t = ans
  where
    g = gcd n m
    lcm = n * m `div` g
    s' = perc (n `div` g) s
    t' = perc (m `div` g) t
    ans | s' == t' = lcm
        | otherwise = -1

perc k "" = ""
perc k (c:str) = c : perc k (drop (k-1) str)


tmain :: String -> Int
tmain cont =
  let [sn, sm, s, t]  = words cont
      n = read sn
      m = read sm
  in solve n m s t

main = putStrLn . show . tmain =<< getContents
