#include <bits/stdc++.h>
using namespace std;

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