N = int(input()) M = int(input()) zyunkai=[ [0], [1], [6,2,4,8], [1,3,9,7], [6,4], [5], [6], [1,7,9,3], [6,8,4,2], [1,9] ] Nmod10=N%10 if M==0: ans=1 else: ans=zyunkai[Nmod10][M%len(zyunkai[Nmod10])] print(ans)