#include <iostream>
#include <vector>
#include <algorithm>
#include <set>

using namespace std;

class Block{
public:
  Block *above;
  Block *below;
  Block() {
    above = 0;
    below = 0;
  }

  Block *getRoot() {
    if(below == 0) return this;
    return below->getRoot();
  }
  Block *rgetRoot() {
    if(above == 0) return this;
    return above->rgetRoot();
  }
  int getRank(int rank = 1) {
    if(below == 0) return rank;
    return below->getRank(rank + 1);
  }

  int rgetRank(int rank = 1) {
    if(above == 0) return rank;
    return above->rgetRank(rank + 1);
  }

  void onFloor() {
    if(above != 0) {
      above->onFloor();
    }
    if(below != 0) {
      below->above = 0;
      below = 0;
    }
  }

  void put(Block *block) {
    Block *tmp = block->rgetRoot();
    below = tmp;
    tmp->above = this;
  }
};

int main() {
  while(1) {
    int m;
    int I, J;
    Block block[100];
    cin >> m;
    if(m == 0) break;
    while(1) {
      cin >> I >> J;
      if(I == 0 && J == 0) break;
      I--;
      J--;
      if(J == -1) {
	if(block[I].below != 0) block[I].onFloor();
      } else if(block[I].getRoot() == block[J].getRoot()) {
	if(block[I].getRank() < block[J].getRank()) {
	  block[I].onFloor();
	  block[I].put(&block[J]);
	}
      } else {
	block[I].onFloor();
	block[I].put(&block[J]);
      }
    }
    set<Block *> s;
    vector<int> rank;
    for(int i = 0; i < m; i++) {
      Block *tmp = block[i].getRoot();
      if(s.find(tmp) == s.end()) {
	s.insert(tmp);
	rank.push_back(tmp->rgetRank());
      }
    }
    sort(rank.begin(), rank.end());

    for(int i = 0; i < rank.size(); i++) {
      cout << rank[i] << endl;
    }
    cout << "end" << endl;
  }
}