n = int(input())%6 k = int(input()) num = '285714' ans = num[pow(n,k,6) - 1] print(ans)