import Control.Applicative
import Control.Monad
import Data.List
import qualified Data.ByteString.Char8 as B

solve :: Int -> Int -> String -> String -> Int
solve n m ss ts
    | all (\(q, r) -> B.index bs q == B.index bt r) (zip qs rs)
        = lcm'
    | otherwise = -1
  where
    lcm' = lcm n m
    bs = B.pack ss
    bt = B.pack ts
    sx = lcm' `div` n
    tx = lcm' `div` m
    qs = [0, tx .. n - 1]
    rs = [0, sx .. m - 1]

main = do
    [n, m] <- fmap read . words <$> getLine :: IO [Int]
    ss <- getLine
    ts <- getLine
    print $ solve n m ss ts