n = int(input()) k = int(input()) n%=6 k%=6 n = n**k n%=6 s = "428571" print(s[n])