import Data.List (intercalate)

solve [] = True
solve ((cs, ct):st)
  | cs == '_' || ct == '_' || cs == ct = solve st
  | otherwise = False

main :: IO ()
main = do
  [n,m] <- map read.words <$> getLine :: IO [Int]
  s <- getLine :: IO String
  t <- getLine :: IO String
  let l = lcm n m
      sl =  intercalate (replicate ((l`div`n)-1) '_') (map (:[]) s)
      tl =  intercalate (replicate ((l`div`m)-1) '_') (map (:[]) t)
  if solve (zip sl tl)
    then print l
    else print (-1)
