N=int(input()) M=int(input()) a=1 N%=10 c=0 T=[0]*10 S=[0]*10 while M>=0 and T[a]<2: a*=N a%=10 M-=1 T[a]+=1 c+=1 if S[a]==0: S[a]=c # print (a) #rint (M) if M==0: print(a) else: M+=c-S[a]+1 # print (M) M%=c-S[a]+1 # print (M) while M>0: a*=N a%=10 M-=1 print (a)