#include<bits/stdc++.h>
using namespace std;
int main(){
  int train;
  stack<int> stay;
  while(cin>>train){
    if(train==0){
      cout<<stay.top()<<endl;
      stay.pop();
    }else
    stay.push(train);
  }
  return 0;
}