#include<stdio.h>
#include<string.h>
#include<stdlib.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 command[20];
    int x;
    while(scanf("%s",command)!=EOF){
        if(strcmp(command,"insertFront")==0){
            scanf("%d",&x);
            insertFront(x);
        }
        else if(strcmp(command,"insertLast")==0){
            scanf("%d",&x);
            insertLast(x);
        }
        else if(strcmp(command,"deleteFront")==0){
            deleteFront();
        }
        else if(strcmp(command,"deleteLast")==0){
            deleteLast();
        }
        else if(strcmp(command,"getFront")==0){
            getFront();
        }
        else if(strcmp(command,"getRear")==0){
            getRear();
        }
        else if(strcmp(command,"isEmpty")==0){
            printf(isEmpty() ?"true\n":false\n);
        }
        else if(strcmp(command,"isFull")==0){
            printf(isFull()?"true\n":"false\n");
        }
    }
    return 0;
}