#include<iostream>
#include<stack>

using namespace std;

int main(){
	stack <int> depot;

	int train;

	while (cin >> train){
		if (train != 0) depot.push(train);
		else {
			cout << depot.top() << endl;
			depot.pop();
		}
	}

	return 0;

}