N = int(input()) K = int(input()) n = 1 if K == 0 else N for _ in range(K-1): n = (n * N) % 6 print('285714'[n-1])