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

int main(){
	stack<int> train;
	int a;
	while(cin >> a){
		if(a){
			train.push(a);
		}else{
			cout << train.top() << endl;
			train.pop();
		}
	}
}