T = 6 N = list(map(int,list(input()))) K = list(map(int,list(input()))) A = [4,2,8,5,7,1] a = N[0]%T for i in range(1,len(N)): a = (a*10+N[i])%T if a==0: print(4) elif a==1: print(2) else: T1 = 1 cur = a while True: b = (cur*a)%T if b==a: break T1 += 1 cur = b if T1==1: print(A[a]) else: a1 = K[0]%T1 for i in range(1,len(K)): a1 = (a1*10+K[i])%T1 if a1==0: a1 = T1 ind = pow(a,a1,T) print(A[ind])