import java.util.Scanner;

class Main {
	public static void main(String[] args) throws Exception {
		int n = new Scanner(System.in).nextInt();
		int i=1;
		do{
			int x=i;
			if(x%3==0){
				System.out.print(" "+i);
			}else if(check3(i, Integer.toString(i).length())){
				System.out.print(" "+i);
			}
		}while(++i<=n);
		System.out.println("");
	}
	
	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.util.Scanner; Scanner java.util class Main {
	public static void main(String[] args) throws Exception {
		int n = new Scanner(System.in).nextInt();
		int i=1;
		do{
			int x=i;
			if(x%3==0){
				System.out.print(" "+i);
			}else if(check3(i, Integer.toString(i).length())){
				System.out.print(" "+i);
			}
		}while(++i<=n);
		System.out.println("");
	}
	
	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 {
		int n = new Scanner(System.in).nextInt();
		int i=1;
		do{
			int x=i;
			if(x%3==0){
				System.out.print(" "+i);
			}else if(check3(i, Integer.toString(i).length())){
				System.out.print(" "+i);
			}
		}while(++i<=n);
		System.out.println("");
	} main {
		int n = new Scanner(System.in).nextInt();
		int i=1;
		do{
			int x=i;
			if(x%3==0){
				System.out.print(" "+i);
			}else if(check3(i, Integer.toString(i).length())){
				System.out.print(" "+i);
			}
		}while(++i<=n);
		System.out.println("");
	} int n = new Scanner(System.in).nextInt(); n new Scanner(System.in).nextInt() new Scanner(System.in).nextInt new Scanner(System.in) int i=1; i 1 do{
			int x=i;
			if(x%3==0){
				System.out.print(" "+i);
			}else if(check3(i, Integer.toString(i).length())){
				System.out.print(" "+i);
			}
		}while(++i<=n); ++i<=n ++i i n {
			int x=i;
			if(x%3==0){
				System.out.print(" "+i);
			}else if(check3(i, Integer.toString(i).length())){
				System.out.print(" "+i);
			}
		} int x=i; x i if(x%3==0){
				System.out.print(" "+i);
			}else if(check3(i, Integer.toString(i).length())){
				System.out.print(" "+i);
			} x%3==0 x%3 x 3 0 {
				System.out.print(" "+i);
			} System.out.print(" "+i) System.out.print System.out System System.out " "+i " " i if(check3(i, Integer.toString(i).length())){
				System.out.print(" "+i);
			} check3(i, Integer.toString(i).length()) check3 i Integer.toString(i).length() Integer.toString(i).length Integer.toString(i) Integer.toString Integer i {
				System.out.print(" "+i);
			} System.out.print(" "+i) System.out.print System.out System System.out " "+i " " i System.out.println("") System.out.println System.out System System.out "" 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.util.Scanner;

class Main {
	public static void main(String[] args) throws Exception {
		int n = new Scanner(System.in).nextInt();
		int i=1;
		do{
			int x=i;
			if(x%3==0){
				System.out.print(" "+i);
			}else if(check3(i, Integer.toString(i).length())){
				System.out.print(" "+i);
			}
		}while(++i<=n);
		System.out.println("");
	}
	
	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;
	}
}