class LinkedList

  @@top = nil

  def initialize(key = nil)
    @key = key
    @prev = self
    @next = self
    @@top = self if @@top.nil?
  end

  attr_accessor :key, :next, :prev

  def insert(key)
    new = LinkedList::new(key)
    new.prev = @@top
    new.next = @@top.next
    @@top.next.prev = new
    @@top.next = new
  end

  def print
    i = 0
    p = @@top
    loop do
      puts "[#{i}]:#{p.key} prev:#{p.prev ? p.prev.key : nil} next:#{p.next ? p.next.key : nil}"
      p = p.next
      i += 1
      break if p == @@top || p.nil?
    end
    puts
  end

  def p
    a = []
    p = @@top.next
    while p != @@top && p
      a << p.key
      p = p.next
    end
    puts a * " "
  end

  def find(key)
    p = @@top.next
    while p != @@top && p
      return p if p.key == key
      p = p.next
    end
    return nil
  end

  def delete(key)
    delete_node(self.find(key))
  end

  def delete_node(p)
    return if p.nil? || p == @@top
    p.prev.next = p.next
    p.next.prev = p.prev
  end
  
  def delete_first
    delete_node(@@top.next)
  end
  
  def delete_last
    delete_node(@@top.prev)
  end
end

p = LinkedList::new()
gets
while gets
  c,x=$_.split.map &:to_s
  case c
  when "insert"
    p.insert(x.to_i)
  when "delete"
    p.delete(x.to_i)
  when "deleteFirst"
    p.delete_first
  when "deleteLast"
    p.delete_last
  else
    puts c,x
    raise
  end
end
p.p


