#include <iostream>
#include <stack>
using namespace std ;

int main()
{
	stack<int> train ;
	int i ;

	while( cin >> i )
	{
		if( i != 0 )
		{
			train.push(i) ;
		}
		else
		{
			cout << train.top() << "\n" ;
			train.pop() ;
		}
	}
	return 0 ;
}