#include <iostream>
#include <stack>


using namespace std;

int main()
{
    stack<int> railroad;
    int num;
    while(cin >> num) {
        if(num == 0) {
            cout << railroad.top() << endl;
            railroad.pop();
        } else {
            railroad.push(num);
        }
    }
    return 0;
}