#include <stack>
#include <iostream>

int main(){
  std::stack<int> train;
  int num;
  while(std::cin >> num){
    if(num != 0){
      train.push( num );
    }
    else {
      std::cout << train.top() << std::endl;
      train.pop();
    }
  }
  return 0;
}