class Calc0167 def initialize(args) args = args.map { |l| l.chomp.split(/\s+/) } @n = args.shift.first @m = args.shift.first end TABLE = [ [0], [1], [6, 2, 4, 8], [1, 3, 9, 7], [6, 4], [5], [6], [1, 7, 9, 3], [6, 8, 4, 2], [1, 9] ] def run n = @n[-1..-1].to_i m = (@m.size == 1 ? @m : @m[-2..-1]).to_i if @m == '0' 1 else TABLE[n][m % TABLE[n].size] end end end puts Calc0167.new(STDIN.readlines).run if __FILE__ == $0