#include <iostream>
#include <sstream>
#include <vector>
#include <list>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
using namespace std;

typedef vector <int> VI;
const int size = 102;
int T[size][size];
int TC[size];

int front( int a ) {
    return T[a][0];
}

bool isFront( int a, int b ) {
    return front( a ) == b;
}

bool first( int a, int b, int c ) {
    for ( int i = 0; i < TC[a]; ++ i ) {
        if ( T[a][i] == b ) {
            return true;
        } else if ( T[a][i] == c ) {
            return false;
        }
    }
    return false;
}

void push( int a, int b ) {
    T[a][TC[a]++] = b;
}

void pop( int a, int b ) {
    int i = 0;
    for ( ; i < TC[a]; ++ i ) {
        if ( T[a][i] == b ) {
            break;
        }
    }
    for ( ; i + 1 < TC[a]; ++ i ) {
        T[a][i] = T[a][i+1];
    }
    TC[a] --;
}

int getSize( int a ) {
    return TC[a];
}

bool empty( int a ) {
    return getSize( a ) == 0;
}

void putoff( int a, int b ) {
    int i = 0;
    for ( ; i < TC[a]; ++ i ) {
        if ( T[a][i] == b ) {
            break;
        }
    }
    for ( int j = i + 1; j < TC[a]; ++ j ) {
        for ( int k = 0; k < size; ++ k ) {
            if ( empty( k ) ) {
                push( k, T[a][j] );
                break;
            }
        }
    }
    TC[a] = i + 1;
}

bool find( int a, int b ) {
    for ( int i = 0; i < TC[a]; ++ i ) {
        if ( T[a][i] == b ) return true;
    }
    return false;
}

int main() {
    int m;
    while ( cin >> m && m ) {
        for ( int i = 0; i < size; ++ i ) TC[i] = 0;
        for ( int i = 0; i < m; ++ i ) push( i, i + 1 );
        int a, b;
        while ( cin >> a >> b && a ) {
            if ( a == b ) continue;
            if ( b == 0 ) {
                for ( int i = 0; i < size; ++ i ) {
                    if ( find( i, a ) ) {
                        if ( ! isFront( i, a ) ) {
                            putoff( i, a );
                            pop( i, a );
                            for ( int j = 0; j < size; ++ j ) {
                                if ( empty( j ) ) {
                                    push( j, a );
                                    break;
                                }
                            }
                        }
                    }
                }
                continue;
            }
            for ( int i = 0; i < size; ++ i ) {
                if ( find( i, b ) ) {
                    if ( find( i, a ) ) {
                        if ( first( i, a, b ) ) {
                            putoff( i, a );
                        }
                    } else {
                        for ( int j = 0; j < size; ++ j ) {
                            if ( find( j, a ) ) {
                                putoff( j, a );
                                pop( j, a );
                                push( i, a );
                                break;
                            }
                        }
                    }
                }
            }
        }
        VI v;
        for ( int i = 0; i < size; ++ i ) {
            if ( ! empty( i ) ) v.push_back( TC[i] );
        }
        sort( v.begin(), v.end() );
        for ( VI::iterator it = v.begin(); it != v.end(); ++ it ) {
            cout << *it << endl;
        }
        cout << "end" << endl;
    }
    return 0;
}

Measured memory_bytes: 0