n,k = map(int, [input(),input()]) nn = n % 6 kk = k % 2 if kk == 0: kk = 2 x = nn ** kk if nn > 0: 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: nn = nn + 6 x = nn ** kk 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) #2/7 → 0.285714