import Control.Applicative ((<$>), (<*>)) import Data.ByteString.Char8 (ByteString) import qualified Data.ByteString.Char8 as B main :: IO () main = solve <$> (cut 1 <$> B.getLine) <*> B.getLine >>= print solve :: Int -> ByteString -> Int solve n bs | bs == B.pack "0" = 1 | otherwise = ls !! ((m-1) `mod` x) where tbl = [(1, [0]), (1, [1]), (4, [2, 4, 8, 6]), (4, [3, 9, 7, 1]), (2,[4, 6]), (1, [5]), (1, [6]), (4, [7, 9, 3, 1]), (4, [8, 4, 2, 6]), (2, [9, 1])] (x, ls) = tbl !! n m = cut 2 bs readi :: Integral a => (ByteString -> Maybe (a, ByteString)) -> ByteString -> a readi f s = let Just (n, _) = f s in n cut :: Int -> ByteString -> Int cut n bs = readi B.readInt (if l > n then B.drop (l-n) bs else bs) where l = B.length bs