#include<iostream>
#include<cstdio>
#include<stack>
using namespace std;
int main(){
  int car;
  stack<int> rail;
  while(cin >> car){
    if(car != 0){
      rail.push(car);
	}
    else{
      cout << rail.top();
      rail.pop();
    }
  }
}