#include <iostream>
#include <functional>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <list>
#include <stack>
#include <queue>
using namespace std;
deque<int> my_deque;
vector<int> my_vec;
void Push_front(int _value)
{
my_deque.push_front(_value);
}
void Push_back(int _value)
{
my_deque.push_back(_value);
}
void Pop_front()
{
if (my_deque.empty())
{
my_vec.push_back(-1);
return;
}
my_vec.push_back(my_deque.front());
my_deque.pop_front();
}
void Pop_back()
{
if (my_deque.empty())
{
my_vec.push_back(-1);
return;
}
my_vec.push_back(my_deque.back());
my_deque.pop_back();
}
void Size()
{
my_vec.push_back(my_deque.size());
}
void Empty()
{
if (my_deque.empty())
my_vec.push_back(1);
else
my_vec.push_back(0);
}
void Front()
{
if (my_deque.empty())
my_vec.push_back(-1);
else
my_vec.push_back(my_deque.front());
}
void Back()
{
if (my_deque.empty())
my_vec.push_back(-1);
else
my_vec.push_back(my_deque.back());
}
int main()
{
string menu = "";
int num = 0;
cin >> num;
while(num--)
{
cin >> menu;
if (menu == "pop_front")
Pop_front();
else if (menu == "pop_back")
Pop_back();
else if (menu == "front")
Front();
else if (menu == "back")
Back();
else if (menu == "push_front")
{
int value = 0;
cin >> value;
Push_front(value);
}
else if (menu == "push_back")
{
int value = 0;
cin >> value;
Push_back(value);
}
else if (menu == "empty")
Empty();
else if (menu == "size")
Size();
}
for (const auto& item : my_vec)
cout << item << endl;
return 0;
}