import Control.Applicative main :: IO () main = solve <$> readLn <*> readLn >>= print solve :: Integer -> Integer -> Int solve n k = let r = f (n `mod` 6) k in if r == 0 then 4 else [2,8,5,7,1] !! (fromIntegral (r - 1)) where f x y | y == 0 = 1 | even y = let z = f x (y `div` 2) in z * z `mod` 6 | otherwise = let z = f x ((y-1) `div` 2) in x * z * z `mod` 6