import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner kb = new Scanner(System.in);
int n = kb.nextInt();
StringBuilder sb = new StringBuilder();
Deque<Integer> deque = new ArrayDeque<>();
for(int i=0; i<n; i++) {
String order = kb.next();
switch(order) {
case "push_front":
deque.offerFirst(kb.nextInt());
break;
case "push_back":
deque.offerLast(kb.nextInt());
break;
case "pop_front":
if(deque.isEmpty()) {
sb.append("-1").append('\n');
}else {
sb.append(deque.removeFirst()).append('\n');
}
break;
case "pop_back":
if(deque.isEmpty()) {
sb.append("-1").append('\n');
}else {
sb.append(deque.removeLast()).append('\n');
}
break;
case "size":
sb.append(deque.size()).append('\n');
break;
case "empty":
if(deque.isEmpty()) {
sb.append("1").append('\n');
}else {
sb.append("0").append('\n');
}
break;
case "front":
if(deque.isEmpty()) {
sb.append("-1").append('\n');
}else {
sb.append(deque.getFirst()).append('\n');
}
break;
case "back":
if(deque.isEmpty()) {
sb.append("-1").append('\n');
}else {
sb.append(deque.getLast()).append('\n');
}
break;
}
}
System.out.println(sb.toString());
}
}