#include <cstdio>
#include <iostream>

using namespace std;

const int N = 1000000;

int stack[N], top = 1;

int main() {
	int n;
	top = -1;
	scanf ("%d", &stack[++top]);
	while (top >= 0) {
		scanf ("%d", &n);
		if (n) stack[++top] = n;
		else printf ("%d\n", stack[top--]);
	}
	return 0;
}