class Node
  attr_accessor :index, :list, :double

  def initialize(num_list)
    @list = num_list.dup
    @index = 0
    @double = false
  end

  def check
    @list.all?(&:zero?) && @double
  end

  def initialize_copy(orig)
    @list = orig.list.dup
    @double = orig.double
    @index = orig.index
  end

  def is_valid?
    !@list.find{|e| e > 4 }
  end
end

class Solver
  def initialize

    loop do
      number = gets

      break if number.nil?

      answer = []

      1.upto(9) do |num|
        if solver(number.chomp, num)
          answer << num
        end
      end

      puts answer.empty? ? 0 : answer.join(' ')
    end
  end

  def queue(list)
    res = Array.new(9, 0)

    list.each do |num|
      res[num-1] += 1
    end

    res
  end

  def solver(number, num)
    list = number.chars.map(&:to_i) + [num]

    num_list = queue(list)

    que = [Node.new(num_list)]

    while !que.empty?
      node = que.shift

      next unless node.is_valid?

      index = node.index

      return true if node.check
      next if index >= 9 || node.list[index] < 0

      if node.list[index] >= 3
        n = node.dup
        n.list[index] -= 3

        n.index += 1 if n.list[index].zero?
        que.push(n)
      end

      if node.list[index] >= 2 && !node.double
        n = node.dup
        n.list[index] -= 2
        n.double = true

        n.index += 1 if n.list[index].zero?
        que.push(n)
      end

      if node.list[index] >= 1 && node.index < 7
        n = node.dup
        n.list[index] -= 1
        n.list[index+1] -= 1
        n.list[index+2] -= 1

        n.index += 1 if n.list[index].zero?
        que.push(n)
      end

      if node.list[index].zero?
        n = node.dup
        n.index += 1
        que.push(n)
      end
    end

    false
  end
end

Solver.new