#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#define MAX_SIZE 5
#define MAX_LEN 50

typedef struct {
    int arr[MAX_SIZE];
    int top;
} Stack;;
void init_stack(Stack *s){
    s->top = -1;
}
int is_full(Stack *s) {
    return s->top == MAX_SIZE - 1;
}
int is_empty(Stack *s) {
    return s->top == -1;
}
int push(Stack *s, int val){
    if (is_full(s)){
        return 0;
    }
    s->arr[++(s->top)] = val;
    return 1;
}
int peek(Stack *s,int *val){
    if(is_empty(s)){
        return 0;
    }
    *val = s->arr[s->top];
    return 1;
}
int is_valid_integer(char *str) {
    int i = 0;
    if (str[0] ==  '-') i = 1;
    if (str[i] == '\0') return 0;
    for (; str[i] != '\0'; i++){
        if(!isdigit(str[i])) return 0;
    }
    return 1;
}
int main(){
    int n;
    if(scanf("%d\n", &n) != 1){
        printf("Invalid input\n");
        return 0;
    }
    Stack stack;
    init_stack(&stack);
    char line[MAX_LEN];
    for (int i = 0;i < n; i++){
        if (!fgets(line, sizeof(line),stdin)) {
            printf("Invalid input\n");
            return 0;
        }
        line[strcspn(line, "\n")] = '\0';
        char command[10], value_str[MAX_LEN];
        int parts = sscanf(line, "%s %s",command, value_str);
        if(strcmp(command, "push") == 0) {
            if(parts != 2 || is_valid_integer(value_str)){
                printf("Invalid input\n");
                return 0;
            }
            int val = atoi(value_str);
            if(!push(&stack, val)) {
                printf("Stack overflow\n");
            }
        }
        else if (strcmp(command, "peek") == 0){
            if(parts != 1) {
                printf("Invalid input\n");
                return 0;
            }
            int top_val;
            if (peek(&stack, &top_val)){
                printf("%d\n",top_val);
            } else {
                printf("Stack is empty\n");
            }
        }
        else{
            printf("Invalid input\n");
            return 0;
}