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

int main(){
	stack<int> train;
	int n;
	
	while((scanf("%d", &n)) != EOF){
		if(n == 0){
			printf("%d\n", train.top());
			train.pop();
		}
		else{
			train.push(n);
		}
	}
	
	return 0;
}