n=int(input()) m=int(input()) n%=10 ans=[] cnt=1 for i in range(11): cnt*=n cnt%=10 if cnt not in ans: ans.append(cnt) else: l=ans.index(cnt) r=i if m<=l: print(ans[m-1]) else: b=m%(r-l) print(ans[b+l-1]) break