#include <iostream>
#include <stack>

using namespace std;

int main(){
	int com;
	stack<int> train;
	
	while(cin >> com){
		if(com != 0) train.push(com);
		else{
			cout << train.top() << endl;
			train.pop();
		}
	}
	
	return 0;
}