#include <iostream>
using namespace std;

struct tCell{
	int value;
	tCell* prev;
	tCell* next;
};

class List{
public:
	List() : mQ( 0 ), head( NULL ), tail( NULL ){
		tail = new tCell;
		head = new tCell;
		tail->value = head->value = 0;
		head->prev = tail->next = NULL;
		head->next = tail;
		tail->prev = head;
	}
	~List(){
		for ( int i = mQ; i > 0; --i ){
			tCell* cell = tail->prev;
			tail->prev = cell->prev;
			delete cell;
		}
		if ( !head ) delete head;
		if ( !tail ) delete tail;
	};

	void push( int a ){
		++mQ;
		tCell* cell = new tCell;
		cell->value = a;
		cell->next = tail;
		cell->prev = tail->prev;
		tail->prev->next = cell;
		tail->prev = cell;
	}
	
	bool popStack( int& a ){
		if ( mQ == 0 ) return false;
		--mQ;
		a = tail->prev->value;
		tCell* cell = tail->prev;
		cell->prev->next = cell->next;
		cell->next->prev = cell->prev;
		delete cell;
		return true;
	}
	
	bool popQueue( int& a ){
		if ( mQ == 0 ) return false;
		--mQ;
		a = head->next->value;
		tCell* cell = head->next;
		cell->next->prev = cell->prev;
		cell->prev->next = cell->next;
		delete cell;
		return true;
	}

	int mQ;
	tCell *head, *tail;
private:
};

int main(){
	List list1;
	List list2;
	int a;
	
	while ( cin >> a ){
		list1.push( a );
	}

	while ( list1.popQueue( a ) ){
		if ( a ){
			list2.push( a );
		}else{
			list2.popStack( a );
			cout << a << endl;
		}
	}

	return 0;
}