#include<iostream>
#include<cstdio>
#include<stack>//?????????????????¢??°

using namespace std;

int main(void)
{
	stack<int>cars;
	int num;
	
	while(cin >> num){
		if(num){
		cars.push(num);
		}else{
		cout << cars.top() << endl;
		cars.pop();
		}
	}
	return 0;
}