# If you wish you can import any modules from the standard library
# Do not use modules that are not in the standard library
import sys


def processArray(array):
    '''Modify this function to process `array` as indicated
    in the question. At the end, return the appropriate 
    value.

    Please create appropriate classes, and use appropriate
    data structures as necessary.

    Do not print anything in this function.

    Submit this entire program (not just this function)
    as your answer
    '''
    if len(array)==1:
    	return 1
    	
    if len(array)==0:
    	return 0
    
    start = 0
    i=0
    while i!=len(array):
    	if array[i]%3==0:
    		sum_accum = 0
    		while array[i]%3==0:
    			sum_accum = sum_accum + array[i]
    			i = i + 1
    		array[start] = sum_accum
    		sum_accum = 0
    	else:
    		array[start] = array[i]
    		i = i + 1
    	start = start + 1
    	
    		
    res = start + 1	
    sys.stdout.write(str(res))
    return res    # change this appropriately, if necessary

def run():
    array = []
    for line in sys.stdin:
        try:
            intval = int(line)
        except ValueError:
            # If line is not exactly one integer, ignore it
            pass
        if intval == 0:
            break
        array.append(intval)
    newlen = processArray(array)
    for i in range(newlen):
        print(array[i])

if __name__ == '__main__':
    run()