N = int(input())
M = int(input())

if M == 0:
    print(1)
    quit()

ans = pow(N%10, M%4 if M%4 != 0 else 4)%10
print(ans)