#include<stdio.h>
#include<string.h>
#include<stdbool.h>
#define MAX 1000
int deque[MAX];
int front=-1,rear=-1;
int capacity;
bool isEmpty(){
    return front==-1;
}
bool isFull(){
    return (front==0; && rear==capacity-1) || (front==rear+1);
}
void insertFront(int x){
    if(isFull())return;
    if(isEmpty()){
        front=rear=0;
    }
    else if(front==0){
        front=capacity-1;
    }
    else{
        front--;
    }
    deque[front]=x;
}
void insertLast(int x){
    if(isFull())return;
    if(isEmpty()){
        front=rear=0;
    }
    else if(rear==capacity-1){
        rear=0;
    }
    else{
        rear++;
    }
    deque[rear]=x;
    }
    void deleteFront(){
        if(isEmpty()){
            printf("-1\n");
            return;
        }
        printf("%d\n",deque[front]);
        if(front == rear){
            front=rear=-1;
        }
        else if(front==capacity-1){
            front=0;
        }
        else{
            front++;
        }
        }
        void deleteLast(){
            if(isEmpty()){
                printf("-1\n");
                return;
            }
            printf("%d\n",deque[rear]);
            if(front==rear){
                front=rear=-1;
            }
            else if(rear==0){
                rear=capacity-1;
            }
            else{
                rear--;
            }
        }
        void getFront(){
            if(isEmpty()){
                printf("-1\n");
            }
            else{
                printf("%d\n",deque[front]);
        }
    }
    void getRear(){
        if(isEmpty()){
            printf("-1\n");
        }
        else{
            printf("%d\n",deque[rear]);
        }
    }
    int main(){
        scanf("%d",&capacity);
        char op[20];
        int x;
        while(scanf("%s",op) != EOF){
            if(strcmp(op,"inserFront")==0){
                scanf("%d",&x);
                insertFront(x);
            }
            else if(strcmp(op,"insertLast")==0){
                scanf("%d",&x);
                insertLast(x);
            }
            else if(strcmp(op,"deleteFront")==0){
                deleteFront();
            }
            else if(strcmp(op,"deleteLast")==0){
                deleteLast();
            }
            else if(strcmp(op,"getEmpty")==0){
                getFront();
            }
            else if(strcmp(op,"getRear")==0){
                getRear();
            }
            else if(strcmp(op,"isEmpty")==0){
                printf(isEmpty() ? "true\n":"false\n");
            }
            else if(strcmp(op,"isFull")==0{
                printf(isFull() ? "true\n":"false\n");
            }
        }
        return 0;
    }