lis = [2,8,5,7,1,4] n = int(input()) k = int(input()) k %= 6 k += 6 print(lis[(n ** k)%6-1])