n,k = map(int, [input(),input()]) x = n % 6 y = n ** k if y > 6: 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 else: if y == 1: b = 2 elif y == 2: b = 8 elif y == 3: b = 5 elif y == 4: b = 7 elif y == 5: b = 1 else: b = 4 print(b)