#include <iostream>
#include <stack>
#include <vector>

using namespace std;

int main(int argc, char const *argv[]){
    int input; 
    stack<int> train_stack;
    vector<int> order;

    cin>>input;
    train_stack.push(input);
    while (!train_stack.empty()){
        cin>>input;
        if (input==0){
            cout<<train_stack.top()<<endl;
            train_stack.pop();
        }
        else{
            train_stack.push(input);
        }
    }

    return 0;
}