N=int(input()) K=int(input()) def mod_pow(x, n, mod): res = 1 while n > 0: # n=0 なら1を返す if n & 1: # i(=0,1,...)番目のビットが1なら res * x^(2^i) res = res * x % mod x = x**2 % mod # x = x^(2^(i+1)) n >>= 1 return res N%=6 ans=mod_pow(N,K,6) str="428571" print(str[ans])