import sys

class Deque:
    def __init__(self):
        self.arr = list()
        
    def push_front(self, x):
        self.arr.insert(0, x)
    
    def push_back(self, x):
        self.arr.append(x)
    
    def pop_front(self):
        if(self.empty()):
            return -1
        else:
            return self.arr.pop(0)
        
    def pop_back(self):
        if(self.empty()):
            return -1
        else:
            return self.arr.pop()
        
    def front(self):
        if(self.empty()):
            return -1
        else:
            return self.arr[0]
        
    def back(self):
        if(self.empty()):
            return -1
        else:
            return self.arr[-1]
        
    def empty(self):
        return 1 if self.size() == 0 else 0
    
    def size(self):
        return len(self.arr)
    
N = int(sys.stdin.readline())
deque = Deque()

for _ in range(N):
    input_split = sys.stdin.readline().split()
    
    order = input_split[0]
    
    if(order == 'push_front'):
        deque.push_front(int(input_split[1]))
    elif(order == 'push_back'):
        deque.push_back(int(input_split[1]))
    elif(order == 'pop_front'):
        print(deque.pop_front())
    elif(order == 'pop_back'):
        print(deque.pop_back())
    elif(order == 'front'):
        print(deque.front())
    elif(order == 'back'):
        print(deque.back())
    elif(order == 'empty'):
        print(deque.empty())
    elif(order == 'size'):
        print(deque.size())

'# 코딩 문제 관련 > 파이썬' 카테고리의 다른 글

백준 5430번(python)  (0) 2019.06.25
백준 1021번(python)  (0) 2019.06.21
백준 11866번, 1158번(python)  (0) 2019.06.20
백준 1966번(python)  (0) 2019.06.20
백준 1260번(python)  (0) 2019.06.18