# your code goes here #167 N^Mmod10 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 S[a]=c c+=1 # print (a) #rint (M) if M==0: print(a) else: M-=S[a] M%=c-S[a] while M>0: a*=N a%=10 M-=1 print (a)