#include <cstdio>
#include <stack>
using namespace std;

int main()
{
	stack<int> stk;
	int n,out;

	while(scanf("%d",&n) != EOF){
		if(n == 0){
			out = stk.top();
			stk.pop();
			printf("%d\n",out);
		}
		else
			stk.push(n);
	}

	return 0;
}