class Node
  attr_accessor :state, :depth
  attr_reader :parent

  def initialize(state, parent)
    @state = state
    @parent = parent
    @depth = parent.nil? ? 0 : parent.depth + 1
  end

  def goal?(limit, max)
    nums.size == limit && sum == max
  end

  def bad_end?(limit, max)
    nums.length > limit || sum > max
  end

  def nums
    parent.nil? ? [state] : parent.nums << state
  end

  def sum
    nums.inject(:+)
  end
end

def bfs
  while gets
    limit, max = $_.split.map(&:to_i)

    next if limit == 0 && max == 0

    goals = []
    10.times do |i|
      init_node = Node.new(i, nil)
      queue = [init_node]
      black_list = [init_node.nums.sort]

      loop do
        break if queue.empty?

        elem = queue.shift

        goals << elem if elem.goal?(limit, max)

        next if elem.bad_end?(limit, max)

        10.times do |j|
          next if elem.nums.include?(j)
          new_node = Node.new(j, elem)
          new_nums = new_node.nums.sort
          next if black_list.include?(new_nums)
          black_list << new_nums
          queue.unshift new_node
        end
      end
    end

    puts goals.map { |e| e.nums.sort }.uniq.size
  end
end

bfs