#include <iostream>
#include <stack>

int main(){
	std::stack<int> trains;

	do{
		int in;
		std::cin >> in;

		if(in == 0){
			std::cout << trains.top() << std::endl;
			trains.pop();
		}
		else
			trains.push(in);
	}while(!trains.empty());
	return 0;
}