N = gets.to_i M = gets.to_i ans = if M == 0 1 else ((N % 10) ** ((M - 1) % 4 + 1)) % 10 end puts ans