const config = {
    input: '/dev/stdin',
    newline: '\n'
};

const line = require('fs').readFileSync(config.input, 'utf-8').split(config.newline);

const createList = x => {
    return { key: x,
             pre: null,
             next: null,
             insert: function(x) {
                let newList = createList(x);
                this.pre = newList;
                newList.next = this;
                return newList;
             },
             del: function(x, dir) {
                dir = dir || "next";
                if (this.pre !== null && dir === "pre") {
                    if (this.pre.key === x) {
                        if (this.pre.pre !== null) this.pre.pre.next = this;
                        this.pre = this.pre.pre;
                    } else this.pre.del(x, "pre");
                } else if (this.next !== null && dir === "next") {
                    if (this.next.key === x) {
                        if (this.next.next !== null) this.next.next.pre = this;
                        this.next = this.next.next;
                    } else this.next.del(x, "next");
                }
                return this;
             },
             delFirst: function() {
                if (this.pre === null) {
                    this.next.pre = null;        
                    return this.next;
                } else {
                    return this.pre.delFirst(); 
                }
             },
             delLast: function(x) {
                if (this.next === null) {
                    this.pre.next = null;        
                    return this.pre;
                } else {
                    return this.next.delLast(); 
                }
             },
             moveHead: function() {
                if (this.pre === null) return this;
                else return this.pre.moveHead();
             }
           };
};

const toArray = list => {
    if (list.next === null) return [list.key];
    return [list.key].concat(toArray(list.next));
};

const command = line.slice(1, line.length-1)
                    .map(xs => xs.split(' '));

const firstCommand = command[0];
const tailCommand = command.slice(1);

const list = tailCommand.reduce((acc, x) => {
    const cmd = x[0];
    const num = x.length == 2 ? parseInt(x[1]) : null;
    switch(cmd) {
        case "insert":
            return acc.insert(num);
        case "delete":
            return acc.del(num);
        case "deleteFirst":
            return acc.delFirst();
        case "deleteLast":
            return acc.delLast();
    }
}, createList(parseInt(firstCommand[1])));

console.log(toArray(list.moveHead()).join(' '));

