let call n =
  let rec check_num i =
    if i <> 1 && i > n then print_newline ()
    else
    if i mod 3 = 0 then begin
      Printf.printf " %d" i; check_num (i+1)
    end
    else
      let rec include3 j =
        if j mod 10 = 3 then begin
          Printf.printf " %d" i; check_num (i+1)
        end
        else
          let k = j/10 in
          if k > 0 then include3 k else check_num (i+1)
      in include3 i
  in check_num 1;;
let () = Scanf.scanf "%d\n" (fun n -> call n);;
