n = int(eval(input()))

list_a = [i for i in range(1,n+1) if i % 3 ==0]

list_b = []

for i in range (1,n+1):

    j = i

    while True:

        if j == 0:

            break

        elif j % 10 == 3:

            list_b.append(i)

            break

        j = j // 10

    

a = set(list_a)

b = set(list_b)

c =list(a | b)

c.sort()

print((" " + " ".join(map(str,c))))
n = int(input())

for i in range(1,n+1):

    if i%3 == 0 or i%10 ==3 or (i//10)%10 ==3 or (i//100)%10 ==3 or (i//1000)%10 ==3 :

        print(' '+str(i),end = '')

print()

n = int(eval(input()))
list_a = [i for i in range(1, n + 1) if i % 3 == 0]
list_b = []
for i in range(1, n + 1):
    j = i
    while True:
        if j == 0:
            break
        elif j % 10 == 3:
            list_b.append(i)
            break
        j = j // 10
a = set(list_a)
b = set(list_b)
c = list(a | b)
c.sort()
print((" " + " ".join(map(str, c))))

n = int(input())
for i in range(1, n + 1):
    if (
        i % 3 == 0
        or i % 10 == 3
        or (i // 10) % 10 == 3
        or (i // 100) % 10 == 3
        or (i // 1000) % 10 == 3
    ):
        print(" " + str(i), end="")
print()

-n = int(eval(input())) -list_a = [i for i in range(1, n + 1) if i % 3 == 0] -list_b = [] +n = int(input()) -    j = i -    while True: -        if j == 0: -            break -        elif j % 10 == 3: -            list_b.append(i) -            break -        j = j // 10 -a = set(list_a) -b = set(list_b) -c = list(a | b) -c.sort() -print((" " + " ".join(map(str, c)))) +    if ( +        i % 3 == 0 +        or i % 10 == 3 +        or (i // 10) % 10 == 3 +        or (i // 100) % 10 == 3 +        or (i // 1000) % 10 == 3 +    ): +        print(" " + str(i), end="") +print()