for i in range(1, int(input()) + 1):

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

        print("", i, end="")

print("")

print(("", *(i for i in range(3, int(eval(input())) + 1) if i % 3 == 0 or "3" in str(i))))

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

print(
    ("", *(i for i in range(3, int(eval(input())) + 1) if i % 3 == 0 or "3" in str(i)))
)

-for i in range(1, int(input()) + 1): -    if i % 3 == 0 or "3" in list(str(i)): -        print("", i, end="") -print("") +print( +    ("", *(i for i in range(3, int(eval(input())) + 1) if i % 3 == 0 or "3" in str(i))) +)