#include<stack>
#include<iostream>
using namespace std;
void main(){
	stack<int> train;
	int n;
	while(cin >> n){
		if(n != 0)
			train.push(n);
		else{
			cout << train.top() << endl;
			train.pop();
		}
	}
}