n = int(input())

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

   x = i

   if x % 3 == 0:

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

   else:

        while True:

            if x % 10 == 3:

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

                break

            x //= 10

            if x == 0:

                break

print()

n = int(input())

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

    if i % 3 == 0 or '3' in str(i):

        print(f' {i}', end='')

print()

n = int(input())
for i in range(1, n + 1):
    x = i
    if x % 3 == 0:
        print("", i, end="")
    else:
        while True:
            if x % 10 == 3:
                print("", i, end="")
                break
            x //= 10
            if x == 0:
                break
print()

n = int(input())
for i in range(1, n + 1):
    if i % 3 == 0 or "3" in str(i):
        print(f" {i}", end="")
print()

-    x = i -    if x % 3 == 0: -        print("", i, end="") -    else: -        while True: -            if x % 10 == 3: -                print("", i, end="") -                break -            x //= 10 -            if x == 0: -                break +    if i % 3 == 0 or "3" in str(i): +        print(f" {i}", end="")