pick :: Int -> String -> String
pick _ [] = []
pick 0 l = []
pick n l = (head l) : (pick n (drop n l))

loop :: Int -> Int -> String -> String -> [Int] -> Bool
loop n m s t [] = True
loop n m s t (x:cd) = if (pick (n`div`x) s) == (pick (m`div`x) t)
                then loop n m s t cd
                else False

check :: Int -> Int -> String -> String -> Int
check n m s t = let g = gcd n m
                    cd = [ x | x <- [1..(min n m)], (g `mod` x) == 0]
                  in if loop n m s t cd
                         then lcm n m else -1

main :: IO ()
main = do
  [n,m] <- map read . words <$> getLine
  s <- getLine
  t <- getLine
  putStrLn $ show $ check n m s t