n, m= int(input()), int(input()) print(pow(n%10,(m%4 if m%4 else 1))%10)