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