#include<iostream>
#include<stack>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)

using namespace std;


int main(){
	stack<int> rail;
	int num;

	while (cin >> num){
		if (num == 0){
			cout << rail.top() << endl;
			rail.pop();
		}
		else{
			rail.push(num);
		}
	}


}