import Data.ByteString (ByteString)
import qualified Data.ByteString as BS

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

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