include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

#define MAX 5

int main() {
    int n;
    if (scanf("%d", &n) != 1 || n <= 0) {
        printf("Invalid input");
        return 0;
    }

    int stack[MAX], top = -1;

    for (int i = 0; i < n; i++) {
        char command[20];
        if (scanf("%s", command) != 1) {
            printf("Invalid input");
            return 0;
        }

        if (strcmp(command, "push") == 0) {
            char numStr[20];
            if (scanf("%s", numStr) != 1) {
                printf("Invalid input");
                return 0;
            }

            // check if valid integer
            int len = strlen(numStr);
            int valid = 1, j = 0;
            if (numStr[0] == '-' && len > 1) j = 1;  // allow negative numbers
            for (; j < len; j++) {
                if (!isdigit(numStr[j])) {
                    valid = 0;
                    break;
                }
            }

            if (!valid) {
                printf("Invalid input");
                return 0;
            }

            int value = atoi(numStr);

            if (top == MAX - 1) {
                printf("Stack overflow\n");
            } else {
                stack[++top] = value;
            }

        } else if (strcmp(command, "peek") == 0) {
            if (top == -1) {
                printf("Stack is empty\n");
            } else {
                printf("%d\n", stack[top]);
            }
        } else {
            printf("Invalid input");
            return 0;
        }
    }

    return 0;
}