#include <algorithm>                                                                    
#include <cstdlib>                                                                      
#include <iostream>                                                                     
#include <stack>                                                                        
                                                                                        
using namespace std;                                                                    
                                                                                        
int main() {                                                                            
  stack<int> cars;                                                                      
  const int kNumCars = 10;                                                              
  int num_entered_cars = 0;                                                             
                                                                                        
  do {
    int car;                                                                            
    cin >> car;                                                                         
    if (car == 0) {
      cout << cars.top() << endl;
      cars.pop();
    } else {
      ++num_entered_cars;
      cars.push(car);
    }
  } while (!cars.empty());

  return 0;
}