import java.io.*;

class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		int N=Integer.parseInt(br.readLine());
		StringBuilder sb=new StringBuilder();
		for(int i=1;i<=N;i++){
			int x=i;
			if(x%3==0||check3(i, Integer.toString(i).length())) sb.append(" "+i);
		}
		System.out.println(sb);
	}
	
	private static boolean check3(int n, int k){
		boolean res=false;
		int t=(int)Math.pow(10, k-1);
		if(n/t==3){
			res=true;
		}else if(k>1){
			res=check3(n%t, k-1);
		}
		return res;
	}
} import java.io.*; io.* java class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		int N=Integer.parseInt(br.readLine());
		StringBuilder sb=new StringBuilder();
		for(int i=1;i<=N;i++){
			int x=i;
			if(x%3==0||check3(i, Integer.toString(i).length())) sb.append(" "+i);
		}
		System.out.println(sb);
	}
	
	private static boolean check3(int n, int k){
		boolean res=false;
		int t=(int)Math.pow(10, k-1);
		if(n/t==3){
			res=true;
		}else if(k>1){
			res=check3(n%t, k-1);
		}
		return res;
	}
} Main public static void main(String[] args) throws Exception {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		int N=Integer.parseInt(br.readLine());
		StringBuilder sb=new StringBuilder();
		for(int i=1;i<=N;i++){
			int x=i;
			if(x%3==0||check3(i, Integer.toString(i).length())) sb.append(" "+i);
		}
		System.out.println(sb);
	} main {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		int N=Integer.parseInt(br.readLine());
		StringBuilder sb=new StringBuilder();
		for(int i=1;i<=N;i++){
			int x=i;
			if(x%3==0||check3(i, Integer.toString(i).length())) sb.append(" "+i);
		}
		System.out.println(sb);
	} BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) int N=Integer.parseInt(br.readLine()); N Integer.parseInt(br.readLine()) Integer.parseInt Integer br.readLine() br.readLine br StringBuilder sb=new StringBuilder(); sb new StringBuilder() for(int i=1;i<=N;i++){
			int x=i;
			if(x%3==0||check3(i, Integer.toString(i).length())) sb.append(" "+i);
		} int i=1; int i=1; i 1 i<=N i N i++ i++ i {
			int x=i;
			if(x%3==0||check3(i, Integer.toString(i).length())) sb.append(" "+i);
		} {
			int x=i;
			if(x%3==0||check3(i, Integer.toString(i).length())) sb.append(" "+i);
		} int x=i; x i if(x%3==0||check3(i, Integer.toString(i).length())) sb.append(" "+i); x%3==0||check3(i, Integer.toString(i).length()) x%3==0 x%3 x 3 0 check3(i, Integer.toString(i).length()) check3 i Integer.toString(i).length() Integer.toString(i).length Integer.toString(i) Integer.toString Integer i sb.append(" "+i) sb.append sb " "+i " " i System.out.println(sb) System.out.println System.out System System.out sb String[] args args private static boolean check3(int n, int k){
		boolean res=false;
		int t=(int)Math.pow(10, k-1);
		if(n/t==3){
			res=true;
		}else if(k>1){
			res=check3(n%t, k-1);
		}
		return res;
	} check3 {
		boolean res=false;
		int t=(int)Math.pow(10, k-1);
		if(n/t==3){
			res=true;
		}else if(k>1){
			res=check3(n%t, k-1);
		}
		return res;
	} boolean res=false; res false int t=(int)Math.pow(10, k-1); t (int)Math.pow(10, k-1) Math.pow Math 10 k-1 k 1 if(n/t==3){
			res=true;
		}else if(k>1){
			res=check3(n%t, k-1);
		} n/t==3 n/t n t 3 {
			res=true;
		} res=true res true if(k>1){
			res=check3(n%t, k-1);
		} k>1 k 1 {
			res=check3(n%t, k-1);
		} res=check3(n%t, k-1) res check3(n%t, k-1) check3 n%t n t k-1 k 1 return res; res int n n int k k
import java.io.*;

class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		int N=Integer.parseInt(br.readLine());
		StringBuilder sb=new StringBuilder();
		for(int i=1;i<=N;i++){
			int x=i;
			if(x%3==0||check3(i, Integer.toString(i).length())) sb.append(" "+i);
		}
		System.out.println(sb);
	}
	
	private static boolean check3(int n, int k){
		boolean res=false;
		int t=(int)Math.pow(10, k-1);
		if(n/t==3){
			res=true;
		}else if(k>1){
			res=check3(n%t, k-1);
		}
		return res;
	}
}