#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <functional>

using namespace std;

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

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

	return 0;
}