def stack_data_structure():
    try:
        n = input().strip()
        if not n.isdigit():
            print("Invalid input")
            return
        n = int(n)
        if n < 1 or n > 1000:
            print("Invalid input")
            return
        
        MAX_SIZE = 5
        stack = [0] * MAX_SIZE  # fixed-size array
        top = -1  # top pointer
        
        for _ in range(n):
            command = input().strip()
            
            if command.startswith("push "):
                parts = command.split()
                if len(parts) != 2 or not (parts[1].lstrip('-').isdigit()):
                    print("Invalid input")
                    continue
                
                value = int(parts[1])
                
                if top >= MAX_SIZE - 1:
                    print("Stack overflow")
                else:
                    top += 1
                    stack[top] = value
            
            elif command == "peek":
                if top == -1:
                    print("Stack is empty")
                else:
                    print(stack[top])
            
            else:
                print("Invalid input")
    
    except:
        print("Invalid input")

# Run the stack simulation
stack_data_structure()