/*
 *
 *  Implementáció listával
 */

#include <iostream>

using namespace std;

struct elem
{
    elem *elozo;
    elem *kovetkezo;
    int   ertek;
};

elem *elso = 0;
elem *utolso = 0;
int   elemek_szama = 0;

void elejere_fuz( int i);
void vegere_fuz( int i);
void kiir_elore();
void kiir_hatra();
void felszamol();

int main()
{
    int ii;

    while ( cin >> ii )
    {
        vegere_fuz(ii);
    }
    cout << "elemszam = " << elemek_szama << endl;
    kiir_elore();
    kiir_hatra();
    
    felszamol();
    cout << "elemszam = " << elemek_szama << endl;
    
    return 0;
}

void elejere_fuz( int ii)
{
    elem *uj = new elem;
    uj->ertek = ii;
    uj->elozo = uj->kovetkezo = 0;
    
    if ( elso )
    {
        uj->kovetkezo = elso;
        elso->elozo = uj;
        elso = uj;
    }
    else
    {
        elso = utolso = uj;
    }
    ++elemek_szama;
}

void vegere_fuz( int ii)
{
    elem *uj = new elem;
    uj->ertek = ii;
    uj->elozo = uj->kovetkezo = 0;
    
    if ( utolso )
    {
        uj->elozo = utolso;
        utolso->kovetkezo = uj;
        utolso = uj;
    }
    else
    {
        elso = utolso = uj;
    }
    ++elemek_szama;
}

void kiir_elore()
{
    elem *kurrens = elso;
    
    while ( kurrens )
    {
        cout << kurrens->ertek << endl;
        kurrens = kurrens->kovetkezo;
    }
}

void kiir_hatra()
{
    elem *kurrens = utolso;
    
    while ( kurrens )
    {
        cout << kurrens->ertek << endl;
        kurrens = kurrens->elozo;
    }
}

void felszamol()
{
    while ( elso )
    {
        elem *kurrens = elso;
        elso = kurrens->kovetkezo;
        // elso->elozo = 0;
        // --elemek_szama;
        delete kurrens;
    }
    elso = 0;
    utolso = 0;
    elemek_szama = 0;
}