#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

int main()
{
    for(;;){
        int n, m;
        cin >> n >> m;
        if(n == 0)
            return 0;

        vector<vector<int> > edges(n);
        for(int i=0; i<m; ++i){
            int a, b;
            cin >> a >> b;
            edges[a-1].push_back(b-1);
            edges[b-1].push_back(a-1);
        }

        vector<bool> check(n, false);
        check[0] = true;
        for(int i=0; i<2; ++i){
            vector<bool> next = check;
            for(int j=0; j<n; ++j){
                if(check[j]){
                    for(unsigned k=0; k<edges[j].size(); ++k)
                        next[edges[j][k]] = true;
                }
            }
            check = next;
        }

        int ret = -1;
        for(int i=0; i<n; ++i){
            if(check[i])
                ++ ret;
        }
        cout << ret << endl;
    }
}
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using Graph=vector<vector<int>>;
#define MOD 1000000007
#define INF 1000000000

int main(){
  int N,M;
  cin>>N>>M;
  Graph G(N);
  vector<int> count(N,0);
  for(int i=0;i<N+M-1;i++){
    int A,B;
    cin>>A>>B;
    A--,B--;
    G.at(A).push_back(B);
    count.at(B)++;
  }

  int root;
  for(int i=0;i<N;i++){
    if(count.at(i)==0){
      root=i;
    }
  }

  vector<int> parent(N,-1);
  stack<int> s;
  s.push(root);
  while(!s.empty()){
    int v=s.top();
    s.pop();
    for(auto nv:G.at(v)){
      if(count.at(nv)>1){
        count.at(nv)--;
      }else{
        parent.at(nv)=v;
        s.push(nv);
      }
    }
  }

  for(int i=0;i<N;i++){
    cout<<parent.at(i)+1<<endl;
  }
}
