let call n =
  let rec check_num i =
    let x = i in
    if x mod 3 = 0 then
      begin
        print_string " ";
        print_int i;
      end
    else
      begin
        let rec include_3 x i =
          if x mod 10 = 3 then
            begin
              print_string " ";
              print_int i
            end
          else
            begin
              let x' = x /10 in
              if x' != 0 then include_3 x' i
            end
        in
        include_3 x i;
      end
  in
  let rec loop i =
    let i' = i + 1 in
    if i' <= n then
      begin
        check_num i';
        loop i'
      end
  in
  loop 1;
  print_string "\n"
;;
let () =
  Scanf.sscanf (read_line()) "%d" (fun n -> call n)
;;
