#include <iostream>
#include <stack>
using namespace std;
 
int main() {
	stack<int> st;
    int train;
    while(cin >> train){
        if(train) st.push(train);
        else{
            cout << st.top() << endl;
            st.pop();
        }
    }
	return 0;
}