n,k = map(int, [input(),input()]) while n > 6: n = n - 6 #n ** k % 6 = (n - 6) ** k % 6 while k >= 3: k = k - 2 #n ** k % 6 = n ** (k - 2) % 6 x = n ** k if x % 6 ==1: b = 2 elif x % 6 == 2: b = 8 elif x % 6 == 3: b = 5 elif x % 6 == 4: b = 7 elif x % 6 == 5: b = 1 else: b = 4 print(b)