#include <iostream>
#include <string>
using namespace std;

class Deque{
	private:
	int *a = new int [100];
	int size_;
	int front_;
	int back_;
	int capacity;
	void resize(int);
	public:
	Deque(): size_(0), front_(1), back_(0), capacity(100){}
	void push_back(int);
	void push_front(int);
	void pop_back();
	void pop_front();
	int size() const;
	int back() const;
	int front() const;
	void clear();
};

void Deque::resize(int newcapacity){
	int* tmp = new int [newcapacity];
	int j = front_;
	for(int i = 0; j!= back_; ++i){
		tmp[i] = a[j];
		j = (j+1)%capacity;
	}
	tmp[size_ - 1] = a[back_];
	delete []a;
	a = tmp;
	capacity = newcapacity;
	front_ = 0;
	back_ = size_ - 1;
}

void Deque::push_back(int b){
	if(size_ == capacity){
		this -> resize(2*capacity);
	}
	back_ = (back_ + 1)%capacity;
	a[back_] = b;
	size_++;
}
void Deque::push_front(int b){
	if(size_ == capacity){
		this -> resize(2*capacity);
	}
	front_ = (front_ - 1 + capacity)%capacity;
	a[front_] = b;
	size_++;
}

void Deque::pop_back(){
	back_ = (back_ - 1 + capacity)%capacity;
	size_--;
}
void Deque::pop_front(){
	front_ = (front_ + 1 + capacity)%capacity;
	size_--;
}
int Deque::size() const{
	return size_;
}

int Deque::front() const{
	return a[front_];
}

int Deque::back() const{
	return a[back_];
}

void Deque::clear(){
	back_ = 0;
	front_ = 1;
	size_ = 0;
}
	
int main() {
	Deque a;
	string s;
	int n;
	while(cin >> s){
		if(s == "push_back"){
			cin >> n;
			a.push_back(n);
			cout << "ok\n";
		}
		else if(s == "push_front"){
			cin >> n;
			a.push_front(n);
			cout << "ok\n";
		}
		else if(s == "pop_back"){
			if(a.size()){
				cout << a.back() << endl;
				a.pop_back();
			}
			else{
				cout << "error\n";
			}
		}
		else if(s == "pop_front"){
			if(a.size()){
				cout << a.front() << endl;
				a.pop_front();
			}
			else{
				cout << "error\n";
			}
		}
		else if(s == "front"){
			if(a.size()){
				cout << a.front() << endl;
			}
			else{
				cout << "error\n";
			}
		}
		else if(s == "back"){
			if(a.size()){
				cout << a.back() << endl;
			}
			else{
				cout << "error\n";
			}
		}
		else if(s == "size"){
			cout << a.size() << endl;
		}
		else if(s == "clear"){
			cout << "ok\n";
			a.clear();
		}
		else if(s == "exit"){
			cout << "bye\n";
			return 0;
		}
	}
	return 0;
}