
from collections import deque
import sys
input=sys.stdin.readline
num=int(input())
dq=deque()
def empty():
    if len(dq)!=0:
        return 0
    else:
        return 1
def size():
    return len(dq)
def front():
    if len(dq)!=0:
        print(dq[0])
    else:
        print(-1)
    
def back():
    if len(dq)!=0:
        print(dq[-1])
    else:
        print(-1)
        
for i in range(num):
    command=input().split()
    
    if command[0]=='push_front':
        dq.appendleft(command[1])
    elif command[0]=='push_back':
        dq.append(command[1])
        
    elif command[0]=='pop_front':
        if len(dq)!=0:
            print(dq[0])
            dq.popleft()
        else:
            print(-1)
    elif command[0]=='pop_back':
        if len(dq)!=0:
            print(dq[-1])
            dq.pop()
        else:
            print(-1)
            
    elif command[0]=='size':
        print(size())
        
    elif command[0]=='empty':
        print(empty())
        
    elif command[0]=='front':
        front()
        
    elif command[0]=='back':
        back()
접근 방법