table = [[0],[1],[2,4,8,6],[3,9,7,1],[4,6],[5],[6],[7,9,3,1],[8,4,2,6],[9,1]] n = gets.chomp m = gets.to_i idx = n[-1].to_i if m == 0 p 1 exit end case idx when 2,3,7,8 p table[idx][(m - 1) % 4] when 4,9 p table[idx][(m - 1) % 2] else p table[idx][0] end