n = gets.to_i m = gets.to_i ary=[] (1..4).each do |i| ary.push (n**i)%10 end ans = ary[(m % 4)] puts ans puts "\n"