let is_mod_3 n =
  (n mod 3) = 0

let rec is_include_3 n =
  if n = 0 then false
  else if (n mod 10) = 3 then true
  else is_include_3 (n / 10)

let () =
  let lim = read_int () in
  let rec loop i cont =
    if i > lim then cont []
    else if is_mod_3 i || is_include_3 i then loop (i+1) (fun x -> cont (i::x))
    else loop (i+1) cont in
  let l = loop 3 (fun x -> x) in
  List.iter (fun n -> print_string (Printf.sprintf " %d" n)) l;
  print_newline ()