#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

int main (int argc, char * const argv[]) {
    // insert code here...
    int n;
	vector<int> densha;
	
	while(cin>>n){
		if(n==0){
			cout << densha[densha.size()-1]<<endl;
			densha.pop_back();
		}else{
			densha.push_back(n);
		}
	}
	
	return 0;
}