#include<bits/stdc++.h>
using namespace std ;
const int N = 1005 ;
int p , n , deg[N] ;
vector<int> nei[N] , path ;
void dfs(int node , int par=-1){
for(int i:nei[node]){
if(i!=par) dfs(i,node) ;
}
path.push_back(node) ;
}
void A(){
for(int i=1 ; i<n ; i++){
int a , b ; cin >> a >> b ;
nei[a].push_back(b) ; nei[b].push_back(a) ;
deg[a]++ ; deg[b]++ ;
}
int root ;
for(int i=0 ; i<n ; i++){
if(deg[i]==1) root = i ;
}
for(int i=0 ; i<n ; i++){
if(deg[i]==n-1) root = i ;
}
dfs(root) ;
path.pop_back() ;
reverse(path.begin(),path.end()) ;
if(path[0]<path[1]) cout << '0' << endl ;
else cout << '1' << endl ;
for(int i:path) cout << i << endl ;
}
void B(){
string s ; cin >> s ;
int temp ;
for(int i=1 ; i<n ; i++){
int a , b ; cin >> a >> b ;
if(i==1){
if(s[0]=='0') cout << a << endl , temp = b ;
else cout << b << endl , temp = a ;
}
else cout << temp << endl , temp = a+b-temp ;
}
}
int main(){
cin >> p >> n ;
if(p==1) A() ;
else B() ;
return 0 ;
}