def func
  n = gets.to_i
  result = []
  1.upto n do |i|
    if i % 3 == 0
      result.push(i)
      next
    end
    if i.to_s.match /.*3.*/
      result.push(i)
      next
    end
  end

  puts ' ' + result.join(' ')
end

func
