import Data.Char import Debug.Trace main = do n <- getLine k <- getLine let r = [2,8,5,7,1,4] n' = modBigInteger n 6 a = case n' of 0 -> 6 1 -> 1 2 -> [4,2] !! (modBigInteger k 2) 3 -> 3 4 -> 4 5 -> [5,1] !! (modBigInteger k 2) print $ r !! ((a+5)`mod`6) modBigInteger :: String -> Int -> Int modBigInteger [] _ = 0 modBigInteger xs m = foldl (\x y -> (x*10 + y) `mod` m) 0 (map digitToInt xs)