#include <iostream>
#include <stack>

int main(){
	std::stack<int> switching_rail;
	int car_number;
	
	while(std::cin >> car_number){
		if(car_number == 0){
			std::cout << switching_rail.top();
			switching_rail.pop();
		}
		switching_rail.push(car_number);
	}
	
	return 0;
}