/**
 * @copyright (c) 2020 Daisuke Hashimoto
 */


#include <cstdint>
#include <iostream>
#include <string>
#include <algorithm>


constexpr int32_t kMaxNumberOfNodes = 1000000;
constexpr int32_t kInvalidIndex = -1;
constexpr int32_t kTopIndex = -2;
constexpr int32_t kTailIndex = -3;

class Key {
 public:
  Key() noexcept;
  explicit Key(const int32_t key) noexcept;
  ~Key() noexcept;
  Key(const Key &obj) noexcept;
  Key &operator=(const Key &rhs) noexcept;
  Key(Key &&obj) noexcept;
  Key &operator=(Key &&rhs) noexcept;
  bool Equals(const Key &obj) const noexcept;
  int32_t GetKey() const noexcept;
  void PrintKey() const noexcept;
  bool IsValid() const noexcept;

 private:
  int32_t key_;
};

class Node {
 public:
  Node() noexcept;
  ~Node() noexcept;
  void SetKey(const Key &key) noexcept;
  void UpdateNext(int32_t next) noexcept;
  void UpdatePrevious(int32_t previous) noexcept;
  bool IsValid() const noexcept;
  Key GetKey() const noexcept;
  void PrintKey() const noexcept;
  bool HasKey(const Key &key) const noexcept;
  int32_t GetNext() const noexcept;
  int32_t GetPrevious() const noexcept;

 private:
  Node(const Node &obj) noexcept = delete;
  Node &operator=(const Node &obj) noexcept = delete;
  Node(Node &&obj) = delete;
  Node &operator=(Node &&obj) = delete;

 private:
  int32_t next_;
  int32_t previous_;
  Key key_;
};

class DoublyLinkedList {
 public:
  DoublyLinkedList() noexcept;
  ~DoublyLinkedList() noexcept;
  void Insert(const Key &key);
  void Delete(const Key &key);
  void DeleteFirst();
  void DeleteLast();
  void Print() const;

 private:
  DoublyLinkedList(const DoublyLinkedList &obj) noexcept = delete;
  DoublyLinkedList &operator=(const DoublyLinkedList &obj) noexcept = delete;
  DoublyLinkedList(DoublyLinkedList &&obj) = delete;
  DoublyLinkedList &operator=(DoublyLinkedList &&obj) = delete;

  int32_t FindInvalidNode();
  int32_t Search(const Key &key);

 private:
  int32_t top_;
  int32_t tail_;
  Node nodes_[kMaxNumberOfNodes];
};

void CallDoublyLinkedList(std::istream &input_stream);


Key::Key() noexcept : key_(INT16_MAX) {}

Key::Key(const int32_t key) noexcept : key_(key) {}

Key::~Key() noexcept {}

Key::Key(const Key &obj) noexcept : key_(obj.key_) {}

Key &Key::operator=(const Key &rhs) noexcept {
  if (this != &rhs) {
    this->key_ = rhs.key_;
  }
  return *this;
}

Key::Key(Key &&obj) noexcept : key_(obj.key_) {
  obj.key_ = INT16_MAX;  // 無効化
}

Key &Key::operator=(Key &&rhs) noexcept {
  if (this != &rhs) {
    this->key_ = rhs.key_;
    rhs.key_ = INT16_MAX;  // 無効化
  }
  return *this;
}

int32_t Key::GetKey() const noexcept {
  return key_;
}

void Key::PrintKey() const noexcept {
  printf("%d", key_);
}

bool Key::Equals(const Key &obj) const noexcept {
  return (IsValid() && (GetKey() == obj.GetKey()));
}

bool Key::IsValid() const noexcept {
  return (key_ != INT16_MAX);
}

// ****************************************************
Node::Node() noexcept : next_(kInvalidIndex), previous_(kInvalidIndex), key_{} {}
Node::~Node() noexcept {}

void Node::SetKey(const Key &key) noexcept {
  key_ = key;
}

void Node::UpdateNext(int32_t next) noexcept {
  next_ = next;
}

void Node::UpdatePrevious(int32_t previous) noexcept {
  previous_ = previous;
}

bool Node::IsValid() const noexcept {
  return (next_ != kInvalidIndex) && (previous_ != kInvalidIndex);
}

Key Node::GetKey() const noexcept {
  return key_;
}

void Node::PrintKey() const noexcept {
  key_.PrintKey();
}

bool Node::HasKey(const Key &key) const noexcept {
  return (IsValid() && key_.Equals(key));
}

int32_t Node::GetNext() const noexcept {
  return next_;
}
int32_t Node::GetPrevious() const noexcept {
  return previous_;
}

// ****************************************************

DoublyLinkedList::DoublyLinkedList() noexcept : top_(kInvalidIndex), tail_(kInvalidIndex), nodes_() {}

DoublyLinkedList::~DoublyLinkedList() noexcept {}

void DoublyLinkedList::Insert(const Key &key) {
  try {
    const int32_t index_stored = FindInvalidNode();
    nodes_[index_stored].SetKey(key);
    nodes_[index_stored].UpdatePrevious(kTopIndex);
    if (top_ == kInvalidIndex) {
      nodes_[index_stored].UpdateNext(kTailIndex);
      tail_ = index_stored;
    } else {
      const int32_t old_top = top_;
      nodes_[index_stored].UpdateNext(old_top);
      nodes_[old_top].UpdatePrevious(index_stored);
    }
    top_ = index_stored;
  } catch (...) {
    throw;
  }
}

void DoublyLinkedList::Delete(const Key &key) {
  try {
    const int32_t index_delete = Search(key);
    const int32_t right = nodes_[index_delete].GetNext();
    const int32_t left = nodes_[index_delete].GetPrevious();
    if ((index_delete == top_) && (index_delete == tail_)) {
      // 左右が存在しない
      top_ = kInvalidIndex;
      tail_ = kInvalidIndex;
    } else if (index_delete == top_) {
      // 右は存在するが、左が存在しない
      top_ = right;
      nodes_[right].UpdatePrevious(kTopIndex);
    } else if (index_delete == tail_) {
      // 左は存在するが、右が存在しない
      nodes_[left].UpdateNext(kTailIndex);
      tail_ = left;
    } else {
      // 左右共に存在する
      nodes_[left].UpdateNext(right);
      nodes_[right].UpdatePrevious(left);
    }
    nodes_[index_delete].UpdatePrevious(kInvalidIndex);
    nodes_[index_delete].UpdateNext(kInvalidIndex);
  } catch (...) {
    // DO NOTHING
  }
}

void DoublyLinkedList::DeleteFirst() {
  try {
    const int32_t index_delete = top_;
    if (index_delete == tail_) {
      // 左右が存在しない
      top_ = kInvalidIndex;
      tail_ = kInvalidIndex;
    } else {
      // 右は存在するが、左が存在しない
      const int32_t right = nodes_[index_delete].GetNext();
      top_ = right;
      nodes_[right].UpdatePrevious(kTopIndex);
    }
    nodes_[index_delete].UpdatePrevious(kInvalidIndex);
    nodes_[index_delete].UpdateNext(kInvalidIndex);
  } catch (...) {
    throw;
  }
}
void DoublyLinkedList::DeleteLast() {
  try {
    const int32_t index_delete = tail_;
    if (index_delete == top_) {
      // 左右が存在しない
      top_ = kInvalidIndex;
      tail_ = kInvalidIndex;
    } else {
      // 左は存在するが、右が存在しない
      const int32_t left = nodes_[index_delete].GetPrevious();
      nodes_[left].UpdateNext(kTailIndex);
      tail_ = left;
    }
    nodes_[index_delete].UpdatePrevious(kInvalidIndex);
    nodes_[index_delete].UpdateNext(kInvalidIndex);
  } catch (...) {
    throw;
  }
}

int32_t DoublyLinkedList::FindInvalidNode() {
  int32_t found_index = -1;
  for (int32_t index = std::max(0, top_); index < kMaxNumberOfNodes; ++index) {
    if (!nodes_[index].IsValid()) {
      found_index = index;
      break;
    }
  }
  if (found_index < 0) {
    std::cout << "ERROR: FindInvalidNode(): not found." << std::endl;
    throw 1;
  }
  return found_index;
}

int32_t DoublyLinkedList::Search(const Key &key) {
  int32_t found_index = -1;
  int32_t current_index = top_;
  for (int32_t count = 1; count <= kMaxNumberOfNodes; ++count) {
    if ((current_index == kTailIndex) || (current_index == kInvalidIndex)) {
      break;
    } else if (nodes_[current_index].HasKey(key)) {
      found_index = current_index;
      break;
    } else {
      current_index = nodes_[current_index].GetNext();
    }
  }
  if (found_index < 0) {
    throw 1;
  }
  return found_index;
}

void DoublyLinkedList::Print() const {
  int32_t current_index = top_;
  for (int32_t count = 1; count <= kMaxNumberOfNodes; ++count) {
    if ((current_index == kTailIndex) || (current_index == kInvalidIndex)) {
      break;
    } else {
      nodes_[current_index].PrintKey();
      if (nodes_[current_index].GetNext() == kTailIndex) {
        printf("\n");
      } else {
        printf(" ");
      }
      current_index = nodes_[current_index].GetNext();
    }
  }
}

void CallDoublyLinkedList(std::istream &input_stream) {
  input_stream.tie(0);
  std::ios::sync_with_stdio(false);
  int32_t number_of_operations;
  input_stream >> number_of_operations;
  DoublyLinkedList *linked_list = new DoublyLinkedList();
  try {
    for (int32_t operation_index = 0; operation_index < number_of_operations; ++operation_index) {
      std::string operation_name;
      int32_t key;
      input_stream >> operation_name;
      if (operation_name == "insert") {
        input_stream >> key;
        linked_list->Insert(Key(key));
      } else if (operation_name == "delete") {
        input_stream >> key;
        linked_list->Delete(Key(key));
      } else if (operation_name == "deleteFirst") {
        linked_list->DeleteFirst();
      } else if (operation_name == "deleteLast") {
        linked_list->DeleteLast();
      } else {
        std::cout << "Invalid operation name" << std::endl;
        throw 1;
      }
    }
    linked_list->Print();
  } catch (...) {
    delete linked_list;
    std::cout << "ERROR" << std::endl;
  }
  delete linked_list;
}

int main(int argc, char *argv[]) {
  CallDoublyLinkedList(std::cin);
  return 0;
}



Measured memory_bytes: 14832