solve _ _ _ [] _ = True
solve _ _ _ _ [] = True
solve l n m (cs:s) (ct:t)
  | condS && condT && cs /= ct = False
  | otherwise = solve (l-1) n m (if condS then s else cs:s) (if condT then t else ct:t)
  where condS = l `mod` n == 0
        condT = l `mod` m == 0

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
  if solve l (l`div`n) (l`div`m) s t
    then print l
    else print (-1)
