#include <iostream>
#include <stack>
int main()
{
  int n;
  std::stack<int> trains;
  while (std::cin >> n) {
    if (n == 0) {
      std::cout << trains.top() << std::endl;
      trains.pop();
    } else {
      trains.push(n);
    }
  }
  return 0;
}