import Data.Maybe

main :: IO ()
main = do
  [n,m] <- map read . words <$> getLine
  s <- getLine
  t <- getLine
  print $ fromMaybe (-1) $ solve n m s t

solve :: Int -> Int -> String -> String -> Maybe Int
solve n m s t = if b then Just l else Nothing
  where
    l = lcm n m
    n' = l `div` n
    m' = l `div` m
    l' = lcm n' m'
    b = all (\(i,j) -> s !! i == t !! j) [(p`div`n',p`div`m') | x <- [0..(l`div`l'-1)], let p = x*l']
