#include <iostream>
#include <stack>

using namespace std;

int main(){

  int train;
  stack<int> s;

  while(cin >> train){
    if(train) s.push(train);
    else {
      cout << s.top() << endl;
      s.pop();
    }
  }

}