object Main extends App {
  val sc = new java.util.Scanner(System.in)
  val n = sc.nextInt()
  println(" " + (1 to n).filter(i => isMultipleOf3(i) || hasNumberOf3(i)).mkString(" "))

  def isMultipleOf3(i: Int): Boolean = i % 3 == 0

  def hasNumberOf3(i: Int): Boolean = {
    def loop(i: Int): Boolean = i match {
      case _ if i % 10 == 3 => true
      case _ if i / 10 == 0 => false
      case _ => loop(i / 10)
    }
    loop(i)
  }
}