n,k = map(int, [input(),input()]) nn = n % 6 kk = k % 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)