def call_filter(x):

    if x % 3 == 0: return True



    while x > 0:

        if x % 10 == 3: return True

        x = int(x/10)



    return False



def call(n):

    for i in [x for x in range(1, n+1) if call_filter(x)]:

        print(' {}'.format(i), end='')

    print()



n = int(input())

call(n)



def calc(n):

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

        if i % 3 == 0:

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

            continue



        x = i



        while x > 0:

            if x % 10 == 3:

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

                break



            x = x // 10



    print()



calc(int(input()))



def call_filter(x):
    if x % 3 == 0:
        return True
    while x > 0:
        if x % 10 == 3:
            return True
        x = int(x / 10)
    return False


def call(n):
    for i in [x for x in range(1, n + 1) if call_filter(x)]:
        print(" {}".format(i), end="")
    print()


n = int(input())
call(n)

def calc(n):
    for i in range(1, n + 1):
        if i % 3 == 0:
            print("", i, end="")
            continue
        x = i
        while x > 0:
            if x % 10 == 3:
                print("", i, end="")
                break
            x = x // 10
    print()


calc(int(input()))

-def call_filter(x): -    if x % 3 == 0: -        return True -    while x > 0: -        if x % 10 == 3: -            return True -        x = int(x / 10) -    return False - - -def call(n): -    for i in [x for x in range(1, n + 1) if call_filter(x)]: -        print(" {}".format(i), end="") +def calc(n): +    for i in range(1, n + 1): +        if i % 3 == 0: +            print("", i, end="") +            continue +        x = i +        while x > 0: +            if x % 10 == 3: +                print("", i, end="") +                break +            x = x // 10 -n = int(input()) -call(n) +calc(int(input()))