#include <iostream>
#include <algorithm>
#include <vector>
#include <functional>
#include <sstream>
#include <cstdio>
#include <stack>
using namespace std;

// Switching Railroad Cars(0013)
int main()
{
	int n;
	stack<int> train;
	while (cin >> n) {
		if (n == 0) {
			cout << train.top() << endl;
			train.pop();
		} else {
			train.push(n);
		}
	}
	return 0;
}