
#include <iostream>
#include <stack>

int main(){
    stack<int> rail;
    int buf;
    
    // repeat until "cin" fails.
    while (cin >> buf) {
        if (buf == 0) { // train goes out.
            cout << rail.top();
            rail.pop();
        }
        else{   // train comes in.
            rail.push(buf);
        }
    }
    
    return 0;
}