#include<stdio.h>
#include<stdlib.h>
#define max 5

int stack[100];
int top = -1;

void push(int value){
    if(top == max-1){
        printf("Invalid input");
    }
    else{
        stack[++top] = value;
        printf("%d ",top);
    }
}

void pop(){
    if(top == -1){
        printf("Invalid input \n");
    }
    else{
        stack[top--];
        //printf("\n");
    }
}

void peek(){
    if(top == -1){
        printf("Invalid input");
    }
    else{
           
        printf("%d\n",stack[top]);
    }
}

void display(){
    if(top == -1){
        printf("Invalid input");
    }
    else{
        for(int i=0; i<=top; i++){
            printf("%d",stack[value]);
        }
        printf("\n");
    }
} 


int main(){
    int choice, value;

    while(1){
        
        scanf("%d",&choice);
        switch(choice){
            
            case 1:
              scanf("%d",&value);
                push(value);
                break;
            
            case 2:
                pop();
                break;
                
            case 3:
                display();
                break;
            
            case 4:
            
                peek();
                break;
            
            case 5:
                exit(0);
            
            default:
                printf("Invalid input");
        }
    }
    return 0;
}