This problem is split into two tasks. In this task, you are required to find the minimum possible answer. In the task Village (Maximum) you are required to find the maximum possible answer. Each task is worth $$$50$$$ points.There are $$$N$$$ houses in a certain village. A single villager lives in each of the houses. The houses are connected by roads. Each road connects two houses and is exactly $$$1$$$ kilometer long. From each house it is possible to reach any other using one or several consecutive roads. In total there are $$$N-1$$$ roads in the village.One day all villagers decided to move to different houses — that is, after moving each house should again have a single villager living in it, but no villager should be living in the same house as before. We would like to know the smallest possible total length in kilometers of the shortest paths between the old and the new houses for all villagers. Example village with seven houses For example, if there are seven houses connected by roads as shown on the figure, the smallest total length is $$$8$$$ km (this can be achieved by moving $$$1 \to 6$$$, $$$2 \to 4$$$, $$$3 \to 1$$$, $$$4 \to 2$$$, $$$5 \to 7$$$, $$$6 \to 3$$$, $$$7 \to 5$$$).Write a program that finds the smallest total length of the shortest paths in kilometers and an example assignment of the new houses to the villagers.

The first line contains an integer $$$N$$$ ($$$1 < N \le 10^5$$$). Houses are numbered by consecutive integers $$$1, 2, \ldots, N$$$. Then $$$N-1$$$ lines follow that describe the roads. Each line contains two integers $$$a$$$ and $$$b$$$ ($$$1 \le a, b \le N$$$, $$$a \neq b$$$) denoting that there is a road connecting houses $$$a$$$ and $$$b$$$.

In the first line output the smallest total length of the shortest paths in kilometers. In the second line describe one valid assignment of the new houses with the smallest total length: $$$N$$$ space-separated distinct integers $$$v_1, v_2, \ldots, v_N$$$. For each $$$i$$$, $$$v_i$$$ is the house number where the villager from the house $$$i$$$ should move ($$$v_i \neq i$$$). If there are several valid assignments, output any of those.

#include<bits/stdc++.h>
#define LL long long
#define pb push_back
#define SZ(x) ((int)x.size()-1)
#define ms(a,b) memset(a,b,sizeof a)
#define F(i,a,b) for (int i=(a);i<=(b);++i)
#define DF(i,a,b) for (int i=(a);i>=(b);--i)
using namespace std;
inline int read(){
	char ch=getchar(); int w=1,c=0;
	for(;!isdigit(ch);ch=getchar()) if (ch=='-') w=-1;
	for(;isdigit(ch);ch=getchar()) c=(c<<1)+(c<<3)+(ch^48);
	return w*c;
}
const int M=1e5+10;
int n;
vector<int> v[M];
bool uesd[M];
int fr[M],to[M];
LL ans;
bool used[M];
void dfs(int x,int fa){
	int num=0;
	int lst=0,hh=0;
	bool gg=0;
	F(i,0,SZ(v[x])){
		int y=v[x][i];
		if (y==fa) continue;
		dfs(y,x);
		num+=(!used[y]);
		if (!used[y]){
			if (lst){
				fr[y]=to[y]=lst;
				to[lst]=fr[lst]=y;
				lst=0;
				used[y]=used[lst]=1;
				ans+=4;
			}
			else lst=y;
			hh=y; gg=1;
		}
		if (!gg) hh=y;
	}

	if (!used[x] && gg){
		used[x]=1;
		to[fr[hh]]=x;
		fr[x]=fr[hh];
		to[x]=hh;
		fr[hh]=x;
	}
	if (lst){
		used[lst]=used[x]=1;
		fr[x]=to[x]=lst;
		to[lst]=fr[lst]=x;
		ans+=2;
	}
	if (!used[x] && x==1){
		used[x]=1;
		to[fr[hh]]=x;
		fr[x]=fr[hh];
		to[x]=hh;
		fr[hh]=x;
		ans+=2;
	}
}
int main(){
	n=read();
	F(i,1,n-1){
		int x=read(),y=read();
		v[x].pb(y); v[y].pb(x);
	}
	dfs(1,0);
	cout<<ans<<"\n";
	F(i,1,n){
		cout<<to[i]<<" ";
	}
	cout<<"\n";
	return 0;
}

#include <bits/stdc++.h>

using namespace std;

vector<int> adj[100005];
int n;

int changed[100005];

int lastVisited = -1;
int dfs(int v, int p){
	lastVisited = v;
	int sum = 0;
	for(int to : adj[v]){
		if(to != p){
			sum += dfs(to,v);
		}
	}
	if(changed[v]==v){
		if(p==0){
			int to = adj[v][0];
			changed[v] = changed[to];
			changed[to] = v;
		}
		else{
			changed[v] = changed[p];
			changed[p] = v;
		}
		sum += 2;
	}
	return sum;
}

int main(){
	cin >> n;
	for(int i =0; i < n-1; i++){
		int a,b;
		cin >> a>>b;
		adj[a].push_back(b);
		adj[b].push_back(a);
	}
	for(int i =0; i <=n; i++){
		changed[i] =i;
	}
	cout << dfs(1,0)<<endl;
	for(int i =1; i <=n; i++){
		cout << changed[i] << " ";
	}
}

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<set>
#define LL long long
using namespace std;
const int N=3e5+10;
int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
void print(LL x){
    if(x>9) print(x/10);
    putchar(x%10+'0');
}
int n,cnt=1;
struct edge{
	int r,nxt;
}e[N];
int hed[N];
void insert(int u,int v){
	e[++cnt].r=v;e[cnt].nxt=hed[u];hed[u]=cnt;
}
LL ans=0;
int p[N],fa[N];
void dfs(int u){
	for(int i=hed[u];i;i=e[i].nxt){
		if(e[i].r==fa[u])continue;
		fa[e[i].r]=u;
		dfs(e[i].r);
	}
	if(p[u]==u&&fa[u]){
		ans+=2;
		swap(p[u],p[fa[u]]);
	}
}
int main(){
	int u,v;
	scanf("%d",&n);
	for(int i=1;i<n;++i){
		scanf("%d%d",&u,&v);
		insert(u,v);insert(v,u);
	}
	for(int i=1;i<=n;++i) p[i]=i;
	dfs(1);
	if(p[1]==1) {
		v=e[hed[1]].r;
		ans+=2;
		swap(p[1],p[v]);
	}
	printf("%I64d\n",ans);
	for(int i=1;i<=n;++i) printf("%d ",p[i]);puts("");
	return 0;
}

#include <bits/stdc++.h>
#define vi vector<int>
#define vvi vector<vi>
#define ii pair<int, int>
#define vii vector<ii>
#define vvii vector<vii>
#define vb vector<bool>
#define x first
#define y second
#define loop(i,s,e) for(int i=s;i<e;++i)
#define loopr(i,s,e) for(int i=e;i>=s;--i)
#define pb push_back
#define chkmax(a,b) a = max(a,b)
#define chkmin(a,b) a = min(a,b)
#define all(a) a.begin(), a.end()
using namespace std;
const int INF = 1e9;

int n;
vvi g;

vi ans;
int res = 0;
void make(int a, int b){
    swap(ans[a], ans[b]);
    res+=2;
}
void recon(int cur, int f=-1){
    for(auto nei:g[cur]) if(nei!=f){
        recon(nei, cur);
    }
    if (ans[cur]==cur){
        if (f!=-1) make(cur, f);
        else{
            make(cur, g[cur][0]);
        }
    }
}
int32_t main(){
    ios_base::sync_with_stdio(0);
    cin>>n;
    g.resize(n);
    loop(i,0,n-1){
        int a,b; cin>>a>>b; a--; b--;
        g[a].pb(b); g[b].pb(a);
    }
    ans.resize(n);
    loop(i,0,n) ans[i]=i;
    recon(0);
    cout<<res<<endl;
    loop(i,0,n) cout<<ans[i]+1<<" ";
    cout<<endl;
    return 0;
}
/*
color a
cls
g++ bb.cpp -o a & a
5
1 2
2 3
2 4
4 5

3
1 2
2 3

*/

#include <iostream>
#include <algorithm>
#include <vector>
#include <assert.h>
#define vi vector<int>
#define pub push_back
using namespace std;

int n;
vi adj[100001];
int res[100001], dp[100001][2], k = 0;
int mtcW[100001];

void dfs(int u, int p)
{
	dp[u][0] = dp[u][1] = 0;
	int ret = 0;
	for (auto v : adj[u])
	{
		if (v != p)
		{
			dfs(v, u);
			ret += dp[v][1];
		}
	}
	dp[u][0] = ret;
	for (auto v : adj[u])
	{
		if (v != p)
		{
			assert(dp[v][1] >= dp[v][0]);
			dp[u][1] = max(dp[u][1], 1 + ret - dp[v][1] + dp[v][0]);
		}
	}
}

void bktrk(int u, int p, int s)
{
	if (dp[u][s] == 0 && res[u] == -1)
	{
		res[u] = res[p];
		res[p] = u;
	}
	if (s)
	{
		int ret = 0, spc = -1;
		for (auto v : adj[u])
		{
			if (v != p) {ret += dp[v][1];}
		}
		for (auto v : adj[u])
		{
			if (v != p)
			{
				assert(dp[v][1] >= dp[v][0]);
				if (1 + ret - dp[v][1] + dp[v][0] == dp[u][1])
				{
					spc = v;
					res[spc] = u;
					res[u] = spc;
					break;
				}
			}
		}
		for (auto v : adj[u])
		{
			if (v != p) bktrk(v, u, (spc != v));
		}
	}
	else
	{
		for (auto v : adj[u])
		{
			if (v != p) {bktrk(v, u, 1);}
		}
	}
}

int main()
{
	cin >> n;
	for (int i = 0; i < n; i++) res[i] = -1;
	for (int i = 1; i < n; i++)
	{
		int u, v; cin >> u >> v;
		adj[u - 1].push_back(v - 1);
		adj[v - 1].push_back(u - 1);
	}

	dfs(0, -1);
	bktrk(0, -1, 1);
	cout << 2 * (dp[0][1] + n - 2 * dp[0][1]) << "\n";

	for (int i = 0; i < n; i++)
	{
		cout << res[i] + 1;
		if (i + 1 < n) cout << " ";
	}
	return 0;
}

#include <bits/stdc++.h>
using namespace std;

#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <class T> using Tree = tree<T, null_type, less<T>,
rb_tree_tag, tree_order_statistics_node_update>;

#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define f first
#define s second
#define pii pair<int,int>
#define is insert
const long long INF = 1000000000;
const long long MOD = 1000000007;
const int MAXN = 2e5;
//store test cases here
/*

*/
int n;
int orig[MAXN];
long long ans=0;
vector<int> adj[MAXN];
void dfs(int v,int last){
	if(orig[v]==v){
		//after handling all children, if still untouched, exchange with parent
		for(int i=0;i<adj[v].size();i++){
			if(adj[v][i]!=last){
				dfs(adj[v][i],v);
			}
		}
		if(orig[v]==v){
			if(v==last){
				for(int i=0;i<adj[v].size();i++){
					if(adj[v][i]!=last){
						swap(orig[v],orig[adj[v][i]]);
						ans+=2;
						break;
					}
				}
			} else{
				swap(orig[last],orig[v]);
				ans+=2;
			}
		}
	}
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>n;
    //check edge case of n=2

    for(int i=0;i<n-1;i++){
    	int a,b;
    	cin>>a>>b;
    	a--,b--;
    	adj[a].pb(b);
    	adj[b].pb(a);
    }
    /*
    if(n==2){
    	cout<<2<<"\n";
    	cout<<"2 1"<<"\n";
    }
    */
    for(int i=0;i<n;i++){
    	orig[i]=i;
    }
    dfs(0,0);
    /*
    for(int i=0;i<n;i++){
    	dfs(i,i);
    }
    */
    cout<<ans<<"\n";
    for(int i=0;i<n;i++){
    	if(i==n-1){
    		cout<<orig[i]+1<<"\n";
    	} else{
    		cout<<orig[i]+1<<" ";
    	}
    }
    return 0;
}
/* REMINDERS
 * STORE INFO IN VECTORS, NOT STRINGS!!!!!!!!!
 * CHECK ARRAY BOUNDS, HOW BIG ARRAY HAS TO BE
 * PLANNING!!!!!!!! Concrete plan before code
 * IF CAN'T FIGURE ANYTHING OUT, MAKE TEN TEST CASES TO EVALUATE ALL TYPES OF SCENARIOS, THEN CONSTRUCT SOLUTION TO FIT IT
 * IF CAN'T FIGURE ANYTHING OUT, MAKE TEN TEST CASES TO EVALUATE ALL TYPES OF SCENARIOS, THEN CONSTRUCT SOLUTION TO FIT IT
 * IF CAN'T FIGURE ANYTHING OUT, MAKE TEN TEST CASES TO EVALUATE ALL TYPES OF SCENARIOS, THEN CONSTRUCT SOLUTION TO FIT IT
 * NAIVE SOL FIRST TO CHECK AGAINST OPTIMIZED SOL
 * MOD OUT EVERY STEP
 * DON'T MAKE ASSUMPTIONS
 * DON'T OVERCOMPLICATE
 * CHECK INT VS LONG, IF YOU NEED TO STORE LARGE NUMBERS
 * CHECK CONSTRAINTS, C <= N <= F...
 * CHECK SPECIAL CASES, N = 1...
 * TO TEST TLE/MLE, PLUG IN MAX VALS ALLOWED AND SEE WHAT HAPPENS
 * ALSO CALCULATE BIG-O, OVERALL TIME COMPLEXITY
 * IF ALL ELSE FAILS, DO CASEWORK
 * compile with "g++ -std=c++11 filename.cpp" if using auto keyword
 */

#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define pb push_back

const int N=1e5+2;

ll ans=0;
vector<int> place(N,0);
vector<vector<int>> g(N,vector<int>());

void dfs(int v,int p=-1)
{
    for(int it : g[v])
    {
        if(it != p)
        {
            dfs(it,v);
        }
    }
    if(place[v]==v)
    {
        if(p!=-1)
        {
            swap(place[v],place[p]);
        }
        else
        {
            swap(place[v],place[g[v].back()]);
        }
        ans+=2;
    }

}

int main()
{
    ll n;
    cin>>n;
    for(ll i=1;i<=n;i++)
    {
        place[i]=i;
    }
    for(ll i=1;i<n;i++)
    {
        ll a,b;
        cin>>a>>b;
        g[a].pb(b);
        g[b].pb(a);
    }
    dfs(1);
    cout<<ans<<"\n";
    for(ll i=1;i<=n;i++)
    {
        cout<<place[i]<<" ";
    }
    return 0;

}

#include<bits/stdc++.h>
#define forn(i, n) for (int i = 0; i < (n); i++)
#define forab(i, a, b) for (int i = (a); i <= (b); i++)
#define forba(i, b, a) for (int i = (b); i >= (a); i--)
#define mset(a, x, n) memset(a, x, sizeof(a[0]) * (n))
#define updiv(x, y) (((x) + (y) - 1) / (y)) // y > 0
#define pb push_back
#define mp(x, y) make_pair(x, y)
#define fi first
#define se second
#define sz(x) ((int)x.size())
#define all(x) (x).begin(), (x).end()
#define endl '\n'
#ifdef hat
#define fast
#define de(x) cout  << #x << '=' << x << ' '
#define dee(x) cout  << #x << '=' << x << endl
#else
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define de(x) ((void) 0)
#define dee(x) ((void) 0)
#endif
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef pair<int, int> pii;
typedef vector<int> VI;

const int maxn = 1e5 + 5;
const int mod = 998244353;
const int INF = 0x3f3f3f3f;
const ll llINF = 0x3f3f3f3f3f3f3f3f;
ll make_compiler_happy() {return INF & maxn & mod & llINF;}
ll fpow(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll inv(ll x) {return fpow(x, mod-2);}

int n;
vector<int> g[maxn];
int ans[maxn];
int dep[maxn];
int sum = 0;

void dfs(int u, int fa, int d)
{
    dep[u] = d;
    for(int v : g[u])
    {
        if(v == fa) continue;
        dfs(v, u, d + 1);
    }
}

int dfs(int u, int fa)
{
    if(fa && sz(g[u]) == 1) return 1;
    int t = u;
    for(int v : g[u])
    {
        if(v == fa) continue;
        if(dfs(v, u)) {
            sum++;
            ans[v] = t;
            t = v;
        }
    }
    ans[u] = t;
    return ans[u] == u;
}

int main()
{
    fast;
    cin >> n;
    forn(i, n-1)
    {
        int u, v;
        cin >> u >> v;
        g[u].pb(v);
        g[v].pb(u);
    }
    int t = 1;
    while(t + 1 <= n && sz(g[t]) == 1) t++;
    dfs(t, 0, 0);
    if(dfs(t, 0)) {
        sum++;
        int u = g[t][0];
        ans[t] = ans[u];
        ans[u] = t;
    }
    cout << 2 * sum << endl;
    forab(i, 1, n)
        cout << ans[i] << " \n"[i==n];
}

#include <bits/stdc++.h>
using namespace std;

const int mxN=1e5;
int n, h[mxN], ans1, ans2[mxN];
vector<int> adj[mxN], a[mxN];

void mrg(int u, int v) {
	if(a[u].size()<a[v].size())
		swap(a[u], a[v]);
	a[u].insert(a[u].end(), a[v].begin(), a[v].end());
	a[v].clear();
}

void dfs(int u=0, int p=-1) {
	a[u]={u};
	if(u) {
		for(int v : adj[u]) {
			if(v==p)
				continue;
			dfs(v, u);
			if(h[v])
				++ans1;
			else {
				mrg(u, v);
				h[u]=1;
			}
		}
	} else {
		set<int> s;
		for(int v : adj[u]) {
			dfs(v, u);
			if(!h[v])
				s.insert(v);
		}
		if(s.empty())
			s.insert(adj[u][0]);
		for(int v : adj[u]) {
			if(s.find(v)==s.end())
				++ans1;
			else
				mrg(u, v);
		}
	}
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);

	cin >> n;
	for(int i=1, a, b; i<n; ++i) {
		cin >> a >> b, --a, --b;
		adj[a].push_back(b);
		adj[b].push_back(a);
	}
	dfs();
	cout << 2*(n-1-ans1) << "\n";
	for(int i=0; i<n; ++i) {
		if(!a[i].size())
			continue;
		for(int j=0; j<a[i].size(); ++j)
			ans2[a[i][j]]=a[i][(j+1)%a[i].size()];
	}
	for(int i=0; i<n; ++i)
		cout << ans2[i]+1 << " ";
}

#include <bits/stdc++.h>
#define vi vector<int>
#define vvi vector<vi>
#define ii pair<int,int>
#define x first
#define y second
#define pb push_back
#define loop(i,s,e) for(int i=s;i<e;++i)
#define int int64_t
#define mod 1000000007
#define INF 100000000
using namespace std;

vvi g; vi v; vi p,np;

void add(int a,int b){
    p[a]=b; np[b]=a;
}
/*
 getNext(){
    while(!q.empty()){
        int cur=q.front(); q.pop();
        if(np[cur]){ return cur; }
    }
}*/

ii func(int cur=1,int head=1, int rnd=1){
    queue<int> q;
    v[cur]=rnd; ii sol; sol.x=1; sol.y=0; rnd++; //cout<<cur<<":"<<endl;
    for(auto nei: g[cur]){
        ii tmp;
        if(v[nei]==0){
            tmp=func(nei,head,rnd);
            if(tmp.x==1){ q.push(nei); sol.x++; }
            sol.y+=tmp.y;
        }
    }
    int a,b;
    if(sol.x==1&&cur==head){
        int done=1;
        for(auto nei:g[cur]){
            for(auto nein:g[nei]){
                if(done==1&&p[nein]==nei){
                    p[nein]=cur; p[cur]=nei;
                    done=0;
                }
            }
        }
        sol.y+=2; return sol;
    }
    if(sol.x==1){ return sol; }
    if(sol.x%2==0){
        //cout<<q.size()<<endl<<sol.x<<endl;
        a=q.front(); q.pop(); add(a,cur); add(cur,a);
        while(!q.empty()){
            a=q.front(); q.pop(); b=q.front(); q.pop();
            add(a,b); add(b,a);
        }
        sol.y+=(sol.x-2)*2+2; sol.x=0;
        return sol;
    }
    else{
        if(cur==head){
            a=q.front(); q.pop(); b=q.front(); q.pop();
            add(b,a); add(cur,b); add(a,cur);
            while(!q.empty()){
                a=q.front(); q.pop(); b=q.front(); q.pop(); add(a,b); add(b,a);
            }
            sol.y+=(sol.x-3)*2+4;
            sol.x=0; return sol;
        }
        else{
            a=q.front(); q.pop(); b=q.front(); q.pop(); add(a,b); add(b,a);
            add(a,b); add(cur,a); add(b,cur);
            while(!q.empty()){
                a=q.front(); q.pop(); b=q.front(); q.pop(); add(a,b); add(b,a);
            }
            sol.y+=(sol.x-3)*2+4;
            sol.x=0; return sol;
        }
    }
}

int32_t main(){
    ios_base::sync_with_stdio(false); int head=2;
    int n; cin>>n; g.resize(n+1);  loop(i,1,n){ int A,B; cin>>A>>B; g[A].pb(B); g[B].pb(A); }  v.resize(n+1,0);
    p.resize(n+1,-1); np.resize(n+1,-1); cout<<func(head,head,1).y<<endl; loop(i,1,n+1){ cout<<p[i]<<" "; }
    vi check; check.resize(n+1,0);
    /*loop(i,1,n+1){
        if(p[i]=-1){ while(6>5){ n++; } }
        if(check[p[i]]==1){ while(6>5){ n++; } }check[p[i]]=1;
    }*/

    return 0;
}

#include <bits/stdc++.h>
using namespace std;

// rotate(vec.begin(), vec.begin() + k, vec.end()); Might come handy
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(c) c.begin(),c.end()
#define sz(a) int((a).size())
typedef long long int lli;
typedef vector<int> vi;
typedef pair<int,int> pi;
typedef map<int,int> mi;
typedef set<int> si;
typedef vector< pair <int,int> > vii;
//vec.resize(unique(all(vec)) - vec.begin()); Remove Consecutive Duplicated
//deque<int> d; length can be changed from both sides
// memset(dp,-1,sizeof(dp));
// priority_queue <int, vector<int>, greater<int>>
// int x = min({a, b, c, d});

/* // NEGATIVE INDICES
const int M = 10; // I want indices from -M to +M
int _a[2*M+1]; // underlying array of size 2*M+1
int* a = _a + M; // array that has index [0] in the middle of the big array
int main() {
    a[-10] = a[1] = 7;
    cout << a[-10] << " " << a[3] << " " << a[10] << endl;
} */

bool sortinrev(const pair<int,int> &a,
               const pair<int,int> &b) {
       return (a.first > b.first);
}

const int MAXN = 100005;
vi adj[MAXN];
vi vis(MAXN,0);
vi pro(MAXN,0);
lli ans = 0;
vi par(MAXN);
vi h(MAXN,0);

void dfs(int s)
{
    vis[s] = 1;
    for (auto u: adj[s])
        if (!vis[u])
        {
            par[u] = s;
            dfs(u);
        }
    if (pro[s]==0)
    {
        ans+=2;
        // cout << s << " " << par[s] << "\n";
        pro[s] = 1;
        pro[par[s]] = 1;
        if (s==1)
        {
            h[1] = h[adj[1][0]];
            h[adj[1][0]] = 1;
        }
        else
        {
            int tem1 = h[par[s]],tem2 = h[s];
            h[s] = tem1;
            h[par[s]] = tem2;
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;
    for (int i=1;i<n;i++)
    {
        par[i] = i;
        h[i] = i;
        int u,v; cin >> u >> v;
        adj[u].pb(v); adj[v].pb(u);
    }
    par[n] = n;
    h[n] = n;
    dfs(1);
    cout << ans << "\n";
    for (int i=1;i<=n;i++)
        cout << h[i] << " ";

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5+10;
typedef long long ll;
/***
题解：
关于树的题目，一般都是从叶子节点开始考虑
对于这个题目，可以贪心的想，如果可以两个节点两个节点的互换，那么就直接互换即可
如果不能两个节点两个节点的互换，那么就是可能要多个节点交换。
所以用一个flag来标记子节点传递给父亲节点的数量
容易得到，对于u的每一棵子树，要么传上来的节点数量是0要么是1
如果传上来节点，那么u和这些节点进行交换
如果没有，那么保留u网上传递这个节点
最后注意要特判根节点。
***/

int head[maxn],to[maxn<<1],nxt[maxn<<1],cnt;
void add(int u,int v){
	++cnt,to[cnt]=v,nxt[cnt]=head[u],head[u]=cnt;
	++cnt,to[cnt]=u,nxt[cnt]=head[v],head[v]=cnt;
}
int flag[maxn],ans[maxn],num;
void dfs(int u,int pre){
	// printf("u=%d pre=%d\n", u,pre);
	int now = 0;
	vector<int>tmp;
	tmp.clear();
	tmp.push_back(u);
	for(int i=head[u];i;i=nxt[i]){
		int v = to[i];
		if(v == pre) continue;
		dfs(v,u);
		if(flag[v]) tmp.push_back(v);
	}
	int len = tmp.size();
	if(len>1){
		flag[u] = 0;
		num += (len-1)*2;
		for(int i=0;i<len;i++) ans[tmp[(i-1+len)%len]] = tmp[i];
	}
	else flag[u] = 1;
}

int main(){
	int n;
	num = 0;
	scanf("%d",&n);
	for(int i=1;i<n;i++){
		int u,v;
		scanf("%d%d",&u,&v);
		add(u,v);
	}
	dfs(1,0);
	if(flag[1]){
		num+=2;
		int u = to[head[1]];
		ans[1] = ans[u];
		ans[u] = 1;
	}
	printf("%d\n", num);
	for(int i=1;i<n;i++) printf("%d ", ans[i]);
	printf("%d\n", ans[n]);
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define mp make_pair
#define PI pair<ll,ll>
#define poly vector<ll>
#define mem(a) memset((a),0,sizeof(a))
#define For(i,l,r) for(int i=(int)(l);i<=(int)(r);i++)
#define Rep(i,r,l) for(int i=(int)(r);i>=(int)(l);i--)
#define pb push_back
#define fi first
#define se second
#define SZ(x) ((int)(x.size()))
#define re resize
inline char gc(){
    static char buf[100000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
#define gc getchar
inline ll read(){
	ll x = 0; char ch = gc(); bool positive = 1;
	for (; !isdigit(ch); ch = gc())	if (ch == '-')	positive = 0;
	for (; isdigit(ch); ch = gc())	x = x * 10 + ch - '0';
	return positive ? x : -x;
}
inline void write(ll a){
    if(a<0){
    	a=-a; putchar('-');
	}
    if(a>=10)write(a/10);
    putchar('0'+a%10);
}
inline void writeln(ll a){write(a); puts("");}
inline void wri(ll a){write(a); putchar(' ');}
ll rnd(){
	ull ans=0;
	For(i,0,4)ans=ans<<15^rand();
	return ans%((ull)1<<63);
}
const int N=100005,inf=1e9;
int n,dp[N][3],res[N],nxt[N]; //分别表示三个有没有，1为根没配
vector<int> v[N];
vector<PI> jj[N][2];
void dfs(int p,int fa){
	For(j,0,1)dp[p][j]=inf;
	dp[p][1]=0;
	For(i,0,v[p].size()-1)if(v[p][i]==fa){v[p].erase(v[p].begin()+i); break;}

	for(auto i:v[p]){
		dfs(i,p);
		int zs[2];
		PI jb[2];
		For(j,0,1)zs[j]=inf;
		For(p2,0,1){
			For(q2,0,1){
				int t=dp[p][p2]+dp[i][q2];
				if(p2&&q2){
					if(t+2<zs[0]){
						zs[0]=t+2; jb[0]=mp(p2,q2);
					}
				}
				else {
					if(t+(p2+q2)*2<zs[p2+q2]){
						zs[p2+q2]=t+q2*2; jb[p2+q2]=mp(p2,q2);
					}
				}
				if(p2+q2==1){
					if(zs[0]>t+2){
						//cout<<zs[0]<<" fj "<<t+2<<endl;
						zs[0]=t+2; jb[0]=mp(p2,q2);
					}
				}
			}
		}
		memcpy(dp[p],zs,sizeof(zs));
		jj[p][0].pb(jb[0]);
		jj[p][1].pb(jb[1]);
	}
	//cout<<dp[p][0]<<" "<<dp[p][1]<<endl;
}
void ae(int x,int y){
	nxt[x]=y; nxt[y]=x;
}
void ins(int x,int y,int z){
	nxt[z]=y;
	nxt[x]=z;

}
void solve(int p,int i,int zt){
	if(i==v[p].size())res[p]=p;
	if(!i)return;
	int t=v[p][i-1],x=jj[p][zt][i-1].fi,y=jj[p][zt][i-1].se;
	//cout<<p<<" "<<t<<" "<<x<<" "<<y<<endl;
	solve(t,v[t].size(),y);
	solve(p,i-1,x);
	if(x&&y){ae(res[p],res[t]); }
	else if(x+y!=zt){if(x==1)ins(t,nxt[t],res[p]); else ins(p,nxt[p],res[t]);}
	else if(y)res[p]=res[t];
}
int main(){
	#ifdef Brollan
		freopen("1.in","r",stdin);
	#endif
	n=read();
	For(i,1,n-1){
		int x=read(),y=read();
		v[x].pb(y); v[y].pb(x);
	}
	dfs(1,0);
	cout<<dp[1][0]<<endl;
	solve(1,v[1].size(),0);
	For(i,1,n)wri(nxt[i]);
}

/*
DavitMarg
In a honky-tonk,
Down in Mexico
*/
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <queue>
#include <iomanip>
#include <bitset>
#include <stack>
#include <cassert>
#include <iterator>
#include <fstream>
#define mod 1000000007ll
#define LL long long
#define LD long double
#define MP make_pair
#define PB push_back
#define all(v) v.begin(), v.end()
#define fastIO ios::sync_with_stdio(false); cin.tie(0)
using namespace std;

const int N = 200005;

int n,used[N],vin[N],ans[N],sz[N],sum;
vector<int> g[N];

void dfs(int v,int p)
{
    vin[v] = v;
    sz[v] = g[v].size();
    for (int i = 0; i < g[v].size(); i++)
    {
        int to = g[v][i];
        if (to == p)
            continue;
        dfs(to, v);
        sz[v]--;
    }
    if (!used[v] || (v != p && sz[p]==1 && !used[p]))
    {
        swap(vin[v], vin[p]);
        ans[vin[p]] = p;
        ans[vin[v]] = v;
        used[v] = used[p] = 1;
        sum += 2;
    }
}

int main()
{
    fastIO;
    cin >> n;
    for (int i = 1; i < n; i++)
    {
        int a, b;
        cin >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    dfs(1, 1);
    cout << sum << endl;
    for (int i = 1; i <= n; i++)
        cout << ans[i] << " ";
    cout << endl;

    return 0;
}

/*

*/

    /**   Created by: Humberto Yusta
          Codeforces User: humbertoyusta
          Country: Cuba
          Copyright��                    */
#include<bits/stdc++.h>
using namespace std;
/// Pragmas:
//#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline") //Optimization flags
//#pragma GCC option("arch=native","tune=native","no-zero-upper") //Enable AVX
//#pragma GCC target("avx2") //Enable AVX
/// Macros:
#define int long long
#define f first
#define s second
#define db(x) cerr << #x << ": " << (x) << '\n';
#define pb push_back
#define lb lower_bound
#define up upper_bound
#define all(x) x.begin() , x.end()
#define rall(x) x.rbegin() , x.rend()
#define enl '\n'
typedef pair<int,int> ii;
typedef long double ld;
typedef unsigned long long ull;
/// Constraints:
const int maxn = 100010;
const int mod = 1000000007;
const ld eps = 1e-9;
const int inf = ((1ll<<31ll)-1ll);
const int INF = 2000000000000000000ll;
const ld pi = acos(-1);
/// Prime Numbers:
// 2, 173, 179, 181, 197, 311, 331, 737, 1009, 2011, 2027, 3079, 4001, 10037, 10079, 20011, 20089;
// 100003, 100019, 100043, 200003, 200017, 1000003, 1000033, 1000037, 1000081;
// 2000003, 2000029, 2000039, 1000000007, 1000000021, 2000000099;
/// Functions:
#define lg2(x) 31 - __builtin_clz(x)
#define lgx(x,b) ( log(x) / log(b) )
/// Red-Black Tree Template ---------------------------------
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//typedef tree < long long ,  null_type ,  less<long long> ,  rb_tree_tag ,  tree_order_statistics_node_update > ordered_set;
/// Quick Pow------------------------------------------------
int qpow(int b,int e){
    if( !e ) return 1;
    if( e & 1 ) return qpow(b,e-1) * b % mod;
    int pwur = qpow(b,e>>1);
    return pwur * pwur % mod;
}
int modinv(int x){
    return qpow(x,mod-2);
}
/// My Code -------------------------------------------------

int n, color[maxn], res[maxn], ans[maxn], cnt;
vector<int> g[maxn], w[maxn];
queue<int> q;

void dfs_color(int u,int c){
    color[u] = c;
    for( auto v : g[u] )
        if( !color[v] )
            dfs_color(v,c%2+1);
}

void dfs(int u,int p){
    for( auto v : g[u] )
        if( v != p )
            dfs(v,u);
    for( auto v : g[u] )
        if( v != p )
            if( color[u] != color[v] && ( res[u] == 0 && res[v] == 0 ) ){
                res[u] = res[v] = ++cnt;
                q.push(u);
                q.push(v);
            }
}

void bfs(){
    while( !q.empty() ){
        int u = q.front();
        q.pop();
        for( auto v : g[u] ){
            if( !res[v] ){
                res[v] = res[u];
                q.push(v);
            }
        }
    }
}

int32_t main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
    cout.setf(ios::fixed); cout.precision(0);
    srand(time(NULL));

    //freopen("a.in","r",stdin);
    //freopen("a.in","w",stdout);

    cin >> n;

    for(int i=1; i<=n; i++){
        int u, v;
        cin >> u >> v;
        g[u].pb(v);
        g[v].pb(u);
    }

    dfs_color(1,1);

    dfs(1,1);

    bfs();

    for(int i=1; i<=n; i++)
        w[res[i]].pb(i);

    int min_ans = 0;
    for(int i=1; i<=cnt; i++){
            min_ans += ( ( (int)w[i].size() - 1 ) * 2 );
        for(int j=0; j<w[i].size(); j++){
            ans[w[i][j]] = w[i][(j+1)%w[i].size()];
        }
    }

    cout << min_ans << '\n';
    for(int i=1; i<=n; i++)
        cout << ans[i] << ' ';
    cout << '\n';

    return 0;
}

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_multi_set;

#define mod         1000000007
#define pi          3.1415926535898
#define eps         1e-9

#define fast       ios::sync_with_stdio(0); cin.tie(0);cout.tie(0)
#define fs          first
#define sc          second
#define pb         push_back
#define sp          printf(" ")
#define nl          '\n'
#define all(a) a.begin(),a.end()
#define unique(c) (c).resize(unique(all(c)) - (c).begin())
#define itn int

#define set0(a)     memset(a,0,sizeof(a))
#define setneg(a)   memset(a,-1,sizeof(a))
#define setinf(a)   memset(a,126,sizeof(a))

#define REP(i,start,end) for (int i = start; i <=end; i++)
#define RREP(i,end,start) for (int i=end; i>=start ;i--)

typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<LL> vll;
typedef vector<double> vd;
typedef vector<vector<LL> > matrix;
typedef vector<vector<int> > graph;
LL INF=1e9;

LL powm(LL x, LL y,LL m)
{
    if(y==0)
        return 1;
    if(y==1)
        return x%m;
    if(y&1)
        return (x*(powm((x*x)%m,y/2,m)))%m;
    return powm((x*x)%m,y/2,m)%m;
}

LL inv(LL x, LL m)
{
    return powm(x,m-2,m);
}

int add(int var1, int var2){
    return (((((LL)(var1)) % mod + (LL)(var2)) % mod) + mod) % mod;
}

int mul(int var1, int var2){
    return (((LL)(var1)) % mod * (LL)(var2)) % mod;
}

int ans=0;
vi arr;
graph adj;
int dfs(int v, int p=0)
{
    int f=1;
    for(auto to:adj[v])
    {
        if(to!=p)
        {
            int f1=dfs(to,v);
            if(f1==1)
            {
                ans+=2;
                swap(arr[to],arr[v]);
                f=0;
            }
        }
    }
    return f;
}
void solve()
{
    int n;
    cin>>n;
    arr.resize(n+1);
    iota(all(arr),0);
    adj.resize(n+1);
    REP(i,1,n-1)
    {
        int x,y;
        cin>>x>>y;
        adj[x].pb(y);
        adj[y].pb(x);
    }
    int f=dfs(1);
    if(f)
    {
        ans+=2;
        swap(arr[1],arr[adj[1][0]]);
    }
    cout<<ans<<nl;
    vi pos(n+1);
    REP(i,1,n)
        pos[arr[i]]=i;
    REP(i,1,n)
        cout<<pos[i]<<" ";
    cout<<nl;
}
int main()
{
    fast;
        solve();
}

//https://codeforces.com/contest/1387/problem/B1
#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define pb push_back
typedef long long int ll;
#define int ll
const int maxn = 1e5 + 5;
const int maxh = 20;
int n;
bitset<maxn> vis = 0;
vector<int> g[maxn];
int p[maxh][maxn] = { { 0 } };
int d[maxn] = { 0 };
int v[maxn] = { 0 };
int almost[maxn] = { 0 };

void dfs(int c, int pp = -1, int dd=0)
{
    if(pp>0)
    {
        p[0][c] = pp;
    }
    d[c] = dd;
    for(auto e : g[c])
    {
        if (e != pp)
        {
            if(g[e].size() >= 2)
            {
                almost[c]++;
            }
            dfs(e, c,dd+1);
        }
    }
}

void init()
{
    for(int h = 1; h < maxh; ++h)
    {
        for(int i = 1; i <= n; ++i)
        {
            p[h][i] = p[h-1][p[h-1][i]];
        }
    }
}

int raise(int b, int am)
{
    int h = 0;
    while(am)
    {
        if(!b) break;
        if(am%2)
        {
            b = p[h][b];
        }
        am >>= 1;
        ++h;
    }
    return b;
}

int lca(int a, int b)
{
    if(a==b) return a;
    if(d[a] > d[b])
    {
        swap(a,b);
    }
    b = raise(b, d[b] - d[a]);
    if(a==b) return a;
    for(int h = maxh - 1; h >= 0; --h)
    {
        if(p[h][b] != p[h][a])
        {
            b = p[h][b];
            a = p[h][a];
        }
    }
    return p[0][b];

}

void solve();
signed main()
{
    ios::sync_with_stdio(false); cin.tie(nullptr);
    cin >> n;
    for(int i = 0; i < n - 1; ++i)
    {
        int a, b; cin >> a >> b;
        g[a].pb(b);
        g[b].pb(a);
    }
    dfs(1);
    /*for(int i = 1; i <= n; ++i)
    {
        cout << almost[i] << " ";
    }
    cout << endl;*/ //v
    // init();
    solve();

    return 0;
}

void solve()
{
    ll cost = 0;
    queue<int> qal;
    int left = n;
    for(int i = 1; i <= n; ++i)
    {
        if(!almost[i] && (g[i].size() > 1 ||!p[0][i])) // almost a leaf (a claw)
        {
            qal.push(i);
        }
    }
    while(!qal.empty())
    {
        int claw = qal.front(); qal.pop();
        // cout << "N"<<claw<<endl;
        vector<int> good;
        for(auto e: g[claw])
        {
            if (e == p[0][claw]) continue;
            if(!vis[e])
            {
                vis[e] = true;
                good.pb(e);
            }
        }
        if(good.empty())
        {
            if(p[0][claw])
            {
                // cout << "P" << p[0][claw] << " " << almost[p[0][claw]] << endl;
                almost[p[0][claw]]--;
                if(!almost[p[0][claw]])
                {
                    qal.push(p[0][claw]);
                }
            }
            continue;
        }
        // cout << "C: " << claw << endl;
        vis[claw] = true;
        left -= good.size() + 1;
        if(left == 1)
        {
            // we have a claw with a crown, we can set the crown to be another leaf lmao
            good.push_back(p[0][claw]);
            vis[p[0][claw]] = true;
            cost += 2 * good.size();
            int next = claw;
            for(auto e : good)
            {
                v[next] = e;
                next = e;
            }
            v[next] = claw;
            left--;
            break;
        }
        else
        {
            cost += 2 * good.size();
            if(good.size() == 1)
            {
                v[good[0]] = claw;
                v[claw] = good[0];
            }
            else
            {
                int next = claw;
                for(auto e : good)
                {
                    v[next] = e;
                    next = e;
                }
                v[next] = claw;
            }
            if(p[0][claw])
            {
                // cout << "P" << p[0][claw] << " " << almost[p[0][claw]] << endl;
                almost[p[0][claw]]--;
                if(!almost[p[0][claw]])
                {
                    qal.push(p[0][claw]);
                }
            }

        }

    }
    cout << cost << endl;
    for(int i = 1; i <= n; ++i)
    {
        cout << v[i] << " ";
    }
    cout << endl;
}

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <utility>
#include <queue>

using namespace std;
typedef long long ll;
const int LEN = 100000;

struct fastio {
  int it, len;
  char s[LEN + 5];
  fastio() {
    it = len = 0;
  }
  char get() {
    if(it < len) return s[it++];
    it = 0, len = fread(s, 1, LEN, stdin);
    return len ? s[it++] : EOF;
  }
  bool notend() {
    char c;
    for (c = get();c == ' ' || c == '\n' || c == '\r';c = get());
    if (it) it--;
    return c != EOF;
  }
  void put(char c) {
    if(it == LEN) fwrite(s,1,LEN,stdout), it = 0;
    s[it++] = c;
  }
  void flush() {
    fwrite(s, 1, it, stdout);
  }
}buff, bufo;
inline int getint() {
  char c; int res = 0, sig = 1;
  for (c = buff.get(); c < '0' || c > '9'; c = buff.get()) if(c == '-') sig = -1;
  for (;c >= '0' && c <= '9';c = buff.get()) res = res * 10 + (c - '0');
  return sig * res;
}
inline ll getll() {
  char c; ll res = 0, sig = 1;
  for (c = buff.get(); c < '0' || c > '9'; c = buff.get()) if (c == '-') sig = -1;
  for (; c >= '0' && c <= '9'; c = buff.get()) res = res * 10 + (c - '0');
  return sig * res;
}
inline void putint(int x, char suf) {
  if (!x) bufo.put('0');
  else {
    if (x < 0) bufo.put('-'), x = -x;
    int k = 0; char s[15];
    while (x) {
      s[++k] = x % 10 + '0';
      x /= 10;
    }
    for (; k; k--) bufo.put(s[k]);
  }
  bufo.put(suf);
}
inline void putll(ll x, char suf) {
  if (!x) bufo.put('0');
  else {
    if (x < 0) bufo.put('-'), x = -x;
    int k = 0; char s[25];
    while (x) {
      s[++k] = x % 10 + '0';
      x /= 10;
    }
    for (; k; k--) bufo.put(s[k]);
  }
  bufo.put(suf);
}
inline char get_char() {
  char c;
  for (c = buff.get(); c == ' ' || c == '\n' || c == '\r'; c = buff.get());
  return c;
}
#define maxn 100005
#define mp make_pair
typedef pair<int, int> P;
int n, rt, deg[maxn];
vector<int> adj[maxn];
int findRt() {
  for (int i = 1; i <= n; i++) {
    for (auto v : adj[i]) {
      if (deg[v] == 1) return i;
    }
  }
}
int par[maxn], dep[maxn];
void dfs(int u, int f) {
  par[u] = f;
  dep[u] = dep[f] + 1;
  for (auto v : adj[u]) {
    if (v != f) dfs(v, u);
  }
}
bool del[maxn];
int ans[maxn], tot;
int main() {
  n = getint();
  for (int i = 1; i < n; i++) {
    int u = getint(), v = getint();
    adj[u].push_back(v);
    adj[v].push_back(u);
    ++deg[u], ++deg[v];
  }
  if (n == 2) {
    printf("2\n2 1\n");
    return 0;
  }
  rt = findRt();
  dfs(rt, 0);
  priority_queue<P> que;
  for (int i = 1; i <= n; i++) {
    if (deg[i] == 1) que.push(mp(dep[i], i));
  }
  while (!que.empty()) {
    P p = que.top(); que.pop();
    if (del[p.second]) continue;
    int u = par[p.second];
    vector<int> lis;
    for (auto v : adj[u]) {
      if (!del[v] && v != par[u]) lis.push_back(v);
    }
    tot += 2 * lis.size();
    lis.push_back(u);
    for (int i = 0; i + 1 < (int)lis.size(); i++) {
      ans[lis[i]] = lis[i + 1];
    }
    ans[lis.back()] = lis.front();
    for (auto v : lis) del[v] = true;
    if (par[u]) deg[par[u]]--;
    if (deg[par[u]] == 1) que.push(mp(dep[par[u]], par[u]));
  }
  putint(tot, '\n');
  for (int i = 1; i <= n; i++) putint(ans[i], i == n ? '\n' : ' ');
  bufo.flush();
  return 0;
}

#include <bits/stdc++.h>
#define N 1000005
#define ll long long int
#define MP make_pair
#define pb push_back
#define ppb pop_back
#define sp " "
#define endl "\n"
#define fi first
#define se second
#define ii pair<int,int>
#define lli pair<ll,ll>
#define fast cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(false)
#define fast2 freopen ("badhair.gir","r",stdin);freopen ("badhair.cik","w",stdout);
#define mod 1000000007
#define fs(x,y) for(ll i=1;i<=y;i++) cin>>x[i]
#define fo(i,x,y) for(ll i=x;i<=y;i++)
#define INF 1000000000005
#define ull unsigned long long int
using namespace std;

ll n,m,ar[N],sum,t,mark[N],tut[N];

vector<int> v[N];

void f(int ind,int back)
{
	for(int i=0;i<v[ind].size();i++)
	{
		if(v[ind][i]!=back)
			f(v[ind][i],ind);
	}
	if(mark[ind])
		return;
	if(ind==1)
	{
		sum+=2;
		swap(tut[1],tut[v[1][0]]);
		return;
	}
	mark[back]++;
	swap(tut[ind],tut[back]);
	sum+=2;
	return;
}

int main()
{
	fast;
	cin>>n;
	fo(i,1,n)
	tut[i]=i;
	fo(i,1,n-1)
	{
		int a,b;
		cin>>a>>b;
		v[a].pb(b);
		v[b].pb(a);
	}
	f(1,1);
	cout<<sum<<endl;
	fo(i,1,n)
	cout<<tut[i]<<sp;
}

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, x, y) for (int i = x; i < y; i++)
#define FORN(i, x, y) for (int i = x; i <= y; i++)
#define DEC(i, x, y) for (int i = x; i >= y; i--)
#define scan1(X) scanf("%d", &X)
#define scan2(X, Y) scanf("%d%d", &X, &Y)
#define scan3(X, Y, Z) scanf("%d%d%d", &X, &Y, &Z)
#define scan4(X, Y, Z, W) scanf("%d%d%d%d", &X, &Y, &Z, &W)
#define scanl1(X) scanf("%lld", &X);
#define scanl2(X, Y) scanf("%lld%lld", &X, &Y)
#define scanl3(X, Y, Z) scanf("%lld%lld%lld", &X, &Y, &Z)
#define scanl4(X, Y, Z, W) scanf("%lld%lld%lld%lld", &X, &Y, &Z, &W)
#define print1(X) printf("%d\n", X);
#define print2(X, Y) printf("%d %d\n", X, Y);
#define print3(X, Y, Z) printf("%d %d %d\n", X, Y, Z);
#define print4(X, Y, Z, W) printf("%d %d %d %d\n", X, Y, Z, W);
#define printl1(X) printf("%lld\n", X);
#define printl2(X, Y) printf("%lld %lld\n", X, Y);
#define printl3(X, Y, Z) printf("%lld %lld %lld\n", X, Y, Z);
#define printl4(X, Y, Z, W) printf("%lld %lld %lld %lld\n", X, Y, Z, W);
#define max3(X, Y, Z) max(X, max(Y, Z))
#define max4(X, Y, Z, W) max(max(X, Y), max(Z, W))
#define min3(X, Y, Z) min(X, min(Y, Z))
#define min4(X, Y, Z, W) min(min(X, Y), min(Z, W))
#define popcount(X) __builtin_popcount(X)
#define all(x) x.begin(), x.end()
#define size(X) (int)X.size()
#define mp make_pair
#define pb push_back
#define fr first
#define sc second
#define lc(x) 2 * x
#define rc(x) 2 * x + 1
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef pair<ld, ld> pldld;
const long long l_inf = numeric_limits<long long>::max();
const long long l_ninf = numeric_limits<long long>::min();
const int inf = numeric_limits<int>::max();
const int ninf = numeric_limits<int>::min();
const double pi = 1.00 * acos(-1.00);
const double eps = 1e-8;
void cio(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}
ll gcd(ll a, ll b){
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return (a * (b / gcd(a, b))); }
int sq( int x ) {return x * x;}
////////////////////////////////////////////////////////////////////Header
const int maxn = 1e5 + 5;
int n;
vector<int> graph[maxn];
int id[maxn];
int ans;

void dfs ( int u, int p ) {
    if ( p != -1 && size(graph[u]) == 1 ) {
        ans+=2;
        swap(id[u], id[p]);
        return;
    }
    for ( auto v : graph[u] ) if ( v != p ) {
        dfs(v, u);
    }
    if ( id[u] == u && p != -1 ) {
        ans += 2;
        swap(id[u], id[p]);
    }
}

void solve () {
    FORN(i, 1, n) id[i] = i;
    dfs(1, -1);
    if ( id[1] == 1 ) {
        for ( auto v : graph[1] ) {
            ans += 2;
            swap(id[1], id[v]);
            break;
        }
    }
    print1(ans);
    FORN(i, 1, n) printf("%d ", id[i]);
    printf("\n");
}

int main () {
    scan1(n);
    FORN(i, 1, n - 1) {
        int u, v;
        scan2(u, v);
        graph[u].pb(v);
        graph[v].pb(u);
    }
    solve();
}

#include<bits/stdc++.h>
#define pi 3.141592653589793238
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#define MOD 1000000007
#define INF 999999999999999999
#define pb push_back
#define ff first
#define ss second

#define mt make_tuple
#define ll long long
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>

using namespace __gnu_pbds;

typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
const int N = 2e5 + 1;
vector<ll> adj[N];
ll dp[N][2], arr[N];
void dfs(ll a, ll par){
    ll sum = 0;
    ll add = 0;    bool swapped = false;
    for(auto u : adj[a]){
        if(u != par){
            dfs(u, a);
            sum += dp[u][1];
            if(sum >= INF){
                sum = INF;
            }
            if(dp[u][1] == INF){
                add += 2;
                swapped = true;
            }
        }
    }
    if(sum >= INF){
        sum = INF;
    }
    if(swapped){
        dp[a][1] = add;
        dp[a][0] = sum;
        for(auto u : adj[a]){
            if(u != par){
                if(dp[u][1] == INF){
                    dp[a][1] += dp[u][0];
                    continue;
                }
                dp[a][1] += dp[u][1];
            }
        }
        //cout << a << " " << dp[a][1] << " " << dp[a][0] << endl;
        return;
    }

    dp[a][0] = sum;
    ll bestval = INF, bestnode = -1;
    for(auto u : adj[a]){
        if(u != par){
            //cout << "# " << a << " " << dp[a][1] << " " << dp[a][0] << endl;
            if(dp[a][1] > sum - dp[u][1] + min(dp[u][0], dp[u][1]) + 2){
                dp[a][1] = min(dp[a][1], sum - dp[u][1] + min(dp[u][0], dp[u][1]) + 2);
                bestnode = u;
            }
        }
    }
    //cout << a << " " << dp[a][0] << " " << dp[a][1] << endl;
}
void build(ll a, ll par, ll sw){
    //cout << "#### " << a << " " << par << " " << sw << endl;
    if(sw == 0){
        for(auto u : adj[a]){
            if(u != par){
                if(dp[u][1] == INF){
                    swap(arr[u], arr[a]);
                    build(u, a, 0);
                }
                else{
                    build(u, a, 1);
                }
            }
        }
        return;
    }
    ll sum = 0, sum0 = 0;
    ll add = 0;    bool swapped = false;
    for(auto u : adj[a]){
        if(u != par){

            sum += dp[u][1];
            sum0 += dp[u][0];
            if(dp[u][1] == INF){
                add += 2;
                swapped = true;
            }
        }
    }
    if(sum >= INF){
        sum = INF;
    }
    if(swapped){

        for(auto u : adj[a]){
            if(u != par){
                if(dp[u][1] == INF){
                    swap(arr[a], arr[u]);
                    build(u, a, 0);
                }
                else{
                    build(u, a, 1);
                }
            }

        }
        return;
    }

    ll bestval = INF, bestnode = -1;
    for(auto u : adj[a]){
        if(u != par){

            if(!swapped && dp[a][1] == sum - dp[u][1] + min(dp[u][0], dp[u][1]) + 2){
                swapped = true;
                ll swa = 0;

                swap(arr[a], arr[u]);
                build(u, a, 0);
            }
            else{
                build(u, a, 1);
            }
        }
    }
}
int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    fast;
    ll T = 1, i, j;

    //cin >> T;
    while (T--) {
        ll n;
        cin >> n;
        for(i = 1; i <= n; i++){
            arr[i] = i;
        }
        for(i =0 ; i < n - 1; i++){
            ll a, b;
            cin >> a >> b;
            adj[a].pb(b);
            adj[b].pb(a);
        }
        if(n == 1){
            cout << 0 << endl;
            cout << 1;
            return 0;
        }
        for(i = 1; i <= n; i++){
            dp[i][0] = INF;
            dp[i][1] = INF;
        }
        dfs(1, -1);
        build(1, -1, 1);
        cout << dp[1][1] << endl;
        for(i = 1; i <= n; i++){
            cout << arr[i] << " ";
        }
    }
    return 0;
}

#include <fstream>
#include <iostream>
#include <vector>
#include <set>
#include <list>
#include <map>
#include <queue>
#include <deque>
#include <string>
#include <stack>
#include <algorithm>
#include <utility>
#include <math.h>
using namespace std;
typedef long long int63;
#define point pair<int63,int63>
#define dpoint pair<double,double>
#define fort(s,e) for(int i = s; i < e; i++)
#define forto(s,e) for(int j = s; j < e; j++)
#define fortoo(s,e) for(int l = s; l < e; l++)
#define fortu(s,e) for(int ii = s; ii < e; ii++)
#define fortuu(s,e) for(int z = s; z < e; z++)
#define fortob(s,e) for(int j = e-1; j >= s; j--)
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int63 n;
	cin >> n;
	vector<set<int63>>v(n);
	fort(1, n) {
		int63 a, b;
		cin >> a >> b;
		a--;
		b--;
		v[a].insert(b);
		v[b].insert(a);
	}
	queue<int63>q;
	vector<int63>as(n,-1);
	int63 d = n*2;
	fort(0, n) {
		if (v[i].size() == 1) {
			q.push(i);
		}
	}
	while (q.size()) {
		int63 cr = q.front();
		q.pop();
		if (v[cr].size() == 0) {
			continue;
		}
		int63 h = *(v[cr].begin());
		vector<int63> s;
		for (auto it = v[h].begin(); it != v[h].end(); it++) {
			v[*it].erase(h);
			if (v[*it].size() == 1) {
				q.push(*it);
			}
			if (v[*it].size() == 0) {
				s.push_back(*it);
			}
		}
		v[h].clear();
		s.push_back(h);
		fort(0, s.size()) {
			as[s[i]] = s[(i + 1) % s.size()];
		}
		d -= 2;
	}
	cout << d << endl;
	fort(0, n) {
		cout << as[i] + 1 << ' ';
	}
	cout << endl;
	return 0;
}

#include <iostream>
#include <assert.h>
#include <algorithm>
#include <vector>
#include <set>
#include <string>
#include <queue>
#include <map>
#include <bits/stdc++.h>
#define initrand mt19937 mt_rand(time(0));
#define rand mt_rand()
using namespace std;
//initrand;
const int N = 1e5 + 5;
vector<int> adj[N];
int lbl[N];
int cost = 0;
bool f (int i, int x){
    bool wecon = false;
    for(int j: adj[i]){
        if(j==x) continue;
        bool con = f(j, i);
        if(con){
            wecon = true;
            cost+=2;
            swap(lbl[i], lbl[j]);
        }
    }
    if(!wecon){
        if(i!=1) return true;
        cost+=2;
        swap(lbl[i], lbl[adj[i][0]]);
        return false;
    }
    else return false;
}
int main(){
    int n;
    cin>>n;
    for(int i = 0;i<=n;i++) lbl[i] = i;
    for(int i = 1;i<n;i++){
        int u, v;
        cin>>u>>v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    f(1, 0);
    cout<<cost<<'\n';
    int lbl1[n+1];
    for(int i = 1;i<=n;i++) lbl1[lbl[i]] = i;
    for(int i = 1;i<=n;i++) cout<<lbl1[i]<<' ';
    cout<<'\n';
}

#include <bits/stdc++.h>
using namespace std;

#define endl "\n"

const int N = 1e5 + 10, inf = 1e9;

vector < int > graph[N], distribution(N, 0);

int ans = 0;

int dfs(int root, int par){

    vector < int > thisone{root};

    for(auto x : graph[root]){
        if(par == x) continue;

        int resultant = dfs(x, root);
        if(resultant != inf){
            thisone.push_back(resultant);
        }
    }

    int a, b;

    if(thisone.size() > 1){
        ans -= 2;
        for(int i = 0;i < thisone.size();++i){
            distribution[thisone[i]] = thisone[(i + 1) % thisone.size()];
            ans += 2;
        }
    }

    return thisone.size()==1?root:inf;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int n, a, b;
    cin >> n;

    for(int i = 0;i < n - 1;++i){

        cin >> a >> b;
        graph[a].push_back(b);
        graph[b].push_back(a);
    }

    int root = 1;
    for(int i = 1;i <= n;++i){
        distribution[i] = i;
        if(graph[i].size() == 1){
            root = i;
        }
    }

    int resultant = dfs(root, -1);

    if(resultant != inf){
        a = graph[root].back();
        b = distribution[a];

        distribution[root] = b;
        distribution[a] = root;
        ans += 2;
    }

    cout << ans << endl;
    for(int i = 1;i <= n;++i)
        cout << distribution[i] << endl;

}

#include<bits/stdc++.h>
using namespace std;
template<typename __readdatatype>
void read(__readdatatype &x){
	x=0;char c=getchar();for(;!isdigit(c);c=getchar());
	for(;isdigit(c);c=getchar())x=x*10+c-'0';
}
template<typename __writedatatype>
void write(__writedatatype x){
	if(x>=10)write<__writedatatype>(x/10);
	putchar(x%10+'0');
}
const int N=1e5+100;
int n,du[N];
vector<int>e[N];
namespace MIN{
	int ans,can[N];
	int a[N];
	void dfs(int u,int fa){
		vector<int>p;
		for(auto v:e[u])
			if(v!=fa){
				dfs(v,u);
				if(!a[v])p.push_back(v);
			}
		if(!p.size())return;
		for(int i=0;i<p.size()-1;i++)a[p[i]]=p[i+1];
		a[p.back()]=u,a[u]=p.front(),ans+=2*p.size();
	}
	void work(){
		dfs(1,0);
		if(!a[1])a[1]=a[e[1][0]],a[e[1][0]]=1,ans+=2;
	}
}
signed main(){
	//freopen("village.in","r",stdin);
	//freopen("village.out","w",stdout);
	read(n);
	for(int i=1,u,v;i<n;i++)
		read(u),read(v),du[u]++,du[v]++,
		e[u].push_back(v),e[v].push_back(u);
	MIN::work();
	//MAX::work();
	write(MIN::ans);
	putchar('\n');
	for(int i=1;i<=n;i++)
		write(MIN::a[i]),putchar(' ');
}

#include "bits/stdc++.h"
//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,fma,sse2")
//#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
//#undef LOCALKL
#define IO \
ios_base::sync_with_stdio(0);(cin).tie(0);(cout).tie(0)
#define y1 _y1__
#define prev _prev__
#define all(a) (a).begin(),(a).end()
#define pb push_back
#define eb emplace_back
#define F first
#define S second
#ifdef LOCALKL
#define cerr cerr<<"\33[1;32m"
#define cout cout<<"\33[0m"
#else
#define endl '\n'
#define cerr if(1){}else cerr
#endif
#define OK cout<<"OK\n"<<endl;
#define setpre(k) fixed<<setprecision(k)
#define mmset(k,y) memset(k,y,sizeof(k))
#define sz(x) ((int)(x).size())
using pii = pair<int,int>;
using pll = pair<long long,long long>;
using ull = unsigned long long;
using intt = long long;
using ll = long long;
using ld = long double;

const ll m9 = 998244353;
const ll m7 = 1000000007;
const ll m18 = 1000000000000000000;
const ll i127 = 2139062143;
const ll l127 = 9187201950435737471;
vector<int>g[200001];

int n,res[200001],x,y,z,cnt,c[200001],dis,mdis,fur,ans,pos;

int dfs(int v,int prev)
{
    vector<int>vc;
    for(int& u : g[v])
        if(prev!=u)
            if(dfs(u,v))
                vc.eb(u);
    if(vc.empty()) return 1;
    //cerr<<v<<' '<<sz(vc)<<endl;
    ans+=2*sz(vc);
    vc.eb(v);
    vc.eb(vc[0]);
    for(int i=0;i<sz(vc)-1;i++)
        res[vc[i]]=vc[i+1];
    return 0;
}

int main()
{
    IO;
    cin>>n;
    cnt=n;
    for(int i=1;i<n;i++)
    {
        cin>>x>>y;
        g[x].eb(y);
        g[y].eb(x);
    }
    for(int i=1;i<=n;i++)
        if(g[i].size()==1)
        {
            dfs(g[i][0],0);
            break;
        }
    cout<<ans<<endl;
    for(int i=1;i<=n;i++) cout<<res[i]<<' ';

    return 0;
}

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

const int N = (int)1e5 + 10;
vector<int> T[N];
bool kill[N];
int deg[N];
int idx[N];
int p[N];

int main(){
    fastIO;
    int n;
    cin >> n;
    int u, v;
    for(int i = 1; i < n; i ++ ){
        cin >> u >> v;
        T[u].push_back(v);
        T[v].push_back(u);
        deg[u] ++ ;
        deg[v] ++ ;
    }
    int sol = 0;
    queue<int> qq;
    for(int i = 1; i <= n; i ++ ){
        if(deg[i] == 1){
            qq.push(i);
        }
        idx[i]=i;
    }
    int node;
    while(!qq.empty()){
        node = qq.front();
        qq.pop();
        for(auto x : T[node]){
            if(!kill[x]){
                deg[x]--;
                if(deg[x]==1)
                    qq.push(x);
            }
        }
        kill[node]=true;
        if(idx[node]!=node) continue;
        for(auto x : T[node]){
            if(!kill[x]){
                swap(idx[node], idx[x]);
                sol += 2;
                break;
            }
        }
        if(idx[node]!=node) continue;
        for(auto x : T[node]){
            swap(idx[node], idx[x]);
            sol += 2;
            break;
        }
    }
    for(int i = 1; i <= n; i ++ )
        p[idx[i]] = i;
    cout << sol << "\n";
    for(int i = 1; i <= n; i ++ ){
        cout << p[i] << " ";
    }
    cout << "\n";
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
vector<int> adjList[100005];
vector<int> forest[100005];
int n;
int leaf[1000005];
int p[100005];
int total=0;
void dfs(int u,int e)
{
    for(int v: adjList[u]){
        if(v==e) continue;
        p[v]=u;
        dfs(v,u);
    }
    if(leaf[u]==1 && u!=1){
        forest[u].push_back(e);
        forest[e].push_back(u);
        total+=2;
        leaf[u]=0;
        leaf[e]=-1;
        if(leaf[ p[e] ]!=-1)
            leaf[ p[e] ]=1;
    }
}
vector<int> idx;
int color[100005];
void dfsVisit(int u)
{
    color[u]=1;
    idx.push_back(u);
    for(int v: forest[u]){
        if(color[v]!=0) continue;
        dfsVisit(v);
    }
}
int answer[100005];
void moveIDs()
{
    for(int i=1;i<=n;i++){
        if(color[i]==0){
            idx.clear();
            dfsVisit(i);
            for(int j=0;j<idx.size();j++){
                answer[ idx[j] ]=idx[(j+1)%idx.size()];
            }
        }
    }
}
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    cin>>n;
    for(int i=1;i<=n-1;i++){
        int a,b;
        cin>>a>>b;
        adjList[a].push_back(b);
        adjList[b].push_back(a);
    }
    for(int i=2;i<=n;i++){
        if(adjList[i].size()==1)
            leaf[i]=1;
    }
    dfs(1,0);
    if(forest[1].size()==0){
        forest[1].push_back(adjList[1][0]);
        total+=2;
    }

    moveIDs();
    cout<<total<<'\n';
    for(int i=1;i<=n;i++){
        cout<<answer[i]<<" ";
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
vector<long long>v[100005];
long long dp[100005][2],fin[100005];
long long cat[100005][2];
long long cine[100005];
//long long towhere[100005];
int fel[100005];
struct ura
{
    long long d0,d1,pz;
};
vector<ura>vc;
bool cmp (ura a, ura b)
{
    return a.d1-a.d0<b.d1-b.d0;
}
void dfs1(int pz, int t)
{
    int felc=fel[pz];
    int catc=cat[pz][felc];
    vc.clear();
    for(long long i=0;i<v[pz].size();++i)
    {
        if(v[pz][i]==t)
            continue;
        long long nn=v[pz][i];
        vc.push_back({dp[nn][0],dp[nn][1],nn});
    }
    sort(vc.begin(),vc.end(),cmp);
    for(int i=0;i<catc;++i)
        fel[vc[i].pz]=1;
    for(long long i=0;i<v[pz].size();++i)
    {
        if(v[pz][i]==t)
            continue;
        dfs1(v[pz][i],pz);
    }
    vc.clear();
    for(long long i=0;i<v[pz].size();++i)
    {
        if(v[pz][i]==t)
            continue;
        long long nn=v[pz][i];
        vc.push_back({dp[nn][0],dp[nn][1],nn});
    }
    sort(vc.begin(),vc.end(),cmp);
    if(felc==1)
    {
        if(catc)
        {
            cine[pz]=cine[vc[0].pz];
            fin[vc[catc-1].pz]=pz;
            for(int i=catc-2;i>=0;--i)
                fin[vc[i].pz]=cine[vc[i+1].pz];
        }
        else
            cine[pz]=pz;
    }
    else
    {
        fin[pz]=cine[vc[0].pz];
        fin[vc[catc-1].pz]=pz;
        for(int i=catc-2;i>=0;--i)
            fin[vc[i].pz]=cine[vc[i+1].pz];
    }
}
void dfs (long long pz, long long t)
{
    dp[pz][0]=dp[pz][1]=999999999;
    for(long long i=0;i<v[pz].size();++i)
    {
        if(v[pz][i]==t)
            continue;
        dfs(v[pz][i],pz);
    }
    vc.clear();
    for(long long i=0;i<v[pz].size();++i)
    {
        if(v[pz][i]==t)
            continue;
        long long nn=v[pz][i];
        vc.push_back({dp[nn][0],dp[nn][1],nn});
    }
    sort(vc.begin(),vc.end(),cmp);
    long long sc=0;
    for(int i=0;i<vc.size();++i)
        sc+=vc[i].d0;
    if(vc.size()==0)
    {
        dp[pz][0]=999999999;
        dp[pz][1]=0;
        return;
    }
    dp[pz][1]=sc;
    cine[pz]=pz;
    for(int i=0;i<vc.size();++i)
    {
        sc+=vc[i].d1-vc[i].d0;
        long long vlc=sc+(i+1)*2LL;
        if(vlc<dp[pz][0])
        {
            dp[pz][0]=vlc;
            cat[pz][0]=i+1;
        }
        if(vlc<dp[pz][1])
        {
            dp[pz][1]=vlc;
            cat[pz][1]=i+1;
            cine[pz]=vc[0].pz;
        }
    }
}
int main()
{
    long long n,m,i,j,x,y,k,t,l,r;
    cin>>n;
    for(i=1;i<n;++i)
    {
        long long a,b;
        cin>>a>>b;
        v[a].push_back(b);
        v[b].push_back(a);
    }
    dfs(1,-1);
    cout<<dp[1][0]<<'\n';
    dfs1(1,-1);
    for(i=1;i<=n;++i)
        cout<<fin[i]<<' ';
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios::sync_with_stdio(false); cin.tie(nullptr);

    size_t n;
    cin >> n;

    vector<vector<size_t>> graph(n);
    for(size_t i = 0; i < n - 1; i++)
    {
        size_t u, v;
        cin >> u >> v; u--; v--;
        graph[u].push_back(v);
        graph[v].push_back(u);
    }

    vector<size_t> leaves(n);
    for(size_t u = 0; u < n; u++)
        for(auto v : graph[u])
            if(graph[v].size() == 1)
                leaves[u]++;

    queue<size_t> Q;

    auto sat = [&](size_t u) {
        return leaves[u] and graph[u].size() - leaves[u] <= 1;
    };

    auto rem = [&](size_t u, size_t v) {
        graph[u].erase(find(graph[u].begin(), graph[u].end(), v));
        graph[v].erase(find(graph[v].begin(), graph[v].end(), u));
        if(graph[v].size() == 1)
        {
            leaves[graph[v][0]]++;
            if(sat(graph[v][0]))
                Q.push(graph[v][0]);
        }
        else if(sat(v))
            Q.push(v);
    };

    for(size_t u = 0; u < n; u++)
        if(sat(u))
            Q.push(u);

    size_t result = 0;
    vector<size_t> to(n, SIZE_MAX);
    size_t m = n;
    while(m)
    {
        size_t par;
        while(true)
        {
            par = Q.front(); Q.pop();
            if(to[par] == SIZE_MAX)
                break;
        }

        size_t notleaf = SIZE_MAX;
        for(auto u : graph[par])
            if(graph[u].size() > 1)
                notleaf = u;

        if(notleaf != SIZE_MAX)
            rem(par, notleaf);

        size_t last = par;
        graph[par].push_back(par);
        for(auto u : graph[par])
        {
            result += (last == par or u == par ? 1 : 2);
            to[last] = u;
            last = u;
        }
        m -= graph[par].size();
    }

    cout << result << endl;
    for(size_t i = 0; i < n; i++)
        cout << to[i]+1 << ' ';
    cout << endl;
}

/*Code by : Harry_Singh*/
#include<bits/stdc++.h>
#define int long long
#define io ios_base::sync_with_stdio(false),cin.tie(nullptr);
#define ipair pair<int,int>
#define pb push_back
#define f(i,n) for(int i=0;i<n;i++)
#define F(i,n) for(int i=1;i<=n;i++)
#define mod 1000000007
using namespace std;
#define inf 100000000000000009
#define tr cout<<"\n"
// directions
int xx[5]={0,0,1,-1};
int yy[5]={1,-1,0,0};
int dp[1000005]={0};
int sum=0;
vector<int> G[100005];
void dfs(int s,int p)
{
   for(int child:G[s])
   {
   	if(p!=child)
   	{
   		dfs(child,s);
   	}
   }

if(dp[s]==s)swap(dp[p],dp[s]),sum+=2;

if(dp[s]==s)
{
	// some of its immidiate children
	for(int child:G[s])
   {
   	    if(child!=p){swap(dp[s],dp[child]);break;}
   }

}

}

void we_have_choices()
{

int n;
cin>>n;
F(i,n)dp[i]=i;
F(i,n-1)
{
	int u,v;
	cin>>u>>v;
	G[u].pb(v);
	G[v].pb(u);
}
dfs(1,1);
cout<<sum;tr;
F(i,n)cout<<dp[i]<<" ";
tr;

}

int32_t main()
{
#ifndef ONLINE_JUDGE
 freopen("input.txt","r",stdin);
 freopen("op.txt","w",stdout);
#endif
    io;
    int t=1;while (t--)we_have_choices();
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define x first
#define y second
typedef pair<int,int> pii;
typedef long long ll;
typedef unsigned long long ull;
template <typename T> void chkmax(T &x,T y){x<y?x=y:T();}
template <typename T> void chkmin(T &x,T y){x>y?x=y:T();}
template <typename T> void readint(T &x)
{
	x=0;int f=1;char c;
	for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-1;
	for(;isdigit(c);c=getchar())x=x*10+c-'0';
	x*=f;
}
/*const int MOD=998244353;
inline int dmy(int x){return x>=MOD?x-MOD:x;}
inline void inc(int &x,int y){x=dmy(x+y);}
int qmi(int x,int y)
{
	int ans=1;
	for(;y;y>>=1,x=1ll*x*x%MOD)
		if(y&1)ans=1ll*ans*x%MOD;
	return ans;
}*/
const int MAXN=100005;

int n;
vector<int> G[MAXN],b[MAXN];
ll f[MAXN],g[MAXN];//f select
int fa[MAXN],res[MAXN],to[MAXN];
int getfa(int u){return u==fa[u]?u:fa[u]=getfa(fa[u]);}
void dfs1(int u,int pa)
{
	int t=1<<30;
	for(auto v:G[u])
	{
		if(v==pa)continue;
		dfs1(v,u);
		g[u]+=f[v];
		f[u]+=min(g[v]+1,f[v]);
		int s=min(g[v],f[v])+1-min(g[v]+1,f[v]);
		if(s<t)to[u]=v,t=s;
	}
	f[u]+=t;
//cerr<<u<<": "<<f[u]<<" "<<g[u]<<" "<<to[u]<<endl;
}
void dfs2(int u,int pa,int ty)
{
	for(auto v:G[u])
	{
		if(v==pa)continue;
		if(ty==0)dfs2(v,u,1);
		else
		{
			if(v==to[u] || g[v]+1<=f[v])fa[getfa(u)]=getfa(v);
			if(g[v]+1<=f[v] || (v==to[u] && g[v]<=f[v]))dfs2(v,u,0);
			else dfs2(v,u,1);
		}
	}
}

int main()
{
	#ifndef ONLINE_JUDGE
	freopen("code.in","r",stdin);
//	freopen("code.out","w",stdout);
	#endif
	int u,v;
	readint(n);
	for(int i=1;i<n;++i)readint(u),readint(v),G[u].pb(v),G[v].pb(u);
	for(int i=1;i<=n;++i)fa[i]=i;
	dfs1(1,-1);
	dfs2(1,-1,1);
	for(int i=1;i<=n;++i)b[getfa(i)].pb(i);
	for(int i=1;i<=n;++i)
		if(getfa(i)==i)
		{
			for(int j=1;j<b[i].size();++j)
				res[b[i][j]]=b[i][j-1];
			res[b[i][0]]=b[i].back();
		}
	printf("%d\n",2*f[1]);
	for(int i=1;i<=n;++i)printf("%d ",res[i]);
	return 0;
}

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
/* #pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")*/
typedef long long ll;
const ll MAXN = 2e5 + 2;
const ll MAXK = 1e3 + 5;
const ll MOD = 1e9 + 7;
const ll MODH = 1e9 + 9;
const int MAXLG = 20;
const long double PI = acos(-1);
const ll p = 41;
const long double EPS = 1e-4;
#define x first
#define y second
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline ll rand(ll a, ll b) {ll c = rng(); return a+((ll)abs(c))%(b-a+1);}
ll binpow(ll a, ll b, ll m) {a %= m;ll res = 1;while (b > 0) {if (b & 1){res = res * a % m;}a = a * a % m;b >>= 1;
}return res;}

int a[MAXN];
int par[MAXN];
vector<int> g[MAXN];
ll Ans = 0;
int swp[MAXN];
int ans[MAXN];
int sz[MAXN];
void dfs(int u,int p)
{
    sz[u] = 1;
    for(auto &k : g[u])
	{
	    if(k == p)
	      continue;
		 dfs(k,u);
		 sz[u]+=sz[k];

	}
}

void dfs2(int u,int p)
{
    par[u] = p;
	for(auto &k : g[u])
	{
	    if(k == p)
	      continue;
	       dfs2(k,u);
	    if(!swp[u] and !swp[k])
		        {
		            swp[u] = swp[k] = 1;
		            ans[u] = k;
		            ans[k] = u;
		            Ans+=2;
		        }

	}

}
bool cmp(int a,int b)
{
    return sz[a] < sz[b];
}
int main() {

  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

int n;
cin >> n;
for(int i = 0 ; i < n - 1 ; i++)
{
	int u,v;
	cin >> u >> v;
	g[u].push_back(v);
	g[v].push_back(u);
}
for(int i = 1 ; i <= n ; i++)
   ans[i] = i;

dfs(1,0);
for(int i = 1 ; i <= n ; i++)
  if(g[i].size())
   sort(g[i].begin(),g[i].end(),cmp);
dfs2(1,0);
par[1] = g[1].back();
for(int i = n ; i >= 1 ; i--)
{
    if(swp[i] ^ 1)
    {
        Ans+=2;
        int u = ans[i];
        int v = ans[par[i]];
        ans[i] = v;
        ans[par[i]] = u;
    }

}

cout << Ans << '\n';

for(int i = 1 ; i <= n ; i++)
   cout << ans[i] << ' ';

}

#include <bits/stdc++.h>
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define ll __int128
//#define ll long long
#define int long long
//#define int __int128
#define LEFT(a) ((a)<<1)
#define RIGHT(a) (LEFT(a) + 1)
#define MID(a,b) ((a+b)>>1)
#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))
#define y1 y122

/*
#pragma GCC optimize ("O3")
#pragma GCC optimize("Ofast")

#pragma GCC target("avx2,fma")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC target ("avx2")
#pragma GCC optimization ("unroll-loops")

#pragma comment(linker, "/STACK: 20000000005")
*/

using namespace std;

const int N = 200005, INF = 1e11;

int n;
vector < int > g[N];
vector < pair < int, int > > V[N];
int DP[N], dp[N];
int ans[N];

void dfs (int k, int p){
    int sum = 0;
    DP[k] = dp[k] = INF;
    for (int to : g[k]){
        if (to == p)
            continue;
        dfs (to, k);
        sum += dp[to];
        V[k].pb ({DP[to] - dp[to], to});
    }
    sort (V[k].begin(), V[k].end());
    DP[k] = min (DP[k], sum);
    for (int i = 0; i < V[k].size(); i++){
        sum += V[k][i].F;
        //if (i % 2 == 0 || i >= 3)
            dp[k] = min (dp[k], i*2 + 2 + sum);
        if (i % 2 == 1 || i >= 2)
            DP[k] = min (DP[k], i*2 + 2 + sum);
    }
    DP[k] = min (DP[k], dp[k]);
}

void go (int k, int p, int T){
    int sum = 0;
    for (int to : g[k])
        if (to != p)
            sum += dp[to];
    if (T == 1 && DP[k] == dp[k])
        T = 0;
    if (T == 0){
        for (int i = 0; i < V[k].size(); i++){
            sum += V[k][i].F;
            if (dp[k] != i*2 + 2 + sum)
                continue;
            for (int j = 0; j < V[k].size(); j++){
                if (j <= i)
                    go (V[k][j].S, k, 1);
                else
                    go (V[k][j].S, k, 0);
            }
            ans[k] = ans[V[k][0].S];
            for (int j = 0; j < i; j++)
                ans[V[k][j].S] = ans[V[k][j+1].S];
            ans[V[k][i].S] = k;
            return;
        }
    }
    else {
        if (DP[k] == sum){
            for (int to : g[k])
                if (to != p)
                    go (to, k, 0);
            return;
        }
        for (int i = 0; i < V[k].size(); i++){
            sum += V[k][i].F;
            if (i == 0)
                continue;
            if (DP[k] != i*2 + 2 + sum)
                continue;
            for (int j = 0; j < V[k].size(); j++){
                if (j <= i)
                    go (V[k][j].S, k, 1);
                else
                    go (V[k][j].S, k, 0);
            }
            int las = ans[V[k][0].S];
            for (int j = 0; j < i; j++){
                ans[V[k][j].S] = ans[V[k][j+1].S];
            }
            ans[V[k][i].S] = las;
            return;
        }
    }
}

main()
{
    //freopen ("in.in", "r", stdin);freopen ("out.out", "w", stdout);
    ios_base::sync_with_stdio(0);
    cin >> n;
    for (int i = 1; i <= n; i++)
        ans[i] = i;
    for (int u, v, i = 1; i < n; i++){
        cin >> u >> v;
        g[u].pb (v);
        g[v].pb (u);
    }
    dfs (1, 0);
    cout << dp[1] << endl;
    go (1, 0, 0);
    for (int i = 1; i <= n; i++)
        cout << ans[i] << " ";
}

#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
typedef long long int LLI;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector<pii> vpii;

vi adjList[100000];
LLI dp[100000][2];
int doDFS(int u,int p) {
    int i;
    LLI m = 1e9;
    for (i = 0; i < adjList[u].size(); i++) {
        int v = adjList[u][i];
        if (v != p) {
            doDFS(v,u);
            dp[u][0] += dp[v][1];
            dp[u][1] += min(dp[v][0]+1,dp[v][1]);
            m = min(m,min(dp[v][0],dp[v][1])+1-min(dp[v][0]+1,dp[v][1]));
        }
    }
    dp[u][1] += m;
    dp[u][0] = min(dp[u][0],(LLI) 1e9),dp[u][1] = min(dp[u][1],(LLI) 1e9);
    return 0;
}
vi adjList2[100000];
int getAns(int u,int p,int t) {
    int i;
    if (t == 0) {
        for (i = 0; i < adjList[u].size(); i++) {
            int v = adjList[u][i];
            if (v != p) getAns(v,u,1);
        }
    }
    else {
        LLI m = 1e9;
        for (i = 0; i < adjList[u].size(); i++) {
            int v = adjList[u][i];
            if (v != p) m = min(m,min(dp[v][0],dp[v][1])+1-min(dp[v][0]+1,dp[v][1]));
        }
        m = max(m,0LL);
        for (i = 0; i < adjList[u].size(); i++) {
            int v = adjList[u][i];
            if (v != p) {
                if (min(dp[v][0],dp[v][1])+1-min(dp[v][0]+1,dp[v][1]) <= m) {
                    adjList2[u].pb(v);
                    adjList2[v].pb(u);
                    getAns(v,u,dp[v][1] < dp[v][0]);
                    m = 0;
                }
                else getAns(v,u,dp[v][1] < dp[v][0]+1);
            }
        }
    }
    return 0;
}
vi order;
int visited[100000];
int doDFS2(int u,int p) {
    int i;
    order.pb(u),visited[u] = 1;
    for (i = 0; i < adjList2[u].size(); i++) {
        int v = adjList2[u][i];
        if (v != p) doDFS2(v,u);
    }
    return 0;
}
int ans[100000];
int main() {
    int i;
    int N,a,b;
    scanf("%d",&N);
    for (i = 0; i < N-1; i++) {
        scanf("%d %d",&a,&b);
        a--,b--;
        adjList[a].pb(b);
        adjList[b].pb(a);
    }

    int j;
    doDFS(0,-1),printf("%lld\n",2*dp[0][1]),getAns(0,-1,1);
    for (i = 0; i < N; i++) {
        if (!visited[i]) {
            doDFS2(i,-1);
            for (j = 0; j < order.size(); j++) ans[order[j]] = order[(j+1) % order.size()];
            order.clear();
        }
    }
    for (i = 0; i < N; i++) printf("%d ",ans[i]+1);
    printf("\n");

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;

int main() {
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    int N; cin >> N;
    vvi tree(N);
    for (int i = 0; i < N - 1; ++i) {
        int a, b; cin >> a >> b; a--; b--;
        tree[a].push_back(b);
        tree[b].push_back(a);
    }
    int min = 0, max = 0; vi min_v(N), max_v(N);
    for (int i = 0; i < N; ++i) min_v[i] = max_v[i] = i;
    vi degree(N);
    queue<int> q;
    for (int u = 0; u < N; ++u) if ((degree[u] = tree[u].size()) == 1) q.push(u);
    while (!q.empty()) {
        int u = q.front(); q.pop();
        int n = tree[u][0];
        for (int v : tree[u]) if (degree[v] > 0) n = v;
        if (min_v[u] == u) {
            swap(min_v[u], min_v[n]);
            min += 2;
        }
        if (degree[n] > 0) {
            degree[u] = 0;
            if (--degree[n] == 1) q.push(n);
        }
    }
    cout << min << endl;
    for (int u = 0; u < N; ++u) cout << min_v[u] + 1 << " ";
    cout << endl;
}

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/priority_queue.hpp>
//#pragma GCC optimize("Ofast") //#pragma GCC optimize "unroll-loops" //#pragma GCC target "sse,sse2,sse3,sse4,abm,avx,avx2,fma,mmx,popcnt,tune=native" //#pragma GCC optimize "prefetch-loop-arrays"
using namespace std; using namespace __gnu_pbds;
#define foru(i,a,b) for(int i=(a);i<(b);i++)
#define ford(i,a,b) for(int i=(a);i>=(b);i--)
#define fori(a,b) foru(i,a,b)
#define forj(a,b) foru(j,a,b)
#define fork(a,b) foru(k,a,b)
#define seto(x,i) memset(x,i,sizeof x)
#define pf first
#define ps second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define popcount __builtin_popcount
#define popcountll __builtin_popcountll
#define clz __builtin_clz
#define clzll __builtin_clzll
#define ctz __builtin_ctz
#define ctzll __builtin_ctzll
#define P2(x) (1LL<<(x))
#define sz(x) (int)x.size()
#define all(x) begin(x),end(x)
#if __SIZEOF_INT128__
    typedef __int128_t i128; typedef __uint128_t ui128;
#else
    typedef int64_t i128; typedef uint64_t ui128;
#endif
typedef int64_t ll;  typedef uint64_t ull; typedef int8_t byte; typedef long double lld;
typedef pair<int,int> pii; typedef pair<ll,ll> pll; typedef pair<lld,lld> pdd;
template<class T1,class T2> using ordered_map=tree<T1,T2,less<T1>,rb_tree_tag,tree_order_statistics_node_update>; template<class T1> using ordered_set=ordered_map<T1,null_type>;
template<class T> using minpq=std::priority_queue<T,vector<T>,greater<T>>; template<class T> using maxpq=std::priority_queue<T,vector<T>,less<T>>;
template<class T> using minpairingheap=__gnu_pbds::priority_queue<T,greater<T>,pairing_heap_tag>; template<class T>using maxpairingheap=__gnu_pbds::priority_queue<T,less<T>,pairing_heap_tag>;
const int inf=0x3f3f3f3f,MOD=1e9+7; const ll INF=0x3f3f3f3f3f3f3f3f; const lld PI=acos((lld)-1);
const ll SEED=443214^chrono::duration_cast<chrono::nanoseconds>(chrono::high_resolution_clock::now().time_since_epoch()).count();
mt19937 randgen(SEED); int randint(int a, int b){return uniform_int_distribution<int>(a,b)(randgen);} ll randll(ll a, ll b){return uniform_int_distribution<ll>(a,b)(randgen);}
ll gcd(ll a, ll b){return b?gcd(b,a%b):a;}
ll fpow(ll a,ll b){if(!b) return 1; ll t=fpow(a,b/2); if(b&1) return t*t%MOD*a%MOD; return t*t%MOD;}
template<class T1,class T2>constexpr const auto _min(const T1&x,const T2&y){return x<y?x:y;} template<class T,class...Ts>constexpr auto _min(const T&x,const Ts&...xs){return _min(x,_min(xs...));}
template<class T1,class T2>constexpr const auto _max(const T1&x,const T2&y){return x>y?x:y;} template<class T,class...Ts>constexpr auto _max(const T&x,const Ts&...xs){return _max(x,_max(xs...));}
#define min(...) _min(__VA_ARGS__)
#define max(...) _max(__VA_ARGS__)
struct chash{
    static ll splitmix64(ll x){x+=0x9e3779b97f4a7c15; x=(x^(x>>30))*0xbf58476d1ce4e5b9; x=(x^(x>>27))*0x94d049bb133111eb; return x^(x>>31);}
    template<class T> size_t operator()(const T &x) const{return splitmix64(hash<T>()(x)+SEED);}
    template<class T1,class T2> size_t operator()(const pair<T1,T2>&x)const{return 31*operator()(x.first)+operator()(x.second);}};
void fileIn(string s){freopen(s.c_str(),"r",stdin);} void fileOut(string s){freopen(s.c_str(),"w",stdout);} void fileIO(string s){fileIn(s+".in"); fileOut(s+".out");}
string to_string(char c){return string(1,c);} string to_string(char* s){return (string)s;} string to_string(string s){return s;}
template<class T> string to_string(complex<T> c){stringstream ss; ss<<c; return ss.str();} template<class T1,class T2> string to_string(pair<T1,T2> p){return "("+to_string(p.pf)+","+to_string(p.ps)+")";}
template<size_t SZ> string to_string(bitset<SZ> b){string ret=""; fori(0,SZ) ret+=char('0'+b[i]); return ret;}
template<class T> string to_string(T v){string ret; for(const auto& x:v) ret+=","+to_string(x); ret[0]='{'; return ret+"}";}
void DBG(){cerr<<"]"<<endl;} template<class T,class... Ts> void DBG(T x,Ts... xs){cerr<<to_string(x); if(sizeof...(xs)) cerr<<", "; DBG(xs...);}
#ifdef LOCAL_PROJECT
    #define dbg(...) cerr<<"Line("<< __LINE__<<") -> ["<<#__VA_ARGS__<<"]: [", DBG(__VA_ARGS__)
#else
    #define dbg(...) 0
#endif
#define nl "\n"

const int N=100010,M=MOD;
int n,a,b,dp[N][2],d[N],ans[N];
vector<int> gr[N];
void dfs(int v,int p=0){
    for(auto i:gr[v]) if(i!=p){
        dfs(i,v);
        dp[v][1]+=dp[i][1];
        if(dp[v][0]+dp[i][0]+2>dp[v][1]) d[v]=i;
        dp[v][1]=max(dp[v][1],dp[v][0]+dp[i][0]+2);
        dp[v][0]+=dp[i][1];
    }
    if(dp[v][1]<dp[v][0]) d[v]=0;
    dp[v][1]=max(dp[v][1],dp[v][0]);
}
void dfs2(int v,int p=0,int s=1){
    if(!d[v]) s=0;
    if(s) ans[v]=d[v], ans[d[v]]=v;
    for(auto i:gr[v]) if(i!=p){
        dfs2(i,v,i!=d[v]);
    }
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin>>n;
    fori(1,n){
        cin>>a>>b; gr[a].pb(b); gr[b].pb(a);
    }
    dfs(1); dfs2(1);
    cout<<dp[1][1]+(n-dp[1][1])*2<<nl;
    fori(1,n+1) if(!ans[i]) for(auto j:gr[i]) if(ans[j]){
        ans[i]=ans[j]; ans[j]=i; break;
    }
    fori(1,n+1) cout<<ans[i]<<" ";
    return 0;
}
/**
7
2 7
5 2
6 1
1 4
4 5
7 3

*/

#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define _CRT_SECURE_NO_WARNINGS
#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline")
#pragma GCC option("arch=native","tune=native","no-zero-upper")
#pragma GCC target("avx2")
using namespace std;
typedef  long long ll;
typedef unsigned long long ull;
const ll INF= 1e16;
const int maxn = 1e6+7;
const int mod= 1e9+7;
inline bool read(ll &num)
{char in;bool IsN=false;
    in=getchar();if(in==EOF) return false;while(in!='-'&&(in<'0'||in>'9')) in=getchar();if(in=='-'){ IsN=true;num=0;}else num=in-'0';while(in=getchar(),in>='0'&&in<='9'){num*=10,num+=in-'0';}if(IsN) num=-num;return true;}
ll n,m,p,c;
vector<int>v[maxn];
int res[maxn],vis[maxn],in[maxn];
ll ans = 0;
void dfs(int u,int fa){
    res[u] = u;
    for(int e:v[u]){
        if(e == fa) continue;
        dfs(e,u);
    }

    for(int e:v[u]){
        if(e == fa) continue;
        if(!vis[e]){
            vis[u] = 1;
            swap(res[u],res[e]);
            ans += 2;
        }
    }
}
int main(){
    read(n);
    for(int i=1;i<=n-1;i++){
        int x,y;scanf("%d%d",&x,&y);
        v[x].push_back(y);
        v[y].push_back(x);
        in[x]++;
        in[y]++;
    }
    dfs(1,1);
    if(res[1] == 1) ans += 2,swap(res[1],res[v[1][0]]);
    printf("%lld\n",ans);
    for(int i=1;i<=n;i++){
        printf("%d ",res[i]);
    }
    return 0;
}

//                             In The Name Of Allah
//                              Mohammad Hosseini
#include <bits/stdc++.h>
#define    ss second
#define ff first
#define use_fast ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define ret(n) return cout << n, 0
#define se(n) cout << setprecision(n) << fixed
#define pb push_back
//#define int long long
#define ld long double
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
#pragma GCC optimize("no-stack-protector,fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;

const int N = 2e5 + 100, T = 1100, OO = 150, M = 1e9 + 7, P = 6151, SQ = 360, lg = 18;
typedef pair <int, int> pii;
vector <int> v[N], t;
bool mark[N];
int ans[N], sum = 0;

void dfs(int x, int y) {
	for(auto u : v[x]) {
		if(u == y)
			continue;
		dfs(u, x);
		if(mark[u])
			continue;
		if(mark[x]) {
			t.pb(u);
			continue;
		}
		ans[u] = x;
		ans[x] = u;
		sum += 2;
		mark[x] = mark[u] = true;
	}
}

int32_t main() {
    use_fast;
	int n;
	cin >> n;
	for(int i = 1; i < n; i++) {
		int x, y;
		cin >> x >> y;
		v[x].pb(y);
		v[y].pb(x);
	}
	dfs(1, 0);
	for(int i = 1; i <= n; i++) {
		if(mark[i])
			continue;
		int x = v[i].front();
		ans[i] = ans[x];
		ans[x] = i;
		sum += 2;
	}
	cout << sum << endl;
	for(int i = 1; i <= n; i++)
		cout << ans[i] << " ";
	return 0;
}
/*
be carefull :
1- if not solve after 20 min, read again twice
2- after submit read the code again
3- fun with contest
4- ...
*/

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

using namespace std;

vector<vector<int>> g, allas, allas2;
vector<bool> volt;

int t[(int)1e5][2] = {}, top[(int)1e5][2] = {};
int sor[(int)1e5];

void Bejar(int akt)
{
    volt[akt] = true;

    t[akt][0] = (int)1e6;
    t[akt][1] = 0;
    top[akt][0] = top[akt][1] = akt;

    allas[akt].resize(g[akt].size());
    allas2[akt].resize(g[akt].size());

    int y = 0;
    for(int x : g[akt])
    {
        if(!volt[x])
        {
            Bejar(x);

            int newX = t[akt][0] + t[x][0]; //min(t[akt][0] + t[x][0], t[akt][1] + t[x][1] + 2);
            allas[akt][y] = 0;

            if(newX > t[akt][1] + t[x][1] + 2)
            {
                newX = t[akt][1] + t[x][1] + 2;
                allas[akt][y] = 3;
            }

            int newY = t[akt][1] + t[x][0];//min(t[akt][1] + t[x][0], t[akt][0] + t[x][1] + 1);
            allas2[akt][y] = 2;

            if(newY > t[akt][0] + t[x][1] + 2)
            {
                newY = t[akt][0] + t[x][1] + 2;
                allas2[akt][y] = 1;
            }

            if(newY > newX)
            {
                newY = newX;
                allas2[akt][y] = allas[akt][y];
            }

            t[akt][0] = newX;
            t[akt][1] = newY;

            /*if(akt == 1)
            {
                cout<<"T: "<<x+1<<" "<<t[akt][0]<<" "<<t[akt][1]<<"\n";
            }*/
        }
        y++;
    }

    return;
}

void Kiir(int akt, int c)
{
    volt[akt] = true;

    reverse(g[akt].begin(), g[akt].end());
    reverse(allas[akt].begin(), allas[akt].end());
    reverse(allas2[akt].begin(), allas2[akt].end());

    vector<int> start(g[akt].size());

    for(int i = 0; i < g[akt].size(); i++)
    {
        if(volt[g[akt][i]])
            continue;
        if(c == 0)
        {
            if(allas[akt][i] == 3)
            {
                swap(sor[akt], sor[g[akt][i]]);
                c = 1;
                start[i] = 1;//Kiir(g[akt][i], 1);
            }
            else
                start[i] = 0;//Kiir(g[akt][i], 0);
        }
        else
        {
            if(allas2[akt][i] == 1)
            {
                c = 0;
                swap(sor[akt], sor[g[akt][i]]);
                start[i] = 1;//Kiir(g[akt][i], 1);
            }
            else if(allas2[akt][i] == 2)
                start[i] = 0;//Kiir(g[akt][i], 0);
            else if(allas2[akt][i] == 3)
            {
                swap(sor[akt], sor[g[akt][i]]);
                start[i] = 1;
            }
            if(allas2[akt][i] == 0)
            {
                c = 0;
                start[i] = 0;
            }
        }
    }

    for(int i = (int)g[akt].size()-1; i >= 0; i--)
        if(!volt[g[akt][i]])
            Kiir(g[akt][i], start[i]);

    return;
}

int tav[10][10];

bool Check(vector<int> &v)
{
    for(int i = 0; i < v.size(); i++)
        if(i == v[i])
            return false;
    return true;
}

int Ertek(vector<int> &v)
{
    int ret = 0;
    for(int i = 0; i < v.size(); i++)
        ret += tav[i][v[i]];
    return ret;
}

void Init(int n)
{
    for(int i = 0; i < n; i++)
        for(int j = 0; j < n; j++)
            for(int k = 0; k < n; k++)
                if(j != k)
                    tav[j][k] = min(tav[j][k], tav[j][i] + tav[i][k]);
    return;
}

void BruteForce(int n)
{
    vector<int> v(n);
    for(int i = 0; i < n; i++)
        v[i] = i;

    vector<int> ki = v;

    int meg = 1e6;
    do{
        if(!Check(v))
            continue;

        int akt = Ertek(v);

        if(akt < meg)
        {
            meg = akt;
            ki = v;
        }
    }
    while(next_permutation(v.begin(), v.end()));

    cout<<meg<<"\n";
    for(int i = 0; i < n; i++)
        cout<<ki[i]+1<<" ";
    cout<<"\n";

    return;
}

int main()
{
    cin.sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin>>n;

    g.resize(n);
    allas.resize(n);
    allas2.resize(n);
    volt.resize(n, false);

    /*for(int i = 0; i < 10; i++)
        for(int j = 0; j < 10; j++)
            tav[i][j] = 100;*/

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

        /*tav[a-1][b-1] = 1;
        tav[b-1][a-1] = 1;*/
    }

    //Init(n);

    for(int i = 0; i < n; i++)
        sor[i] = i;

    Bejar(0);

    volt.assign(n, false);
    Kiir(0, 0);

    vector<int> ki(n);

    for(int i = 0; i < n; i++)
        ki[sor[i]] = i;

    cout<<t[0][0]<<"\n";
    for(int i = 0; i < n; i++)
        cout<<ki[i]+1<<" ";
    cout<<"\n";

    /*if(!Check(ki) || Ertek(ki) != t[0][0])
        cout<<"Rossz! "<<Ertek(ki)<<"\n";

    cout<<"-------------------\n";
    BruteForce(n);*/

    /*for(int i = 0; i < n; i++)
        cout<<i+1<<" | "<<t[i][0]<<" "<<t[i][1]<<"\n";*/

    return 0;
}

#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#define MAXN 100005
using namespace std;
int N, D, Ans[MAXN];
vector <int> adj[MAXN];

void dfs(int u, int prev) {
    for (int v: adj[u]) {
        if (v == prev) {continue;}
        dfs(v, u);
    }
    if (prev && Ans[u] == u) {
        swap(Ans[u], Ans[prev]);
        D += 2;
    }
}

int main() {
    //freopen("villagein.txt","r",stdin);
    scanf("%d",&N);
    for (int i=1; i<N; i++) {
        int u,v;
        scanf("%d %d",&u,&v);
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    for (int i=1; i<=N; i++) {Ans[i] = i;}
    dfs(1,0);
    if (Ans[1] == 1) {
        D+= 2;
        swap(Ans[1], Ans[adj[1][0]]);
    }
    printf("%d\n",D);
    for (int i=1; i<=N; i++) {
        printf("%d ", Ans[i]);
    }
}

#include<cstdio>
#include<iostream>
#include<queue>
#include<algorithm>
#include<cstring>
#include<set>
#include<vector>
#define PII pair<int,int>
#define pb push_back
#define ep emplace_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
inline int read(){
	int x=0,f=1;char c=getchar();
	while(c<'0'||c>'9'){ if(c=='-') f=-1; c=getchar();}
	while(c>='0'&&c<='9'){x=(x<<3)+(x<<1)+(c^48);c=getchar();}
	return f==1?x:-x;
}
inline void print(int x){
	if(x<0) putchar('-'),x=~x+1;
	if(x>=10) print(x/10);
	putchar((x%10)|48);
}
int n;
int siz[100010];
int rt,idx;
long long res;
int ans[100010];
vector<int>vec[100010];
int dfs(int x,int fa){
	siz[x]=1;
	vector<int>ve;
	for(int i:vec[x]){
		if(i==fa) continue ;
		int a=dfs(i,x);
		if(a!=-1) ve.pb(a);
		siz[x]+=siz[i];
	}
	int sz=ve.size();
	if(sz==0){
		if(x!=1) return x;
		int v=vec[x][0];
		ans[x]=ans[v],ans[v]=x;res+=2;return -1;
	}
	else if(sz&1){
		for(int i=0;i+2<=sz;i+=2){
			res+=4;
			ans[ve[i]]=ve[i+1],ans[ve[i+1]]=ve[i];
		}
		ans[ve[sz-1]]=x,ans[x]=ve[sz-1];
		res+=2;
		return -1;
	}
	else{
		for(int i=0;i+4<=sz;i+=2){
			res+=4;
			ans[ve[i]]=ve[i+1],ans[ve[i+1]]=ve[i];
		}
		res+=4;
		ans[ve[sz-2]]=x,ans[x]=ve[sz-1],ans[ve[sz-1]]=ve[sz-2];
		return -1;
	}
}
int main(){
	n=read();
	for(int i=1;i<n;++i){
		int u=read(),v=read();
		vec[u].pb(v);vec[v].pb(u);
	}
	dfs(1,0);
	printf("%lld\n",res);
	for(int i=1;i<=n;++i) printf("%d ",ans[i]);
} //MI

#include <bits/stdc++.h>
#include <ext/rope>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define ll long long
//#define int long long
#define pb push_back
#define x first
#define ld long double
#define y second
#define mk(a,b) make_pair(a,b)
#define rr return 0
#define sqr(a) ((a)*(a))
#define all(a) a.begin(),a.end()

using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
template<class value, class cmp = less<value> >
using ordered_set = tree<value, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
template<class value, class cmp = less_equal<value> >
using ordered_multiset = tree<value, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
template<class key, class value, class cmp = less<key> >
using ordered_map = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;

/// find_by_order()
/// order_of_key()
mt19937 rng(230);
inline int randll(int l = INT_MIN, int r = INT_MAX) {
    return uniform_int_distribution<int>(l, r)(rng);
}
vector <vector <int> > edge;
vector <int> res;
vector <vector <int> > have;
int ans = 0, n;
int cannot = -1;
inline void dfs (int v, int p = -1) {
    have[v].pb(v);
    for (int to : edge[v]) {
        if (to == p || to == cannot) continue;
        dfs(to, v);
        ans += (int)have[to].size();
//        if ((int)have[to].size() > (int)have[v].size()) {
//            have[v].swap(have[to]);
//        }
        for (int j : have[to]) {
            have[v].pb(j);
        }
        have[to].clear();
    }
    while ((int)have[v].size() > 3) {
        int a = have[v].back();
        have[v].pop_back();
        int b = have[v].back();
        have[v].pop_back();
        res[a] = b;
        res[b] = a;
    }
    if ((int)have[v].size() == 3) {
        int a = have[v].back();
        have[v].pop_back();
        int b = have[v].back();
        have[v].pop_back();
        int c = have[v].back();
        have[v].pop_back();
        res[a] = b;
        res[b] = c;
        res[c] = a;
        return;
    }
    if ((int)have[v].size() == 2) {
        int a = have[v].back();
        have[v].pop_back();
        int b = have[v].back();
        have[v].pop_back();
        res[a] = b;
        res[b] = a;
        return;
    }
}
vector <vector <int> > d;
inline int bfs (int v, int t) {
    if (!d[v].empty()) return d[v][t];
    queue <int> q;
    d[v].assign(n, (int)1e9);
    d[v][v] = 0;
    q.push(v);
    while (!q.empty()) {
        int V = q.front();
        q.pop();
        for (int to : edge[V]) {
            if (d[v][to] == 1e9) {
                d[v][to] = d[v][V] + 1;
                q.push(to);
            }
        }
    }
    return d[v][t];
}
main()
{
    ios::sync_with_stdio(0);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    edge.resize(n);
    have.resize(n);
    res.assign(n, -1);
    int a, b;
    for (int i = 1; i < n; i++) {
        cin >> a >> b;
        --a, --b;
        edge[a].pb(b);
        edge[b].pb(a);
    }
    if (n <= 10) {
        int an = 1e9;
        vector <int> re;
        vector <int> p(n);
        d.resize(n);
        for (int i = 0; i < n; i++) p[i] = i;
        do {
            if (clock() > 0.7 * CLOCKS_PER_SEC) break;
            int cur = 0;
            for (int i = 0; i < n; i++) {
                if (p[i] == i) {
                    cur = 1e9;
                    break;
                }
            }
            if (cur != 0) continue;
            for (int i = 0; i < n; i++) {
                cur += bfs(i, p[i]);
            }
            if (cur < an) {
                an = cur;
                re = p;
                }
        } while(next_permutation(all(p)));
        cout << an << '\n';
        for (auto &i : re) cout << i + 1 << ' ';
        cout << '\n';
        rr;
    }
    int leaf = -1;
    for (int i = 0; i < n; i++) {
        if ((int)edge[i].size() == 1) {
            leaf = i;
            break;
        }
    }
    dfs(leaf);
    if ((int)have[leaf].size() == 1) {
        assert(have[leaf][0] == leaf);
        res[leaf] = edge[leaf][0];
        int v = -1;
        for (int i = 0; i < n; i++) {
            if (res[i] == edge[leaf][0]) v = i;
        }
        res[v] = leaf;
        ++ans;
    }
    cout << ans + ans << '\n';
    for (auto &i : res) cout << i + 1 << ' ';
    cout << '\n';
}

#pragma comment(linker, "/stack:200000000")
#include <bits/stdc++.h>

#define F first
#define S second
#define pb push_back
#define ll long long

using namespace std;

const int MOD = 1e9 + 7;
const int N = 200005;

int n;
vector<int> g[N];

int par[N];

int main() {
    ios_base :: sync_with_stdio(0);
    cin.tie(0);
    /// i'm very hungry
    cin >> n;
    if (n == 2) {
        cout << "2\n2 1" << endl;
        return 0;
    }
    for (int i = 1, a, b; i < n; i++) {
        cin >> a >> b;
        g[a].pb(b);
        g[b].pb(a);
    }
    int rt = -1;
    for (int i = 1; i <= n; i++) {
        if ((int)g[i].size() > 1) {
            rt = i; break;
        }
    }
    queue<int> q;
    q.push(rt);
    vector <int> ord;
    while(!q.empty()) {
        int cur = q.front(); q.pop();
        ord.pb(cur);
        for (int i : g[cur]) {
            if (i != par[cur]) {
                par[i] = cur;
                q.push(i);
            }
        }
    }
    reverse(ord.begin(), ord.end());
    vector<int> used(n + 1, 0), pos(n + 1, 0);
    int i = 0;
    int res = 0;
    while(i < n) {
        if (used[ord[i]]) {i++; continue;}
        int j = i;
        while(j < n && par[ord[j]] == par[ord[i]]) j++;
        vector<int> nw;
        j--;
        for (int k = i; k <= j; k++) {
            if (!used[ord[k]]) nw.pb(ord[k]);
        }
        int sz = nw.size();/*
        cout << "current are ";
        for (int k = 0; k < sz; k++) {
            cout << nw[k] << " ";
        }
        cout << endl;*/
        if (sz % 2 == 1) {
            pos[nw[0]] = par[nw[0]];
            pos[par[nw[0]]] = nw[0];
            used[par[nw[0]]] = 1;
            res += 2;
            for (int k = 1; k < sz - 1; k += 2) {
                pos[nw[k]] = nw[k + 1];
                pos[nw[k + 1]] = nw[k];
                res += 4;
            }
        } else {
            for (int k = 2; k < sz - 1; k += 2) {
                pos[nw[k]] = nw[k + 1];
                pos[nw[k + 1]] = nw[k];
                res += 4;
            }
            pos[nw[0]] = nw[1];
            pos[nw[1]] = par[nw[0]];
            pos[par[nw[0]]] = nw[0];
            res += 4;
            used[par[nw[0]]] = 1;
        }/*
        cout << "now the pos are ";
        for (int i = 1; i <= n; i++) {
            cout << pos[i] << " ";
        }
        cout << endl;*/
        i = j + 1;
    }
    if (pos[rt] == 0) {
        int x = g[rt][0];
        for (int j : g[rt]) {
            if ((int)g[j].size() > 1) {
                x = j; break;
            }
        }
        int to = pos[x];
        pos[x] = rt;
        pos[rt] = to;
    }
    cout << res << endl;
    //set<int> s;
    for (int i = 1; i <= n; i++) {
        cout << pos[i] << " ";
        //s.insert(pos[i]);
        //if (pos[i] < 1 || pos[i] > n) assert(0);
    }
    //if ((int)s.size() != n) assert(0);
    cout << endl;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;

using ll = long long;

using vi = vector<int>;
using vl = vector<ll>;
#define pb push_back
#define rsz resize
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()

using pi = pair<int,int>;
using pl = pair<ll, ll>;
#define f first
#define s second
#define mp make_pair

namespace output {
  void pr(int x) {
    cout << x;
  }
  void pr(long x) {
    cout << x;
  }
  void pr(ll x) {
    cout << x;
  }
  void pr(unsigned x) {
    cout << x;
  }
  void pr(unsigned long x) {
    cout << x;
  }
  void pr(unsigned long long x) {
    cout << x;
  }
  void pr(float x) {
    cout << x;
  }
  void pr(double x) {
    cout << x;
  }
  void pr(long double x) {
    cout << x;
  }
  void pr(char x) {
    cout << x;
  }
  void pr(const char * x) {
    cout << x;
  }
  void pr(const string & x) {
    cout << x;
  }
  void pr(bool x) {
    pr(x ? "true" : "false");
  }

  template < class T1, class T2 > void pr(const pair < T1, T2 > & x);
  template < class T > void pr(const T & x);

  template < class T, class...Ts > void pr(const T & t,
    const Ts & ...ts) {
    pr(t);
    pr(ts...);
  }
  template < class T1, class T2 > void pr(const pair < T1, T2 > & x) {
    pr("{", x.f, ", ", x.s, "}");
  }
  template < class T > void pr(const T & x) {
    pr("{"); // const iterator needed for vector<bool>
    bool fst = 1;
    for (const auto & a: x) pr(!fst ? ", " : "", a), fst = 0;
    pr("}");
  }

  void ps() {
    pr("\n");
  } // print w/ spaces
  template < class T, class...Ts > void ps(const T & t,
    const Ts & ...ts) {
    pr(t);
    if (sizeof...(ts)) pr(" ");
    ps(ts...);
  }

  void pc() {
    cout << "]" << endl;
  } // debug w/ commas
  template < class T, class...Ts > void pc(const T & t,
    const Ts & ...ts) {
    pr(t);
    if (sizeof...(ts)) pr(", ");
    pc(ts...);
  }
  #define dbg(x...) pr("[", #x, "] = ["), pc(x);
}

#ifndef ONLINE_JUDGE
using namespace output;
#else
using namespace output;
#define dbg(x...)
#endif

const int MX = 200005;
const int MOD = (int) (1e9 + 7);
const ll INF = (ll) 1e18;

int n;
vi adj[MX];
ll dp[MX][2];
int fr[MX];
int ret[MX];

void dfs(int v, int p) {
    pl dp_curr = mp(0, INF);
    pl dp_prev = mp(INF, INF);
    for (int to : adj[v]) {
        if (to != p) {
            dfs(to, v);
            swap(dp_curr, dp_prev);
            vl trans = {
                dp_prev.f + dp[to][1] + 2,
                dp_prev.s + dp[to][1],
                dp_prev.s + dp[to][0] + 2,
                dp_prev.f + dp[to][0] + 2,
            };
            pair<ll, int> best = mp(min(INF, trans[0]), 0);
            for (int i = 1; i < 4; i++) {
                best = min(best, mp(min(INF, trans[i]), i));
            }
            dp_curr.s = best.f;
            fr[to] = best.s;
            dp_curr.f = min(INF, dp_prev.f + dp[to][1]);
        }
    }
    dp[v][0] = dp_curr.f;
    dp[v][1] = dp_curr.s;
    // dbg(v, dp[v][0], dp[v][1]);
}

void construct(int v, int p, int x);

void go(int v, vi &tos, int pre, bool done) {
    if (pre < 0) return;
    int to = tos[pre];
    if (done) {
        go(v, tos, pre - 1, true);
        construct(to, v, 1);
    } else {
        int need = fr[to];
        if (need == 1 || need == 2) {
            go(v, tos, pre - 1, false);
            if (need == 1) {
                construct(to, v, 1);
            } else {
                construct(to, v, 0);
                ret[to] = ret[v];
                ret[v] = to;
            }
        } else {
            go(v, tos, pre - 1, true);
            if (need == 0) {
                construct(to, v, 1);
                ret[v] = ret[to];
                ret[to] = v;
            } else {
                construct(to, v, 0);
                ret[v] = to;
                ret[to] = v;
            }
        }
    }
}

void construct(int v, int p, int x) {
    if (x == 0) {
        for (int to : adj[v]) {
            if (to != p) {
                construct(to, v, 1);
            }
        }
    } else {
        vi gos;
        for (int to : adj[v]) if (to != p) gos.pb(to);
        go(v, gos, sz(gos) - 1, false);
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n;
    for (int i = 0; i < n - 1; i++) {
        int u, v; cin >> u >> v;
        u--, v--;
        adj[u].pb(v), adj[v].pb(u);
    }
    dfs(0, -1);
    cout << dp[0][1] << '\n';
    memset(ret, -1, sizeof(ret));
    construct(0, -1, 1);
    for (int i = 0; i < n; i++) {
        cout << ret[i] + 1 << " ";
    }
    cout << '\n';
}

#include <bits/stdc++.h>
using namespace std;

#define MOD 1000000007
#define ll long long int
#define vi vector<int>
#define vii vector<vector<int> >

vii graph(100005,vi());

bool was[100005];
int goal[100005];
int invgoal[100005];
int cost = 0;

void dfs(int v) {
	was[v] = true;
	vector<int> tomove;
	for(int u : graph[v]) {
		if(!was[u]) {
			dfs(u);
			if(goal[u] == 0) {
				tomove.push_back(u);
			}
		}
	}
	if(tomove.size()) {
		cost+= tomove.size()*2;
		for(int i = 1; i < tomove.size(); i++) {
			goal[tomove[i-1]] = tomove[i];
			invgoal[tomove[i]] = tomove[i-1];
		}
		goal[tomove[tomove.size()-1]] = v;
		invgoal[v] = tomove[tomove.size()-1];
		goal[v] = tomove[0];
		invgoal[tomove[0]] = v;
	} else if(v == 1) {
		goal[1] = graph[1][0];
		goal[invgoal[graph[1][0]]] = 1;
		cost+= 2;
	}
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	int n;
	cin >> n;
	for(int i = 1; i < n; i++) {
		int a,b;
		cin >> a >> b;
		graph[a].push_back(b);
		graph[b].push_back(a);
	}
	dfs(1);
	cout << cost << "\n";
	for(int i = 1; i <= n; i++) {
		cout << goal[i] << " ";
	}
	return 0;
}

#include <bits/stdc++.h>

using namespace std;

using ll = int64_t;
//using ll = long long;
using ull = uint64_t;
using i32 = int32_t;
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;
using f64 = double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using ld = double;

#define X first
#define Y second

//#ifndef ONLINE_JUDGE
#define FWRITE
//#endif

namespace io
{

#ifndef FWRITE
#include <unistd.h>
	auto unistd_read = read;
	auto unistd_write = write;
#endif
	const int BUFSIZE = 1 << 20;

	int isize, osize;
	char ibuf[BUFSIZE + 10], obuf[BUFSIZE + 10];
	char* is, * it, * os = obuf, * ot = obuf + BUFSIZE;

	char getchar()
	{
		if (is == it)
		{
			is = ibuf;
#ifdef FWRITE
			it = ibuf + fread(ibuf, 1, BUFSIZE, stdin);
#else
			it = ibuf + unistd_read(STDIN_FILENO, ibuf, BUFSIZE);
#endif
			if (is == it) return EOF;
		}
		return *is++;
	}

	char getalpha()
	{
		char c = getchar();
		while (!isalpha(c)) c = getchar();
		return c;
	}

	void putchar(char c)
	{
		*os++ = c;
		if (os == ot)
		{
#ifdef FWRITE
			fwrite(obuf, 1, BUFSIZE, stdout);
#else
			unistd_write(STDOUT_FILENO, obuf, BUFSIZE);
#endif
			os = obuf;
		}
	}

	int inp() {
		int x = 0, f = 0; char ch;
		for (ch = getchar(); !isdigit(ch); ch = getchar())
		{
			if (ch == EOF) return -1;
			if (ch == '-') f = 1;
		}
		for (; isdigit(ch); x = x * 10 + ch - '0', ch = getchar());
		return f ? -x : x;
	}

	ll inp_ll() {
		ll x = 0; int f = 0; char ch;
		for (ch = getchar(); !isdigit(ch); ch = getchar())
			if (ch == '-') f = 1;
		for (; isdigit(ch); x = x * 10 + ch - '0', ch = getchar());
		return f ? -x : x;
	}

	template<class T>
	bool read(T& x)
	{
		x = 0;
		int f = 0; char ch;
		for (ch = getchar(); !isdigit(ch); ch = getchar())
		{
			if (ch == EOF) return 0;
			if (ch == '-') f = 1;
		}
		for (; isdigit(ch); x = x * 10 + ch - '0', ch = getchar());
		if (f) x = -x;
		return 1;
	}

	bool read(char* s)
	{
		char* t = s;
		char ch = getchar();
		for (; ch == ' ' || ch == '\n'; ch = getchar());
		for (; ch != ' ' && ch != '\n' && ch != EOF; ch = getchar())
			*t++ = ch;
		*t = 0;
		return s != t;
	}

	template<class T, class... Args>
	bool read(T& x, Args&... args)
	{
		return read(x) && read(args...);
	}

	template<class T>
	bool readln(T& x)
	{
		x = 0;
		int f = 0; char ch;
		for (ch = getchar(); !isdigit(ch); ch = getchar())
		{
			if (ch == EOF) return 0;
			if (ch == '-') f = 1;
		}
		for (; isdigit(ch); x = x * 10 + ch - '0', ch = getchar());
		if (f) x = -x;
		for (; ch != '\n' && ch != EOF; ch = getchar());
		return 1;
	}

	bool readln(char* s)
	{
		char* t = s;
		while (1)
		{
			char ch = getchar();
			if (ch == '\n' || ch == EOF) break;
			*t++ = ch;
		}
		*t = 0;
		return s != t;
	}

	template<class T, class... Args>
	bool readln(T& x, Args&... args)
	{
		return read(x) && readln(args...);
	}

	template<class T>
	void write(T x)
	{
		static char s[22];
		static char* it = s + 20;
		static char* end = s + 20;

		if (x < 0)
		{
			putchar('-');
			x = -x;
		}

		do
		{
			*--it = x % 10 + '0';
			x /= 10;
		} while (x);
		/*
		if (!x)
			*-- it = '0';
		while (x)
		{
			*-- it = x%10+'0';
			x /= 10;
		}
		*/
		for (; it < end; ++it)
			putchar(*it);
	}

	void write(const char* s)
	{
		for (; *s; ++s) putchar(*s);
	}

	template<>
	void write(char* s)
	{
		write((const char*)s);
	}

	template<>
	void write(char c)
	{
		putchar(c);
	}

	template<class T, class... Args>
	void write(T x, Args... args)
	{
		write(x);
		write(args...);
	}

	void writeln()
	{
		putchar('\n');
	}

	template<class T, class... Args>
	void writeln(T x, Args... args)
	{
		write(x);
		writeln(args...);
	}

	template<class Iterator>
	bool input(Iterator st, Iterator ed)
	{
		for (; st != ed; ++st)
		{
			if (!read(*st)) return false;
		}
		return true;
	}

	template<class T>
	bool input(T& a)
	{
		return input(a.begin(), a.end());
	}

	template<class Iterator>
	void print(Iterator st, Iterator ed, const char* c = " ")
	{
		int flag = 0;
		for (; st != ed; ++st)
		{
			if (flag) write(c);
			flag = 1;
			write(*st);
		}
		writeln();
	}

	template<class T>
	void print(const T& a, const char* c = " ")
	{
		print(a.begin(), a.end(), c);
	}

	struct ender
	{
		~ender()
		{
			if (os != obuf)
#ifdef FWRITE
				fwrite(obuf, 1, os - obuf, stdout);
#else
				unistd_write(STDOUT_FILENO, obuf, os - obuf);
#endif
		}
	}__ender;

}

int64_t power(int64_t a, int64_t b, int64_t p)
{
	if (!b) return 1;
	int64_t t = power(a, b >> 1, p);
	t = t * t % p;
	if (b & 1) t = t * a % p;
	return t;
}

pll exgcd(ll a, ll b)
{
	if (b == 0) return { 1, 0 };
	//auto [x, y] = exgcd(b, a % b);
	auto e = exgcd(b, a % b);
	ll x = e.first;
	ll y = e.second;
	return { y, x - a / b * y };
}

//mt19937_64 rd(chrono::steady_clock::now().time_since_epoch().count());
//mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rd(1);

using namespace io;

template<class T>
inline void freshmin(T& a, const T& b)
{
	if (a > b) a = b;
}

template<class T>
inline void freshmax(T& a, const T& b)
{
	if (a < b) a = b;
}

//const ll B = 31;
//const ll MOD = 1000000007;
//const int INF = 1000000010;
//const ll INFll = 4000000000000000000LL;
//const int MAXN = 600010;
//const int MAXK = 21;

int dx[] = { -1, 1, 0, 0, -1, -1, 1, 1 };
int dy[] = { 0, 0, -1, 1, -1, 1, -1, 1 };

template<class T>
T det(T x1, T y1, T x2, T y2, T x3, T y3)
{
	return x1 * y2 - x2 * y1 + x2 * y3 - x3 * y2 + x3 * y1 - x1 * y3;
}

template<class T>
T dis(T x1, T y1, T x2, T y2)
{
	return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}

const int INF = 1000000000;

void solve()
{
	int n;
	read(n);
	vector<vector<int>> v(n);
	for (int i = 1; i < n; ++i)
	{
		int x, y;
		read(x, y);
		x -= 1; y -= 1;
		v[x].push_back(y);
		v[y].push_back(x);
	}
	int root = 0;
	for (int i = 0; i < n; ++i)
		if (v[i].size() != 1) root = i;

	vector<int> u(n);
	vector<int> res(n);
	for (int i = 0; i < n; ++i) res[i] = i;
	int ans = 0;
	function<void(int, int)> dfs = [&](int x, int pre)
	{
		for (auto y : v[x])
		{
			if (y == pre) continue;
			dfs(y, x);
		}
		if (!u[x])
		{
			ans += 2;
			if (x == root)
			{
				assert(v[x].size() >= 1);
				u[x] = 1;
				swap(res[x], res[v[x][0]]);
			}
			else
			{
				u[x] = 1;
				u[pre] = 1;
				swap(res[x], res[pre]);
			}
		}
	};

	dfs(root, -1);
	writeln(ans);
	for (auto& x : res) x += 1;
	print(res);
}

int main()
{

	int T = 1;
	//read(T);
	//init();
	for (int test = 1; test <= T; ++test)
	{
		//write("Case #", test, ": ");
		solve();
	}

	return 0;
}

#include <bits/stdc++.h>

//#pragma GCC optimize("Ofast")

#define fi first
#define se second
#define ll long long
#define dl double long

using namespace std;

const int NN = 1e9 + 7;
const int N = 2e5 + 7;
const int M = 25;
const ll mod = 1e9 + 7;
const ll inf = 1e18 + 7;
const dl rf = 1e-14;
const int B = sqrt(N);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int n;
int ans[N];
bool used[N];
set < int > s[N];

void solve1()
{
    cin >> n;
    for( int i = 1; i < n; i++ ){
        int x,y;
        cin >> x >> y;
        s[x].insert(y);
        s[y].insert(x);
    }
    queue < int > q;
    for( int i = 1; i <= n; i++ ){
        if( (int)s[i].size() == 1 ){
            q.push(i);
        }
    }
    int res = 0;
    while( !q.empty() ){
        int x = q.front();
        q.pop();
        if( used[x] )continue;
        vector < int > t;
        int y = *s[x].begin();
        t.push_back(y);
        for( auto z : s[y] ){
            if( (int)s[z].size() == 1 ){
                t.push_back(z);
            }
        }
        for( int i = 1; i < (int)t.size() - 1; i++ ){
            res += 2;
            ans[t[i]] = t[i + 1];
        }
        res += 2;
        int l = (int)t.size();
        ans[t[0]] = t[1];
        ans[t[l - 1]] = t[0];
        for( auto z : t ){
            used[z] = true;
        }
        for( auto z1 : s[y] ){
            if( used[z1] )continue;
            s[z1].erase(y);
            if( (int)s[z1].size() == 1 ){
                q.push(z1);
            }
        }
    }
    cout << res << "\n";
    for( int i = 1; i <= n; i++ ){
        assert( ans[i] != i );
        cout << ans[i] << " ";
    }
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    //freopen( "input.txt" , "r" , stdin );
    //freopen( "output.txt" , "w" , stdout );

    int cghf = 1;//cin >> cghf;
    while( cghf-- ){
        solve1();
    }
}

/*
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
*/
#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define loop(i,n) for(int i=0;i<n;i++)
#define lop(i,a,b) for(int i=a;i<=b;i++)
#define int long long int
typedef vector< int > vi;
typedef vector< vi > vvi;
typedef pair< int,int > pii;
#define mp make_pair
#define sz(a) int((a).size())
#define pb push_back
#define tr(container, it) for(auto it = container.begin(); it != container.end(); it++)
#define all(a) a.begin(), a.end()
#define allr(a) a.rbegin(), a.rend()
#define so(v) sort(all(v))
#define sod(v) sort(allr(v))
#define mfind(c, element) (c.find(element) != c.end())
#define gfind(c, element) (find(all(c),element) != c.end())
#define gfindindex(c, element) (find(all(c),element)-c.begin())
#define vp(x) cout << #x << " --> " << x << endl;
#define cp1(v) cout << #v << "--->" << endl; for (auto it : v)  cout << it << " "; cout << endl;
#define cpp1(v) cout << #v << "-->" << endl; for(auto it:v){cout << it.fi << " " << it.se << endl;}
#define ccp1(v) cout << #v << "--->" << endl;for (auto it:v){for(auto it1 : it){cout << it1 << " ";}cout<< endl;}
#define bsnp np3(low,high,mid);
#define hmm1 high=mid-1;
#define lmp1 low=mid+1;
#define hmp1 high=mid+1;
#define lmm1 low=mid-1;
#define fi first
#define se second
#define endl "\n"
#define np1(x) cout<<#x<<"="<<x<<'\n'
#define np2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<'\n'
#define np3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<'\n'
struct cmp {
	bool operator() (const pair<int, int> &a, const pair<int, int> &b) const {
		int lena = a.second - a.first + 1;
		int lenb = b.second - b.first + 1;
		if (lena == lenb) return a.first < b.first;
		return lena > lenb;
	}
};//comparison used for sorting set<pair<i,i>,cmp>
bool myComparison(const pair<int,int> &a,const pair<int,int> &b)
{
       return a.second>b.second;
//increasing ordr basedd on 2nd
};
int n,m;
vvi edges;
vector<bool> visited;
main()
{
IOS;
cin>>n;
m=n-1;
int x,y;
edges.clear();visited.clear();
edges.resize(n+1);
visited.resize(n+1,false);
vi deg(n+1,0),house(n+1);
set<int> leafs;
lop(i,1,n) house[i]=i;
loop(i,m)
{
    cin>>x>>y;
    edges[x].pb(y);
    edges[y].pb(x);
    deg[x]++;
    deg[y]++;
}

lop(i,1,n)
if(deg[i]==1) leafs.insert(i);
int ans=0;
while(!leafs.empty())
{
    auto x=*leafs.begin();
    leafs.erase(x);
    if(visited[x])
    {
        for(auto u:edges[x])
        {
            deg[u]--;
            if(deg[u]==1)
            leafs.insert(u);
        }
        continue;
    }
    visited[x]=true;
    bool f=false;
    for(auto u:edges[x])
    {
        if(!visited[u] && !f)
        {
            f=true;
            visited[u]=true;
            ans+=2;
            swap(house[x],house[u]);
        }
        deg[u]--;
        if(deg[u]==1) leafs.insert(u);
    }
    if(f) continue;
    else
    {
        ans+=2;
        auto z=*edges[x].begin();
        swap(house[x],house[z]);
    }

}

cout<<ans<<endl;
lop(i,1,n)
cout<<house[i]<<" ";

return 0;
}

#include <bits/stdc++.h>

using namespace std;

vector <int> G[100007];
bool odw[100007];
int ile[100007];
int ans[100007];
deque <int> kolejka;
int cost=0;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n,a,b;
    cin>>n;
    for(int i=1;i<n;i++)
    {
        cin>>a>>b;
        G[a].push_back(b);
        G[b].push_back(a);
        ile[a]++;
        ile[b]++;
    }
    for(int i=1;i<=n;i++) ans[i]=i;
    for(int i=1;i<=n;i++) if(G[i].size()==1) kolejka.push_back(i);
    while(!kolejka.empty())
    {
        int u=kolejka[0];
        kolejka.pop_front();
        int p=-1;
        for(auto v:G[u])
        {
            if(!odw[v]) p=v;
        }
        if(p==-1)
        {
            if(ans[u]==u)
            {
                cost+=2;
                swap(ans[u],ans[G[u][0]]);
            }
        }
        else
        {
            if(ans[u]==u)
            {
                cost+=2;
                swap(ans[u],ans[p]);
            }
            ile[p]--;
            if(ile[p]==1)
            {
                kolejka.push_back(p);
            }
        }
        odw[u]=1;

    }
    cout<<cost<<endl;
    for(int i=1;i<=n;i++) cout<<ans[i]<<" ";
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define pii pair <int, int>
#define F first
#define S second
#define ll long long
#define io ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define M_PI 3.14159265358979323846
const int N = 100005;
int n, deg[N], res[N];
int ans = 0;
vector <int> a[N];

void DFS(int node, int par){
    int ch = 0, last_child = node;
    for(int i : a[node]){
        if(i == par) continue;
        DFS(i, node);
        if(deg[i] == 0){
            ch++;
            res[last_child] = i;
            last_child = i;
        }
    }
    deg[node] = ch * 2;
    ans += deg[node];
    if(ch > 0) res[last_child] = node;
}

int main() {
    io;
    scanf("%d", &n);
    int root = 1;
    for(int i = 1; i < n; i++){
        int x, y;
        scanf("%d%d", &x, &y);
        a[x].push_back(y);
        a[y].push_back(x);
        res[x]++;
        res[y]++;
        if(res[x] > 1) root = x;
        if(res[y] > 1) root = y;
    }
    DFS(root, root);
    if(deg[root] == 0){
        ans += 2;
        int child = a[root][0];
        res[root] = res[child];
        res[child] = root;
    }
    printf("%d\n", ans);
    for(int i = 1; i <= n; i++) printf("%d ", res[i]);
}

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define PI 3.14159265

ll mod = 1000000007;
ll z   = 1000000000;
const ll N   = 1e5+1;
const ll INF = 1e18;

#define mp                make_pair
#define pb                push_back
#define ppb               pop_back
#define pf                push_front
#define ppf               pop_front
#define all(x)            (x).begin(),(x).end()
#define uniq(v)           (v).erase(unique(all(v)),(v).end())
#define sz(x)             (ll)((x).size())
#define fr                first
#define sc                second
#define pii               pair<ll,ll>
#define vi                vector<ll>
#define vvi               vector<vector<ll>>
#define vp                vector<pair<ll,ll>>
#define rep(i,a,b)        for(ll i=a;i<b;i++)
#define repr(i,a,b)       for(ll i=b-1;i>=a;i--)
#define mem1(a)           memset(a,-1,sizeof(a))
#define mem0(a)           memset(a,0,sizeof(a))
#define ppc               __builtin_popcount
#define ppcll             __builtin_popcountll

template<typename T,typename T1>T amax(T &a,T1 b){if(b>a)a=b;return a;}
template<typename T,typename T1>T amin(T &a,T1 b){if(b<a)a=b;return a;}

#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)

// ll euclideandis(ll a, ll b)
// {
// 	return sqrt(a*a + b*b);
// }
// ll sumofdigit(ll a){ll ans = 0;while(a){ans += a%10;a = a/10;}return ans;}

// bool cmp(const pair<ll,ll>& p1, const pair<ll,ll>& p2)
// {
// 	if(p1.first == p2.first)
// 	{
// 		return (p1.second > p2.second);
// 	}
// 	return (p1.first < p2.first);
// }

// int gcd(int a, int b)
// {
//     if (!a)
//        return b;
//     return gcd(b%a,a);
// }

//////////////////////////////////////////////////////////////////////Factorial and inverse modulo calc//////////////////////////////
// vector<ll> fact(1000001,0);
// fact[0] = 1;
// for(int i=1;i<=1000000;i++)
//     fact[i] = (fact[i-1]*i)%mod;

// vector<ll> inv(1000001,0);
// inv[0]=1;
// for(int i=1;i<=1000000;i++)
// {
//     inv[i]=(inv[i-1]*powe(i,mod-2))%mod;
// }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////exponential power calc////////////////////////////////////////
// ll powe(ll x, ll y)
// {
//     if(y == 0) return 1;
//     if(y == 1) return x;

//     ll t = powe(x,y/2);
//     t = (t*t)%mod;

//     if(y%2 == 1) t = (t*x)%mod;

//     return t;
// }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////fibonacii//////////////////////////////////////////////////////////
// vi fib(100005);
// void fibo()
// {
// 	fib[0] = 1;fib[1] = 1;fib[2] = 2;
// 	for(int i=3;i<=10000;i++)
// 		fib[i] = (fib[i-1]%mod+fib[i-2]%mod)%mod;
// }
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////DFS, Dijkstra and calc of which path was travelled more////////////
// void DFS(pii end)
// {
// 	queue<pii> q;
// 	q.push(end);
// 	int movex[] = {1,0,-1,0};
// 	int movey[] = {0,1,0,-1};
// 	while(!q.empty())
// 	{
// 		pii p = q.front();
// 		q.pop();
// 		int x = p.fr;
// 		int y = p.sc;
// 		rep(i,0,4)
// 		{
// 			int X = x + movex[i];
// 			int Y = y + movey[i];
// 			if(X>=0 && X<r && Y>=0 && Y<c && !visited[X][Y] && a[X][Y] != 'T')
// 			{
// 				dist[X][Y] = min(dist[X][Y], dist[x][y] + 1);
// 				q.push(mp(X,Y));
// 			}
// 		}
// 	}
// }

// vector<bool> visited(N, false);
// vector<vector<pair<ll,ll>>> adj(N);
// vector<pair<ll,ll>> edges;
// vector<ll> dist(N,INT_MAX);
// void dijkstra(ll start, ll n)
// {
// 	set<pair<ll,ll>> s;
// 	s.insert(mp(0,start));
// 	while(!s.empty())
// 	{
// 		pii p = *s.begin();
// 		s.erase(s.begin());
// 		for(pii u:adj[p.sc])
// 		{
// 			ll to = u.fr;
// 			ll time = u.sc;
// 			if(dist[to] > dist[p.sc]+time)
// 			{
// 				s.erase({dist[to], to});
// 				dist[to] = dist[p.sc]+time;
// 				s.insert({dist[to], to});
// 			}
// 		}
// 	}
// }

// ll dfs(priority_queue<ll>& pq, vvi& adj, ll start, ll par)
// {
// 	ll sum = 1;
// 	for(auto u:adj[start])
// 	{
// 		if(u != par)
// 		{
// 			ll x = dfs(pq, adj, u, start);
// 			pq.push((adj.size()-x)*x);
// 			sum += x;
// 		}
// 	}
// 	return sum;
// }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

ll n,m,k,t;
// vi a;
vvi adj;
vector<bool> visited;
ll cost = 0;
vi houses;

ll dfs(ll start, ll par)
{
	for(ll u:adj[start])
	{
		if(u == par)
			continue;
		dfs(u,start);
	}
	ll lsize = 0;
	ll allot = start;
	for(ll u:adj[start])
	{
		if(u == par)
			continue;
		if(visited[u] == false)
		{
		    houses[u] = allot;
		    allot = u;
			lsize++;
			visited[u] = true;
		}
	}
	ll c = 0;
	if(lsize>0)
	{
		visited[start] = true;
    	c += lsize*2;
    	houses[start] = allot;
	}
	else if(lsize == 0 && start == 0)
	{
		c += 2;
		visited[start] = true;
		ll occupant = adj[0][0];
// 		cout<<adj[0][0]<<" "<<houses[occupant]<<endl;
		houses[start] = houses[occupant];
		houses[occupant] = 0;
	}
	cost += c;
	return c;
}

int main()
{
	fio;
	cin>>n;
	adj.resize(n);
	houses.resize(n);
	rep(i,0,n-1)
	{
		ll u,v;
		cin>>u>>v;
		u--;v--;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	visited.assign(n, false);
	dfs(0,-1);
	cout<<cost<<endl;
	rep(i,0,n)
	{
	    cout<<houses[i]+1<<" ";
	}
}

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define forn(i,n) for(int i=0;i<n;i++)
#define minf numeric_limits<int>::min()
#define inf numeric_limits<int>::max()
#define linf numeric_limits<long>::max()
#define lminf numeric_limits<long>::min()
#define llinf numeric_limits<long long>::max()
#define llminf numeric_limits<long long>::min()
#define forln(i,n) for(long i=0;i<n;i++)
#define forlln(i,n) for(long long i=0;i<n;i++)
#define For(i,s,e) for(int i=s; i<=e; i++)
#define rFor(i,s,e) for(int i=s; i>=e; i--)
#define ll long long
#define l long
#define ivec vector<int>
#define lvec vector<l>
#define llvec vector<ll>
#define iimap map<int, int>
#define llmap map<ll, ll>
#define illmap map<int, ll>
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define ipair pair<int,int>
#define lpair pair<l,l>
#define llpair pair<ll,ll>
#define vec vector
#define CASE int t; cin>>t; while(t--)
#define len(a) a.size()
#define all(a) a.begin(), a.end()

using namespace std;
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

template<typename T> istream & operator>>(istream& in, vec<T> &a){
    forn(i,a.size()) in>>a[i];
    return in;
}
template<typename T> ostream & operator<<(ostream& out, vec<T> &a){
    forn(i,a.size()) out<<a[i]<<" ";
    out<<endl;
    return out;
}
template<typename T> T max(vec<T> a){
    T ans=numeric_limits<T>::min();
    forn(i, a.size()) ans=max(ans, a[i]);
    return ans;
}
template<typename T> T min(vec<T> a){
    T ans=numeric_limits<T>::max();
    forn(i, a.size()) ans=min(ans, a[i]);
    return ans;
}
vec<ivec> g;
ivec sz, par;
void dfs(int v, int p){
    par[v]=p;
    for(int u: g[v]){
        if(u==p)continue;
        dfs(u,v);
        sz[v]++;
    }
}

int main(){
    ios_base::sync_with_stdio(false);
    #ifndef ONLINE_JUDGE
        freopen("../input.txt","r",stdin);
        freopen("../output.txt","w",stdout);
    #endif
    int n;
    cin>>n;
    g.resize(n+1);
    sz.assign(n+1, 0);
    par.assign(n+1, 0);
    forn(i,n-1){
        int u, v;
        cin>>u>>v;
        g[u].pb(v); g[v].pb(u);
    }
    dfs(1, 0);
    ivec pos(n+1);
    forn(i,n) pos[i+1]=i+1;
    int ans=0;
    set<ipair> st;
    For(i,1,n) st.insert({sz[i], i});
    while(len(st)>1){
        auto v=*st.begin();
        st.erase(st.begin());
        auto ip=st.find({sz[par[v.S]], par[v.S]});
        auto p=*ip;
        p.F--;
        sz[p.S]--;
        st.erase(ip);
        st.insert(p);
        if(v.S==pos[v.S]) ans+=2, swap(pos[v.S], pos[p.S]);
        if(len(st)==1 && p.S==pos[p.S]) ans+=2, swap(pos[v.S], pos[p.S]);
    }
    cout<<ans<<endl;
    For(i,1,n) cout<<pos[i]<<" ";
    cout<<endl;
}

#include <iostream>
#include <assert.h>
#include <algorithm>
#include <vector>
#include <set>
#include <string>
#include <queue>
#include <map>
#include <bits/stdc++.h>
#define initrand mt19937 mt_rand(time(0));
#define rand mt_rand()
using namespace std;
//initrand;
const int N = 1e5 + 5;
vector<int> adj[N];
int lbl[N];
int cost = 0;
bool f (int i, int x){
    bool wecon = false;
    for(int j: adj[i]){
        if(j==x) continue;
        bool con = f(j, i);
        if(con){
            wecon = true;
            cost+=2;
            swap(lbl[i], lbl[j]);
        }
    }
    if(!wecon){
        if(i!=1) return true;
        cost+=2;
        swap(lbl[i], lbl[adj[i][0]]);
        return false;
    }
    else return false;
}
int main(){
    int n;
    cin>>n;
    for(int i = 0;i<=n;i++) lbl[i] = i;
    for(int i = 1;i<n;i++){
        int u, v;
        cin>>u>>v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    f(1, 0);
    cout<<cost<<'\n';
    int lbl1[n+1];
    for(int i = 1;i<=n;i++) lbl1[lbl[i]] = i;
    for(int i = 1;i<=n;i++) cout<<lbl1[i]<<' ';
    cout<<'\n';
}

#include <bits/stdc++.h>
#define maxn 100005
std::queue<int>q;
int n,u,v,d[maxn],match[maxn],ans;
std::set<int>ch[maxn],leaf[maxn];
void del(int x) {
//	printf("del %d\n",x);
	for (auto v:ch[x]) {
		ch[v].erase(x);
		leaf[v].erase(x);
		d[v] --;
		if (d[v] == 1) { q.push(v); for (auto x:ch[v]) leaf[x].insert(v); }
	}
}

int main() {
	scanf("%d",&n);
	for(int i=1;i<n;++i){
		scanf("%d%d",&u,&v);
		ch[u].insert(v);
		ch[v].insert(u);
		d[u]++; d[v]++;
	}for(int i=1;i<=n;++i){if(d[i]==1){q.push(i);for (auto v:ch[i]) leaf[v].insert(i);}}
	while (!q.empty()) {
		int u = q.front(); q.pop();

		if (match[u]) continue;//printf("leaf %d\n",u);
		int to = *ch[u].begin();
		if (leaf[to].size()>2) {
			for (auto v:leaf[to])
				if (v != u) { match[u] = v; match[v] = u; del(u); del(v); ans+=4;  break;}
		} else if (leaf[to].size()==2) {
			for (auto v:leaf[to]) {
				if (v != u) { match[u] = to; match[to] = v; match[v] = u; del(v); del(u); del(to); ans += 4;
			//	printf("triple %d %d %d\n",u,to,v);
				break; }
			}
		} else {
		//	printf("up %d %d\n",u,to);
			match[u] = to; match[to] = u; del(u); del(to); ans += 2;
		}
	}printf("%d\n",ans);
	for(int i=1;i<=n;++i){printf("%d ",match[i]);assert(match[i]);}
	return 0;
}

#include<iostream>
#include<vector>
#include<set>
#include<cassert>
#include<algorithm>
using namespace std;
typedef long long int llint;
vector <int> v[100005];
set <pair<llint,llint> > s;
llint bio[100005],cnt[100005],sol[100005];
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n;
	cin >> n;
	llint ret=0;
	for(int i=0;i<n-1;i++) {
		int a,b;
		cin >> a >> b;
		v[a-1].push_back(b-1);
		v[b-1].push_back(a-1);
	}
	for(int i=0;i<n;i++) {
		cnt[i]=v[i].size();
		s.insert(make_pair(cnt[i],i));
	}
	while(!s.empty()) {
		llint cnt1=(*s.begin()).first,x=(*s.begin()).second;
		//cout << x << " " << cnt1 << " " << bio[x] << "\n";
		//if(s.find(make_pair(cnt[6],6))!=s.end()) cout << "abc\n";
		s.erase(s.begin());
		if(s.empty()) break;
		llint par=-1,a=0,ind1=-1,ind2=-1,b=0;
		for(int i=0;i<v[x].size();i++) {
			if(v[x][i]!=-1 && !bio[v[x][i]]) {
				par=v[x][i];
				b+=!bio[par];
				//break;
			}
		}
		//cout << par << "\n";
		//cout << bio[x] << " " << bio[par] << "\n";
		if(bio[x] or b==0) {
			//cout << x << "\n";
			for(int i=0;i<v[x].size();i++) {
				if(v[x][i]==-1 or bio[v[x][i]]) continue;
				s.erase(make_pair(cnt[v[x][i]],v[x][i]));
				cnt[v[x][i]]-=1;
				s.insert(make_pair(cnt[v[x][i]],v[x][i]));
			}
			//cout << x << " " << cnt[0] << "\n";
			bio[x]=1;
			//bio[par]=1;
			//cout << "\n";
			continue;
		}
		for(int i=0;i<v[par].size();i++) {
			if(v[par][i]==-1 or bio[v[par][i]]) continue;
			if(cnt[v[par][i]]==1) {
				if(v[par][i]!=x) {
					sol[v[par][i]]=ind2;
					bio[v[par][i]]=1;
					cnt[v[par][i]]=0;
					if(ind1==-1) ind1=v[par][i];
					ind2=v[par][i];
				}
				a+=1;
				v[par][i]=-1;
			}
		}
		if(a==1) {
			sol[x]=par;
			sol[par]=x;
		}
		else if(a==2) {
			sol[x]=par;
			sol[par]=ind1;
			sol[ind1]=x;
		}
		else {
			sol[ind1]=ind2;
			sol[x]=par;
			sol[par]=x;
		}
		//bio[x]=1;
		//bio[par]=1;
		cnt[x]=0;
		for(int i=0;i<v[x].size();i++) {
			if(v[x][i]==-1 or bio[v[x][i]]) continue;
			//if(cnt[v[x][i]]>1) {
				//cout << "  " << v[x][i] << "\n";
				s.erase(make_pair(cnt[v[x][i]],v[x][i]));
				cnt[v[x][i]]-=1;
				s.insert(make_pair(cnt[v[x][i]],v[x][i]));
			//}
		}
		s.erase(make_pair(cnt[par],par));
		cnt[par]=0;
		s.insert(make_pair(cnt[par],par));
		bio[x]=1;
		bio[par]=1;
		/*if(v[par].size()>1) {
			for(int i=0;i<v[par].size();i++) {
				if(v[par][i]==-1 or bio[v[par][i]]) continue;
				if(cnt[v[par][i]]>1) {
					s.erase(make_pair(cnt[v[par][i]],v[par][i]));
					cnt[v[par][i]]-=1;
					s.insert(make_pair(cnt[v[par][i]],v[par][i]));
				}
			}
		}*/
		ret+=a*2;
		//cout << x << " " << a << "\n";
		//if(s.find(make_pair(cnt[6],6))!=s.end()) cout << "abc\n";
		/*cout << x << " " << a << "\n\n";
		for(int i=0;i<n;i++) {
			cout << cnt[i] << " ";
		}
		cout << "\n";*/
	}
	/*for(int i=0;i<n;i++) {
		assert(sol[i]!=i);
	}*/
	cout << ret << "\n";
	for(int i=0;i<n;i++) {
		cout << sol[i]+1 << " ";
	}
	return 0;
}

#include <iostream>
#include <math.h>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <map>
#include <string.h>
#include <array>
#include <bitset>
#include <assert.h>

using namespace std;

const int maxn=1e5+5;

vector < int > ms[maxn];
int br[maxn];
bitset < maxn > bio;
bitset < maxn > proso;
int val[maxn];
int sol;

void siri(int x){
//	cout << "idem " << x << endl;
	for(int i=0; i<ms[x].size(); i++){
		if(!proso[ms[x][i]]){
			br[ms[x][i]]--;
		}
	}
	proso[x]=1;
	if(bio[x]){
		for(int i=0; i<ms[x].size(); i++){
			if(!proso[ms[x][i]] && br[ms[x][i]]<2){
				siri(ms[x][i]);
			}
		}
		return;
	}
	bio[x]=1;
	bool p=0;
	sol+=2;
	for(int i=0; i<ms[x].size(); i++){
		if(!proso[ms[x][i]]){
			bio[ms[x][i]]=1;
			swap(val[x], val[ms[x][i]]);
			p=1;
			if(br[ms[x][i]]<2){
				siri(ms[x][i]);
			}
		}
	}
	if(!p){
//		cout << "usao " << x << endl;
		swap(val[x], val[ms[x][0]]);
	}
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n;
	cin >> n;
	for(int i=0; i<n; i++){
		val[i]=i;
	}
	int a, b;
	for(int i=0; i<n-1; i++){
		cin >> a >> b;
		a--;
		b--;
		ms[a].push_back(b);
		ms[b].push_back(a);
		br[a]++;
		br[b]++;
	}
	for(int i=0; i<n; i++){
		if(!proso[i] && br[i]<2){
//			cout << "sirim " << i << endl;
			siri(i);
		}
	}
	cout << sol << '\n';
	for(int i=0; i<n; i++){
		assert(val[i]!=i);
		cout << val[i]+1 << " ";
	}
	cout << '\n';
	return 0;
}

#include <bits/stdc++.h>
#define ff first
#define ss second
#define endl '\n'
using namespace std;
const long long INF = (long long) 1e12;
const int mod = (int) 1e9+7;
const int MAXN = (int) 1e5+5;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll n;

ll dp[MAXN][2];
ll st[MAXN][2];
int ans[MAXN];
vector<int> adj[MAXN];
vector<ll> suf[MAXN], pre[MAXN];

void dfs(int v, int par, int state){
    if(dp[v][state] != -1) return;
    //cerr<<v<<" "<<par<<" "<<state<<endl<<flush;

    if(adj[v].size() == 1 && par != -1){
        if(state == 1){ dp[v][1] = 0; st[v][1] = -1;}
        else dp[v][0] = INF;
        return;
    }

    for(int i = 0; i < (int) adj[v].size();i++){
        int j = adj[v][i];
        if(j != par){
            dfs(j, v, 0);
            dfs(j, v, 1);
            pre[v][i] = dp[j][0];
            suf[v][i] = dp[j][0];
        }else{
            pre[v][i] = 0;
            suf[v][i] = 0;
        }
    }
    for(int i = 1; i < (int) adj[v].size(); i++)
        pre[v][i] += pre[v][i - 1];

    for(int i = (int) adj[v].size() - 2; i >= 0; i--)
        suf[v][i] += suf[v][i + 1];
    if(state == 0){

        ll mn = 1e18;
        ll mst = -1;

        int leaf = 0;

        for(int j: adj[v]){
            if(j != par && adj[j].size() == 1)
                leaf++;
        }

        if(leaf >= 2 && leaf != 3){
            ll sim = pre[v].back();
            sim -= leaf*INF;
            sim += 2*(leaf - 2);
            sim += 4;
            if(sim < mn){
                mn = sim;
                mst = -2;
            }
        }

        for(int i = 0; i < (int) adj[v].size();i++){
            int j = adj[v][i];
            if(j != par){
                if(adj[j].size() == 1) leaf--;
                ll sim = 0;
                sim += dp[j][1];

                if(i != 0)
                    sim += pre[v][i - 1];

                if(i != ((int) suf[v].size()) - 1)
                    sim += suf[v][i + 1];
                sim += 2;
                sim -= leaf*INF;
                sim += 2*leaf;
                if(leaf != 1 && sim < mn){
                    mn = sim;
                    mst = i;
                }
                if(adj[j].size() == 1) leaf++;
            }
        }
        dp[v][state] = mn;
        st[v][state] = mst;

    }else if(state == 1){
        int leaf = 0;
        for(int j: adj[v]){
            if(j != par && adj[j].size() == 1)
                leaf++;
        }
        ll mst = -2;
        ll mn = INF;

        if(leaf != 1){
            mn = pre[v].back() + 2*leaf - leaf*INF;
            mst = -1;
        }
        /*if(v == 8)
        cout<<pre[v].back()<<endl;*/

        for(int i = 0; i < (int) adj[v].size(); i++){
            int j = adj[v][i];

            if(j != par){
                if(adj[j].size() == 1){
                    leaf--;
                }
                //if(v == 8) cout<<i<<" "<<leaf<<endl;
                ll sim = 0;
                sim += dp[j][1];
                if(i != 0)
                    sim += pre[v][i - 1];

                if(i != (int) adj[v].size() - 1)
                    sim += suf[v][i + 1];

                sim -= leaf*INF;

                sim += 2;
                sim += 2*leaf;
                if(leaf != 1 && sim < mn){
                    mst = i;
                    mn = sim;
                }
                if(adj[j].size() == 1){
                    leaf++;
                }
            }
        }

        dp[v][state] = mn;
        st[v][state] = mst;
    }
}

void bt(int v, int state, int bas, int par){
    //cerr<<v<<" "<<state<<" "<<bas<<" "<<par<<" "<<st[v][state]<<endl;
    if(state == 1){
        if(st[v][state] == -1){
            ans[v] = bas;
            int fl = -1, lf = -1;
            for(int j: adj[v]){
                if(j != par){
                    if(adj[j].size() == 1){
                        if(fl == -1){
                            fl = j;
                            lf = j;
                        }else{
                            ans[lf] = j;
                            lf = j;
                        }
                    }else
                        bt(j, 0, -1, v);
                }
            }
            if(lf != -1)
                ans[lf] = fl;
        }else{
            ans[v] = adj[v][st[v][state]];
            int lf = -1, fl = -1;
            for(int j: adj[v]){
                if(j == par) continue;
                if(j == adj[v][st[v][state]]){
                    bt(j, 1, bas, v);
                }else if(adj[j].size() == 1){
                    if(fl == -1){
                        fl = j;
                        lf = j;
                    }else{
                        ans[lf] = j;
                        lf = j;
                    }
                }else{
                    bt(j, 0, -1, v);
                }
            }
            ans[lf] = fl;
        }
    }else{
        if(st[v][state] == -2){
                int ff = -1, ss = -1;
                for(int i = 0; i < (int) adj[v].size(); i++){
                    int j = adj[v][i];
                    if(j != par){
                        if(adj[j].size() == 1 && ff == -1){
                            ff = j;
                            break;
                        }
                    }
                }

                for(int i = (int) adj[v].size() - 1; i >= 0; i--){
                    int j = adj[v][i];
                    if(j != par){
                        if(adj[j].size() == 1 && ss == -1){
                            ss = j;
                            break;
                        }
                    }
                }
                //cout<<ff<<" "<<v<<" "<<ss<<endl;
                ans[ff] = v;
                ans[v] = ss;
                ans[ss] = ff;
                int lf = -1, fl = -1;
                for(int i = (int) adj[v].size() - 1; i >= 0; i--){
                    int j = adj[v][i];
                    if(j != par){
                        if(j != ss && j != ff){
                            if(adj[j].size() == 1){
                                if(fl == -1){
                                    fl = lf = j;
                                }else{
                                    ans[fl] = j;
                                    fl = j;
                                }
                            }else{
                                bt(j, 0, -1, v);
                            }
                        }
                    }
                }
                ans[fl] = lf;
            }else{
                ans[v] = adj[v][st[v][state]];
                int lf = -1, fl = -1;
                for(int j: adj[v]){
                    if(j == par) continue;
                    if(j == adj[v][st[v][state]]){
                        bt(j, 1, v, v);
                    }else if(adj[j].size() == 1){
                        if(lf == -1){
                            lf = fl = j;
                        }else{
                            ans[lf] = j;
                            lf = j;
                        }
                    }else{
                        bt(j, 0, -1, v);
                    }
                }
                ans[lf] = fl;
            }
    }
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);cout.tie(nullptr);

    #ifdef Local
        freopen("C:/Users/Admin/Desktop/Yazilim/C/IO/int.txt","r",stdin);
        freopen("C:/Users/Admin/Desktop/Yazilim/C/IO/out.txt","w",stdout);
    #endif
    memset(dp, -1, sizeof(dp));
    cin>>n;

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

    dfs(1, -1, 0);

    bt(1, 0, -1, -1);
    //cout<<st[10][0]<<endl;
    cout<<dp[1][0]<<endl;
    for(int i = 1; i <= n; i++){
        cout<<ans[i]<<" ";
    }

    #ifdef Local
        cout<<endl<<fixed<<setprecision(2)<<1000.0 * clock() / CLOCKS_PER_SEC<< " milliseconds ";
    #endif
}

#include<bits/stdc++.h>

using namespace std;

#define ll  long long
#define ld  double

#define sz(x)   (int)x.size()
#define all(x)  x.begin(),x.end()

#define pb  emplace_back
#define X   first

const int   N   = 1e5 + 5;

vector<int> g[N];

int p[N];
int a[N];
int d[N];

int main()  {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int n;  cin >> n;

    for(int i = 1 ; i < n ; ++i)    {
        int x;  cin >> x;
        int y;  cin >> y;

        g[x].pb(y);
        g[y].pb(x);
    }
    queue<int>  qu;

    for(int i = 1 ; i <= n ; ++i)   {
        a[i] = i;
        d[i] = g[i].size();

        if (d[i] == 1)  qu.push(i);
    }
    int ans = 0;

    while (qu.size())   {
        int u = qu.front(); qu.pop();

        if (d[u] == 1)  {
            d[u] = 0;

            for(int v : g[u])   if (d[v])   {
                if (u == a[u])  {
                    swap(a[u],a[v]);
                    ans += 2;
                }
                if ((--d[v]) == 1)
                    qu.push(v);
            }
        }
        else if (a[u] == u) {
            int v = g[u][0];
            swap(a[u],a[v]);
            ans += 2;
        }
    }
    cout << ans << "\n";

    for(int i = 1 ; i <= n ; ++i)   p[a[i]] = i;
    for(int i = 1 ; i <= n ; ++i)   cout << p[i] << " ";
}

#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef double db;
typedef vector<long long> vl;
typedef pair<long long , long long > pl;
const int N=1e6+1;
#define po pop_back
#define pb push_back
#define mk make_pair
#define lw lower_bound
#define up upper_bound
#define ff first
#define ss second
#define boost ios_base::sync_with_stdio(); cin.tie(0); cout.tie(0);
#define MOD 1000000007
#define MAX 1e18
#define MIN -1e18
#define rep(i,a,b) for(ll i=a;i<=b;i++)
#define per(i,a,b) for(ll i=b;i>=a;i--)
#define con continue
#define freopen freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
#define PI 3.14159265358979323846264338327950288419716939937510582097494459230781640628
// typedef tree<ll , null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
// template< typename T>
// using indexed_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
ll n,m,ans,mid,mn,mx,cnt,T,sum,h1,h2,e[1234567],b[1234567],c[1234567],d[1<<20],k,i,j,l,x,y,r,z,h,a[1234567],w,c1[1234][1234];
bool used[1234567],used1[1234567];
pl p[1234567];
string s,s1[1234567];
map<ll,ll> mp;
map<pl,ll> mpl;
vl vec,vec1,ansvec;
vector<ll> v[1234567],v1[1234567];
vector<ll> vp[1234567];
set<ll> st;
queue<ll> que;
ll col[1234567],res[1234567];
void dfs(ll x,ll y){
    for(auto u:v[x]){
        if(u == y) continue;
        dfs(u , x);
    }
    for(auto &u:v[x]){
        if(u == y) continue;
        if(col[u] != col[x] && !res[u] && !res[x]){
            cnt++;
            res[u] = res[x] = cnt;
            que.push(x);
            que.push(u);
        }
    }
    return;
}
void bfs(){
    que.push(1);
    col[1] = 1;
    while(!que.empty()){
        ll x = que.front();
        que.pop();
        for(auto u:v[x]){
            if(!col[u]){
                col[u] = col[x] % 2 + 1;
                que.push(u);
            }
        }
    }
    cnt = 0;
    dfs(1,0);
    while(!que.empty()){
        ll x = que.front();
        que.pop();
        for(auto u:v[x]){
            if(!res[u]){
                res[u] = res[x];
                que.push(u);
            }
        }
    }
    return;
}
int main(){
    cin>>n;
    rep(i,2,n){
        cin>>x>>y;
        v[x].pb(y);
        v[y].pb(x);
    }
    bfs();
    rep(i,1,n){
        v1[res[i]].pb(i);
    }
    for(ll i = 1; i <= cnt; i++) {
        ans += (v1[i].size() - 1) * 2;
        for(ll j = 0; j < v1[i].size(); j++)
            c[v1[i][j]] = v1[i][(j + 1) % v1[i].size()];
    }
    cout<<ans<<'\n';
    rep(i,1,n)cout<<c[i]<<" ";
}

#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 5;

int n, deg[N], pos[N];
vector<int> g[N], cc[N];

int main() {
    scanf("%d", &n);
    for(int i = 1, a, b; i < n; i++) {
        scanf("%d %d", &a, &b);
        g[a].emplace_back(b);
        g[b].emplace_back(a);
        ++deg[a], ++deg[b];
    }
    queue<int> Q;
    for(int i = 1; i <= n; i++) if(deg[i] == 1)
        Q.emplace(i);

    int cnt = n;
    while(cnt > 2) {
        int u = Q.front(); Q.pop();
        --cnt;
        for(int v : g[u]) if(deg[v] != 1) {
            if(cc[u].empty()) cc[v].emplace_back(u);
            if(--deg[v] == 1) Q.emplace(v);
            break;
        }
    }
    if(Q.size() == 1) {
        int a = Q.front();
        if(cc[a].empty()) {
            for(int v : g[a]) if(!cc[v].empty()) {
                cc[v].emplace_back(a);
                break;
            }
        }
    }
    if(Q.size() == 2) {
        int a = Q.front(); Q.pop();
        int b = Q.front();
        if(cc[a].empty() || cc[b].empty()) {
            if(cc[a].size() > cc[b].size()) swap(a, b);
            cc[b].emplace_back(a);
        }
    }
    int ans = 0;
    for(int i = 1; i <= n; i++) if(cc[i].size()) {
        pos[i] = cc[i][0];
        for(int j = 0; j < cc[i].size() - 1; j++)
            pos[cc[i][j]] = cc[i][j + 1];
        pos[cc[i].back()] = i;
        ans += 2 * cc[i].size();
    }
    printf("%d\n", ans);
    for(int i = 1; i <= n; i++) printf("%d ", pos[i]);

    return 0;
}

#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn = 100005;
int n;
vector<int> g[maxn];
int res[maxn];
int rez = 0;
void dfs(int v, int p){
   for(auto u : g[v]){
      if(u != p){
         dfs(u, v);
         if(res[u] == u){
            rez += 2;
            swap(res[u], res[v]);
         }
      }
   }
}
int main()
{
   ios::sync_with_stdio(false);
   cin.tie(nullptr);
   cout.tie(nullptr);
   cin >> n;
   for(int i=1;i<n;i++){
      int u, v;
      cin >> u >> v;
      g[u].pb(v);
      g[v].pb(u);
   }
   for(int i=1;i<=n;i++)res[i] = i;
   dfs(1, -1);
   if(res[1] == 1){
      rez += 2;
      swap(res[1], res[g[1][0]]);
   }
   cout << rez << endl;
   for(int i=1;i<=n;i++)cout << res[i] << " ";
   return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll,ll> ii;
#define F first
#define S second
typedef vector<ii> vii;
const ll INF = 1e18;

const ll max(ll x, ll y){
    return (x > y) ? x : y;
}

const ll min(ll x, ll y){
    return (x < y) ? x : y;
}

ll n;
vvi adjList;
vi perm;
vvi dist;

vi p;
vector<set<ll> >  c;
vi topsort;
void DFS(ll v){
    topsort.push_back(v);
    for (auto u: adjList[v]){
        if (u != p[v]){
            p[u] = v;
            c[v].insert(u);
            DFS(u);
        }
    }
}

void get_perm (){
    reverse(topsort.begin(),topsort.end());
    vi perm;
    for (ll i = 0; n > i; i++) perm.push_back(i);
    ll ans = 0;
    for (auto v: topsort){
        if (perm[v] == v){
            if (v != 0)swap(perm[v],perm[p[v]]);
            else swap(perm[v],perm[*c[0].begin()]);
            ans += 2;
        }
    }
    cout << ans << endl;
    for (auto i: perm) cout << i+1 << " ";
}

int main(){
    cin >> n;
    adjList.resize(n);
    p.assign(n,-1);
    c.resize(n);
    for (ll i = 0; n-1 > i; i++){
        ll a,b;
        cin >> a >> b;
        a--; b--;
        adjList[a].push_back(b);
        adjList[b].push_back(a);
    }
    DFS(0);
    get_perm();
}

#include <bits/stdc++.h>

typedef long long ll;
typedef long double ld;
#define FASTIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define PRECISION std::cout << std::fixed << std::setprecision(20);

using namespace std;

const ll inf = 1e18;
const int N = 1e6;

void solve(){
    int n,u,v;
    cin>>n;

    vector<array<int,2>>edges;

    vector<vector<int>>adj(n+1);
    vector<int>pos(n+1), dist(n+1);
    vector<int>deg(n+1);

    for(int i=1;i<n;i++){
        cin>>u>>v;
        adj[u].push_back(v);
        adj[v].push_back(u);
        pos[u] = u;
        pos[v] = v;
    }
    int ans = 0;

    function<void(int, int)>dfs = [&](int node,int par){
        for(int nxt: adj[node]){
            if(nxt!=par){
                dfs(nxt, node);
            }
        }

        if(pos[node]==node){
            if(!par){
                swap(pos[node], pos[adj[node][0]]);
            }else{
                swap(pos[node], pos[par]);
            }
            ans+=2;
        }
    };

    dfs(1, 0);

    vector<int>store(n+1);

    cout<<ans<<"\n";
    for(int i=1;i<=n;i++){
        store[pos[i]] = i;
    }

    for(int i=1;i<=n;i++){
        cout<<store[i]<<" \n"[i==n];
    }
}

int main(){
    FASTIO;
    PRECISION;

    int t = 1;
    // cin>>t;

    while(t--){
        solve();
    }
}

#include <bits/stdc++.h>
#define pb push_back
#define vii vector<int>
#define task "barnpainting"
#define pll pair<ll, ll>
#define pii pair< ll, pair< ll, ll > >
#define fi first
#define se second

using namespace std;
using ll = long long;
using ull = unsigned long long;
const int N = 3e5 + 5;
const int Q = 2e6+5;
const ll mod = 1e9+7;
ll m, n, k, t, T, ans, u, v, tong;
ll a[N], c[N], b[N], d[N], lab[N], st[N], dp[N], in[N], res[N], h[N], out[N], P[N][20], fe[N];
string sa, sb;
vector<ll> kq, adj[N];
vector<ll> adt[N], kind[N];
ll dx[4] = {0, 0, -1, 1};
ll dy[4] = {1, -1, 0, 0};
bool ok;
void dfs(ll u, ll p)
{
    for(int i = 1; i <= 18; i ++)P[u][i] = P[P[u][i-1]][i-1];
    in[u] = ++k;
    d[k] = u;
    for(ll v : adj[u])
    {

        if(v == p)continue;
        h[v] = h[u] + 1;
        P[v][0] = u;
        dfs(v, u);
    }
    out[u] = k;
    if(a[u] == u)
    {
        if(u != 1)swap(a[u], a[P[u][0]]);
        else swap(a[u], a[adj[u].back()]);
        ans += 2;
    }
}
struct dang
{
    ll x, y, c, pos, w;
};
ll lca(ll u, ll v)
{
    if(h[u] < h[v])swap(u, v);
    ll log = log2(h[u]);
    for(int i = log; i >= 0; i --)
    {
        if(h[u] >= h[v] + (1 << i))u = P[u][i];
    }
    if(u == v)return u;
    for(int i = log; i >= 0; i --)
    {
        if(P[u][i] != 0 && P[u][i] != P[v][i])
        {
            u = P[u][i];
            v = P[v][i];
        }
    }
    return  P[u][0];
}
ll dis(ll u, ll v)
{
    return h[u] + h[v] - 2 * h[lca(u, v)];
}

void bfs(ll u, ll p)
{

    for(ll v : adj[u])
    {
        if(v == p)
            continue;
        bfs(v, u);
        P[u][1] = (P[u][1] * (P[v][3] + P[v][2]) % mod) %mod;
        P[u][2] = (P[u][2] * (P[v][1] + P[v][3]) % mod) %mod;
        P[u][3] = (P[u][3] * (P[v][1] + P[v][2]) % mod) %mod;

    }

}
bool cmp(const dang& X, const dang& Y)
{
    return X.w > Y.w;
}
void up(ll id)
{
    for(; id <= n; id += id & -id)fe[id] += 1;
}
ll gets(ll id)
{
    ll total = 0;
    for(; id; id -= id & -id)total += fe[id];
    return total;
}
ll pw(ll k, ll n)
{
    ll total = 1;
    for(; n; n >>= 1)
    {
        if(n & 1)total = (total * k) % mod;
        k = (k * k) % mod;
    }
    return total;
}
ll cal(ll u, ll v)
{
    if(u > v)return 0;
    return a[v] * b[u] % mod * b[v-u] % mod;
}
void sol()
{
    cin >> n;
    for(int i = 1; i < n; i ++)
    {
        ll x, y;
        cin >> x >> y;
        adj[x].pb(y);
        adj[y].pb(x);
    }
    for(int i = 1; i <= n; i++)a[i] = i;
    dfs(1, 1);
    cout << ans << '\n';
    for(int i = 1; i <= n; i ++)cout << a[i] <<" ";
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    if(fopen(task".in", "r"))
    {
		freopen(task".in","r",stdin);
		freopen(task".out","w",stdout);
    }
    int ntest;
    ntest = 1;
    //cin >> ntest;
    while(ntest -- > 0)
    sol();
}
/*
3 2 10
2 1 1
1 2 1
2 2 1
1 1 2
3 1 2
1 2 2
2 2 2
2 1 2
3 2 4
2 1 5
*/

#include <bits/stdc++.h>
#define ld long double
#define endl "\n"
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pb(x) push_back(x)
#define mp(a,b) make_pair(a,b)
#define ms(v,x) memset(v,x,sizeof(v))
#define all(v) v.begin(),v.end()
#define ff first
#define ss second
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define per(i, a, b) for(int i = b-1; i>=a ; i--)
#define trav(a, x) for(auto& a : x)
#define allin(a , x) for(auto a : x)
#define td(v) v.begin(),v.end()
#define sz(v) (int)v.size()
//#define int long long
using namespace std;
typedef vector<int> vi;
#define y1 abacaba
#define left sefude
#define db(x) cerr << #x <<" == "<<x << endl;
#define db2(x,y) cerr<<#x <<" == "<<x<<", "<<#y<<" == "<<y<<endl;
#define db3(x,y,z) cerr << #x<<" == "<<x<<", "<<#y<<" == "<<y<<", "<<#z<<" == "<<z<<endl;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
inline ll mod(ll n, ll m ){ ll ret = n%m; if(ret < 0) ret += m; return ret; }
ll gcd(ll a, ll b){return (b == 0LL ? a : gcd(b, a%b));}
ll exp(ll a,ll b,ll m){
    if(b==0LL) return 1LL;
    if(b==1LL) return mod(a,m);
    ll k = mod(exp(a,b/2,m),m);
    if(b&1LL){
        return mod(a*mod(k*k,m),m);
    }
    else return mod(k*k,m);
}
const int N = 100100;
ll res = 0;
vi g[N];
int V[N];

void dfs(int v,int p = -1){

  for(int to : g[v])if(to!=p){
    dfs(to,v);
    if(V[to]!=-1)continue;
    if(V[v] == -1){// conecta
        V[v] = to;
        V[to] = v;
        res+=2;
    }else{// troca
      V[to] = V[v];
      V[v] = to;
      res+=2;
    }

  }

}

int32_t main(){
  fastio;
  int n;
  cin >> n;
  ms(V,-1);
  for(int i=0;i<n-1;i++){
    int a,b;
    cin >> a >> b;
    g[a].pb(b);
    g[b].pb(a);
  }
  dfs(1);

  if(V[1]==-1){
    int a = g[1][0];
    V[1] = V[a];
    V[a] = 1;
    res+=2;
  }

  cout << res << endl;
  for(int i=1;i<=n;i++){
    cout << V[i]<<" ";
  }
  cout << endl;
  // math -> gcd it all
  // Did u check N=1? Did you switch N,M?
}

#include <bits/stdc++.h>
using namespace std;

#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
void fst()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
}
int mod = 1e9 + 7;
int binpow(int a, int b)
{
    int res = 1;
    while (b > 0)
    {
        if (b & 1)
        {
            res = res * a;
            res %= mod;
        }
        a = a * a;
        a %= mod;
        b >>= 1;
        b %= mod;
    }
    return res;
}
#define Darr(x)           \
    cerr << #x << " = ";  \
    for (auto i : x)      \
        cerr << i << " "; \
    cerr << "\n";

#define all(a) a.begin(), a.end()
#define pb push_back
#define deb(x) cerr << #x << "=" << x << " ";
#define int long long
#define R(n) for (int i = 0; i < n; i++)
#define Rj(n) for (int j = 0; j < n; j++)
#define F(i, a, n) for (int i = a; i <= n; i++)
#define MN(x, y) x = min(x, y);
#define MX(x, y) x = max(x, y);
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;

int n, k, m, tc, x, y, u, v;
int MS = 1e5;

int32_t main()
{
    fst();
    cin >> n;
    vector<vi> adj(n + 1);
    vi deg(n + 1), vis(n + 1), par(n + 1);
    vi pos(n + 1);
    R(n)
    pos[i + 1] = i + 1;
    int id = 0;
    R(n - 1)
    {
        cin >> x >> y;
        adj[x].pb(y);
        adj[y].pb(x);
        deg[y]++;
        deg[x]++;
        if (deg[y] > deg[id])
            id = y;
        if (deg[x] > deg[id])
            id = x;
    }
    int dist = 0;
    function<void(int)> dfs = [&](int x) {
        vis[x] = 1;
        for (auto i : adj[x])
        {
            if (!vis[i])
                par[i] = x, dfs(i);
        }
        if (deg[x] == 1)
        {
            if (pos[x] == x)
            {
                swap(pos[par[x]], pos[x]);
                dist += 2;
            }
            deg[x]--;
            deg[par[x]]--;
        }
    };
    dfs(id);
    par[id] = adj[id][0];

    if (pos[id] == id)
    {
        dist += 2;
        swap(pos[id], pos[par[id]]);
    }

    cout << dist << "\n";
    R(n) cout << pos[i + 1] << " ";
}

/*
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
*/
#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define loop(i,n) for(int i=0;i<n;i++)
#define lop(i,a,b) for(int i=a;i<=b;i++)
#define int long long int
typedef vector< int > vi;
typedef vector< vi > vvi;
typedef pair< int,int > pii;
#define mp make_pair
#define sz(a) int((a).size())
#define pb push_back
#define tr(container, it) for(auto it = container.begin(); it != container.end(); it++)
#define all(a) a.begin(), a.end()
#define allr(a) a.rbegin(), a.rend()
#define so(v) sort(all(v))
#define sod(v) sort(allr(v))
#define mfind(c, element) (c.find(element) != c.end())
#define gfind(c, element) (find(all(c),element) != c.end())
#define gfindindex(c, element) (find(all(c),element)-c.begin())
#define vp(x) cout << #x << " --> " << x << endl;
#define cp1(v) cout << #v << "--->" << endl; for (auto it : v)  cout << it << " "; cout << endl;
#define cpp1(v) cout << #v << "-->" << endl; for(auto it:v){cout << it.fi << " " << it.se << endl;}
#define ccp1(v) cout << #v << "--->" << endl;for (auto it:v){for(auto it1 : it){cout << it1 << " ";}cout<< endl;}
#define bsnp np3(low,high,mid);
#define hmm1 high=mid-1;
#define lmp1 low=mid+1;
#define hmp1 high=mid+1;
#define lmm1 low=mid-1;
#define fi first
#define se second
#define endl "\n"
#define np1(x) cout<<#x<<"="<<x<<'\n'
#define np2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<'\n'
#define np3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<'\n'
struct cmp {
	bool operator() (const pair<int, int> &a, const pair<int, int> &b) const {
		int lena = a.second - a.first + 1;
		int lenb = b.second - b.first + 1;
		if (lena == lenb) return a.first < b.first;
		return lena > lenb;
	}
};//comparison used for sorting set<pair<i,i>,cmp>
bool myComparison(const pair<int,int> &a,const pair<int,int> &b)
{
       return a.second>b.second;
//increasing ordr basedd on 2nd
};
int n,m;
vvi edges;
vector<bool> visited;
main()
{
IOS;
cin>>n;
m=n-1;
int x,y;
edges.clear();visited.clear();
edges.resize(n+1);
visited.resize(n+1,false);
vi deg(n+1,0),house(n+1);
set<int> leafs;
lop(i,1,n) house[i]=i;
loop(i,m)
{
    cin>>x>>y;
    edges[x].pb(y);
    edges[y].pb(x);
    deg[x]++;
    deg[y]++;
}

lop(i,1,n)
if(deg[i]==1) leafs.insert(i);
int ans=0;
while(!leafs.empty())
{
    auto x=*leafs.begin();
    leafs.erase(x);
    if(visited[x])
    {
        for(auto u:edges[x])
        {
            deg[u]--;
            if(deg[u]==1)
            leafs.insert(u);
        }
        continue;
    }
    visited[x]=true;
    bool f=false;
    for(auto u:edges[x])
    {
        if(!visited[u] && !f)
        {
            f=true;
            visited[u]=true;
            ans+=2;
            swap(house[x],house[u]);
        }
        deg[u]--;
        if(deg[u]==1) leafs.insert(u);
    }
    if(f) continue;
    else
    {
        ans+=2;
        auto z=*edges[x].begin();
        swap(house[x],house[z]);
    }

}
lop(i,1,n)
if(!visited[i])
{
    ans+=2;
    auto z=*edges[i].begin();
    swap(house[z],house[i]);
}

cout<<ans<<endl;
lop(i,1,n)
cout<<house[i]<<" ";

return 0;
}

#include <bits/stdc++.h>

#define mp make_pair
#define pb push_back
#define f first
#define s second
#define ll long long
#define forn(i, a, b) for(int i = (a); i <= (b); ++i)
#define forev(i, b, a) for(int i = (b); i >= (a); --i)
#define VAR(v, i) __typeof( i) v=(i)
#define forit(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define file(s) freopen(s".in","r",stdin); freopen(s".out","w",stdout);

using namespace std;

const int maxn = (int)2e5 + 100;
const int maxm = (int)1e6 + 100;
const int mod = (int)998244353;
const int P = (int) 1e6 + 7;
const double pi = acos(-1.0);

#define inf mod

typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> Vll;
typedef vector<pair<int, int> > vpii;
typedef vector<pair<ll, ll> > vpll;

int n, p[maxn];
vi g[maxn];
bool ban[maxn];
int ans, lvl[maxn];
unordered_map<int, bool> was[maxn];
void dfs(int v, int par = -1){
	vi add;
	for(auto to : g[v]){
		if(to == par) continue;
		lvl[to] = lvl[v] + 1;
		dfs(to, v);
		if(!ban[to]) add.pb(to);
	}
	if(sz(add) == 1){
	    ans += 2;
		ban[v] = ban[add.back()] = 1;
		p[v] = add.back();
		p[add.back()] = v;
	    return;
	}
	forn(i, 0, sz(add) - 1){
		if(!i){
		    p[v] = add[i];
		    p[add[i]] = add[i + 1];
		    ans += 3;
		    continue;
		}
		if(i == sz(add) - 1){
		   p[add[i]] = v;
		   ans++;
		   continue;
		}
		p[add[i]] = add[i + 1];
		ans += 2;
	}
	for(auto x : add) ban[x] = 1;
	ban[v] = sz(add);

}
int a[maxn], mn = inf, f[maxn];
void solve(int root){
	forn(i, 1, n) ban[i] = p[i] = 0;
	lvl[root] = 0;
	ans = 0;
	dfs(root);
	forn(i, 1, n) f[p[i]] = i;
	if(!ban[root]){
		int res = inf, go;
		forn(i, 1, n){
			if(i == root) continue;
			int cur = ans - (was[i][f[i]] ? 1 : 2) + lvl[i] + lvl[f[i]];
			if(res > cur)
				res = cur, go = i;
		}
		p[f[go]] = root;
		p[root] = go;
		ans = res;
	}
	if(mn > ans){
		mn = ans;
		forn(i, 1, n) a[i] = p[i];
	}
}
void solve() {
	scanf("%d", &n);
	forn(i, 1, n - 1){
		int u, v;
		scanf("%d%d", &u, &v);
		g[u].pb(v);
		g[v].pb(u);
		was[u][v] = was[v][u] = 1;
	}
	solve(1);
	printf("%d\n", mn);
	forn(i, 1, n) printf("%d ", a[i]);
	forn(i, 1, n) assert(a[i] != i);
	sort(a + 1, a + n + 1);
	forn(i, 1, n) assert(a[i] == i);
}

int main () {
	int t = 1;
	//scanf("%d", &t);
	while(t--) solve();
}

#include <bits/stdc++.h>
using namespace std;

#define MOD 1000000007
#define ll long long int
#define vi vector<int>
#define vii vector<vector<int> >

vii graph(100005,vi());

bool was[100005];
int goal[100005];
int invgoal[100005];
int cost = 0;

void dfs(int v) {
	was[v] = true;
	vector<int> tomove;
	for(int u : graph[v]) {
		if(!was[u]) {
			dfs(u);
			if(goal[u] == 0) {
				tomove.push_back(u);
			}
		}
	}
	if(tomove.size()) {
		cost+= tomove.size()*2;
		for(int i = 1; i < tomove.size(); i++) {
			goal[tomove[i-1]] = tomove[i];
			invgoal[tomove[i]] = tomove[i-1];
		}
		goal[tomove[tomove.size()-1]] = v;
		invgoal[v] = tomove[tomove.size()-1];
		goal[v] = tomove[0];
		invgoal[tomove[0]] = v;
	} else if(v == 1) {
		goal[1] = graph[1][0];
		goal[invgoal[graph[1][0]]] = 1;
		cost+= 2;
	}
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	int n;
	cin >> n;
	for(int i = 1; i < n; i++) {
		int a,b;
		cin >> a >> b;
		graph[a].push_back(b);
		graph[b].push_back(a);
	}
	dfs(1);
	cout << cost << "\n";
	for(int i = 1; i <= n; i++) {
		cout << goal[i] << " ";
	}
	return 0;
}

//   __________________
//  | ________________ |
//  ||          ____  ||
//  ||   /\    |      ||
//  ||  /__\   |      ||
//  || /    \  |____  ||
//  ||________________||
//  |__________________|
//  \###################\
//   \###################\
//    \        ____       \
//     \_______\___\_______\
// An AC a day keeps the doctor away.

#pragma GCC optimize("Ofast")
#pragma loop_opt(on)
#include <bits/extc++.h>
#ifdef local
#define safe std::cerr<<__PRETTY_FUNCTION__<<" line "<<__LINE__<<" safe\n"
#define debug(...) qqbx(#__VA_ARGS__,  __VA_ARGS__)
template <typename H, typename ...T> constexpr void qqbx(const char *s, const H &h, T &&...args) {
    for(; *s && *s != ','; ++s) if(*s != ' ') std::cerr << *s;
    std::cerr << " = " << h << (sizeof...(T) ? ", " : "\n");
    if constexpr(sizeof...(T)) qqbx(++s, args...); // C++ 17!
}
#else
#include <bits/stdc++.h>
#define debug(...) ((void)0)
#define safe ((void)0)
#endif // local
#define all(v) begin(v),end(v)
#define get_pos(v,x) int(lower_bound(begin(v),end(v),x)-begin(v))
#define sort_uni(v) sort(begin(v),end(v)),v.erase(unique(begin(v),end(v)),end(v))
#define pb emplace_back
#define ff first
#define ss second
#define mem(v,x) memset(v,x,sizeof v)

using namespace std;
using namespace __gnu_pbds;
typedef int64_t ll;
typedef long double ld;
typedef pair<ll,ll> pll;
typedef pair<ld,ld> pld;
template <typename T> using max_heap = std::priority_queue<T,vector<T>,less<T> >;
template <typename T> using min_heap = std::priority_queue<T,vector<T>,greater<T> >;
template <typename T> using rbt = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
constexpr ld PI = acos(-1), eps = 1e-9;
constexpr ll N = 1000325, INF = 1e18, MOD = 1000000007, K = 14699, inf = 1e9;
constexpr inline ll cdiv(ll x, ll m) { return x/m + ((x<0 ^ m>0) && (x%m)); } // ceiling divide
constexpr inline ll modpow(ll e,ll p,ll m=MOD) { ll r=1; for(e%=m;p;p>>=1,e=e*e%m) if(p&1) r=r*e%m; return r; }

int dp[N][2];
vector<int> g[N];
int P[N], ans;
void dfs(int i, int p) {
    dp[i][1] = 1;
    for(int j: g[i]) if(j != p) {
        dfs(j, i);
        dp[i][0] += max(dp[j][0], dp[j][1]);
        dp[i][1] += dp[j][0];
    }
}
void dfs2(int i, int p, int f) {
    /* if(f) swap(P[i], P[p]), ++ans; */
    for(int j: g[i]) if(j != p) {
        int t = f ? 0 : dp[j][1] > dp[j][0];
        dfs2(j, i, t);
    }
    if(p && P[i] == i) swap(P[i], P[p]), ans += 2;
}
signed main() {
    ios_base::sync_with_stdio(0), cin.tie(0);
    int n;
    cin >> n;
    for(int i = 1; i < n; i++) {
        int a, b;
        cin >> a >> b;
        g[a].pb(b), g[b].pb(a);
    }
    for(int i = 1; i <= n; i++) P[i] = i;
    dfs(1, 0);
    dfs2(1, 0, 0);
    if(P[1] == 1) ans += 2, swap(P[1], P[g[1][0]]);
    cout << ans << '\n';
    for(int i = 1; i <= n; i++) cout << P[i] << (i==n ? '\n' : ' ');
}

#include <bits/stdc++.h>

using namespace std;

#define mp make_pair
#define fr first
#define sc second

long long n,cr,z=0,sq[100069],inf=1e18;
vector<long long> al[100069],al2[100069];
bitset<100069> vtd,vtd2,dp;

void dfs(long long x)
{
	long long i,ii,sz=al[x].size(),l;

	vtd[x]=1;
	dp[x]=1;
	for(i=0;i<sz;i++)
	{
		l=al[x][i];
		if(!vtd[l])
		{
			dfs(l);
			if(dp[l])
			{
				dp[x]=0;
				z+=2;
				al2[x].push_back(l);
			}
		}
	}
	if(dp[x]&&x==1)
	{
		z+=2;
		al2[x].push_back(l);
	}
}

void trv(long long x)
{
	long long i,sz=al2[x].size(),l;

	vtd2[x]=1;
	for(i=0;i<sz;i++)
	{
		l=al2[x][i];
		if(!vtd2[l])
		{
			trv(l);
		}
	}
	sq[x]=cr;
	cr=x;
}

void bt(long long x)
{
	long long i,sz=al[x].size(),l;

	vtd[x]=1;
	if(!vtd2[x])
	{
		cr=x;
		trv(x);
	}
	for(i=0;i<sz;i++)
	{
		l=al[x][i];
		if(!vtd[l])
		{
			bt(l);
		}
	}
}

int main()
{
	long long i,k,l;

	scanf("%lld",&n);
	for(i=0;i<n-1;i++)
	{
		scanf("%lld%lld",&k,&l);
		al[k].push_back(l);
		al[l].push_back(k);
	}
	dfs(1);
	vtd.reset();
	bt(1);
	printf("%lld\n",z);
	for(i=1;i<=n;i++)
	{
		printf("%lld%c",sq[i]," \n"[i==n]);
	}
}

#include <iostream>
#include <assert.h>
#include <algorithm>
#include <vector>
#include <set>
#include <string>
#include <queue>
#include <map>
#include <bits/stdc++.h>
#define initrand mt19937 mt_rand(time(0));
#define rand mt_rand()
using namespace std;
//initrand;
const int N = 1e5 + 5;
vector<int> adj[N];
int lbl[N];
int cost = 0;
bool f (int i, int x){
    bool wecon = false;
    for(int j: adj[i]){
        if(j==x) continue;
        bool con = f(j, i);
        if(con){
            wecon = true;
            cost+=2;
            swap(lbl[i], lbl[j]);
        }
    }
    if(!wecon){
        if(i!=1) return true;
        cost+=2;
        swap(lbl[i], lbl[adj[i][0]]);
        return false;
    }
    else return false;
}
int main(){
    int n;
    cin>>n;
    for(int i = 0;i<=n;i++) lbl[i] = i;
    for(int i = 1;i<n;i++){
        int u, v;
        cin>>u>>v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    f(1, 0);
    cout<<cost<<'\n';
    int lbl1[n+1];
    for(int i = 1;i<=n;i++) lbl1[lbl[i]] = i;
    for(int i = 1;i<=n;i++) cout<<lbl1[i]<<' ';
    cout<<'\n';
}

#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>

//using namespace __gnu_pbds;
using namespace std;

#define pb push_back
#define mp make_pair
#define mt make_tuple
#define ff first
#define ss second
#define NULL 0
#define pi 3.14159265358979323846
#define all(x) x.begin(), x.end()
#define d0(x) cout<<x<<" ";
#define d1(x) cout<<x<<"\n";
#define d2(x,y) cout<<x<<" "<<y<<"\n";
#define d3(x,y,z) cout<<x<<" "<<y<<" "<<z<<"\n";
#define d4(x,y,z,w) cout<<x<<" "<<y<<" "<<z<<" "<<w<<"\n";

//#define ordered_set tree<pair<int,int>, null_type,less<pair<int,int>>, rb_tree_tag,tree_order_statistics_node_update>
//#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>

template<typename T, typename U> static inline void amin(T &x, U y){if (y < x)x = y;}
template<typename T, typename U> static inline void amax(T &x, U y){if (y > x)x = y;}

typedef long long int ll;
typedef unsigned long long int ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

const  ll mod= 1000000007  ;
const long long inf = (ll)1e18+17;
const int inff = 1500000000;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};

vector<int> g[100010];
int n;
ll ans=0,ass[100010],val[100010];

void dfs(int nn,int pp){
    int cnt=0;
    vector<ll>op;
    for(int e:g[nn]){
        if(e==pp)continue;
        dfs(e,nn);
        if(val[e]==1)op.pb(e);
    }
    if(op.size()==0){
        val[nn]=1;return;
    }
    if(op.size()%2==1){
        ans += (op.size()-1)*2 + 2;
        for(int i=1;i<op.size();i++)ass[op[i]]=op[op.size()-i];
        ass[nn]=op[0];
        ass[op[0]]=nn;
    }

    else{
        for(int i=0;i<op.size();i++)ass[op[i]]=op[op.size()-i-1];
        if(op.size()!=0){
        ass[nn]=ass[op[0]];
        ass[op[0]]=nn;}
        ans += op.size()*2;
    }

}

void solve(){
    cin>>n;
    for(int i=0;i<n-1;i++)
    {
        int a,b;
        cin>>a>>b;
        g[a].pb(b);g[b].pb(a);
    }
    for(int i=1;i<=n;i++)if(g[i].size()==1 && i!=1)val[i]=1; else val[i]=0;
    dfs(1,00);
    if(val[1]==1){
        ans+=2;
        ass[1]=ass[g[1][0]];
        ass[g[1][0]]=1;
    }
    cout<<ans<<endl;
    for(int i=1;i<=n;i++)d0(ass[i]);

}

int main()
{
    clock_t tt;tt = clock();
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int t=1;
    //cin>>t;
    //pre();
    for(int i=1;i<=t;i++){
       //cout<<"Case #"<<i<<": ";
       solve();
    }
    tt = clock() - tt; double time_taken = ((double)tt)/CLOCKS_PER_SEC;
    //printf("code took %f seconds to execute \n", time_taken);
    return 0;
}

// ============== Notes =============== //
// common bugs
// - remove ios_base for interactive problems
// - int overflow (especially intermediate expressionns)
// - array bounds (indices bigger than MAXN?)
// - special cases (n=1? graph not connected?)
// - re-initialize efficiently between test cases

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll,ll> pi;
typedef vector<pi> vpi;
typedef long double ld;
#define pb emplace_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define ALL(x) x.begin(), x.end()
#define SZ(x) (ll)x.size()
#define f first
#define s second
const ll MOD = 1e9+7;
const ll INF = 1e9;
const ll MAXN = 100010;

int N,a,b;
int A[MAXN];
queue<int> Q;
set<int> S[MAXN];
int fl=-1;
int O[MAXN];

int main(){
    cin>>N;
    for(int i=1;i<N;++i){
        cin>>a>>b;
        O[a]=b;
        O[b]=a;
        S[a].insert(b);S[b].insert(a);
    }
    for(int i=1;i<=N;++i){
        A[i]=i;
        if(SZ(S[i])==1)Q.push(i);
    }
    int ans=0;
    while(SZ(Q)){
        int x=Q.front();Q.pop();
        if(SZ(S[x])==0&&A[x]==x){
            swap(A[x],A[O[x]]);
            ans+=2;
            break;
        }
        int t=*S[x].begin();
        if(A[x]==x){
            ans+=2;
            swap(A[x],A[t]);
        }
        S[t].erase(x);
        S[x].erase(t);
        if(SZ(S[t])==1)Q.push(t);
    }
    cout<<ans<<'\n';
    for(int i=1;i<=N;++i){
        assert(A[i]!=i);
        cout<<A[i]<<' ';
    }
    cout<<'\n';
}

#include <bits/stdc++.h>
using namespace std;
typedef int64_t llo;
#define mp make_pair
#define pb push_back
#define a first
#define b ssecond
llo n;
vector<llo> adj[100001];

int par2[100001];
vector<int> ord;
void dfs(llo no,llo par=-1){
	ord.pb(no);
	par2[no]=par;
	for(auto j:adj[no]){
		if(j==par){
			continue;
		}
		dfs(j,no);
	}
}
int ans[100001];
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin>>n;
	for(llo i=0;i<n-1;i++){
		llo aa,bb;
		cin>>aa>>bb;
		aa--;
		bb--;
		adj[aa].pb(bb);
		adj[bb].pb(aa);
	}
	dfs(0);
	for(int i=0;i<n;i++){
		ans[i]=i;

	}
	reverse(ord.begin(),ord.end());
	int ans2=0;
	for(auto j:ord){
		if(ans[j]!=j){
			continue;
		}
		ans2+=2;
		if(par2[j]==-1){
			swap(ans[j],ans[adj[j][0]]);
		}
		else{
			swap(ans[par2[j]],ans[j]);
		}
	}
	cout<<ans2<<endl;
	for(int i=0;i<n;i++){
		cout<<ans[i]+1<<" ";
	}
	cout<<endl;

	return 0;
}

/*
K.D. Vinit
*/
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>

using namespace std;
#define int long long

int do_tree(vector< vector<int> >& adj,int par,int chi,int new_house[],int not_assign[]) // '&' is veru Important here;
{
    int ans=0;
    vector<int> chi_unass;
    for(int i=0;i<adj[chi].size();i++)
    {
        if(adj[chi][i]==par) continue;
        ans+=do_tree(adj,chi,adj[chi][i],new_house,not_assign);
        if(not_assign[adj[chi][i]]==1) chi_unass.push_back(adj[chi][i]);
    }
    int n=chi_unass.size();
    if(chi_unass.size()==0) { not_assign[chi]=1; return ans; }

    new_house[chi_unass[0]]=chi;
    new_house[chi]=chi_unass[n-1];

    for(int i=1;i<n;i++)
        new_house[chi_unass[i]]=chi_unass[i-1];

    ans+=2*n;
    return ans;
}

void solve()
{
    int n,x,y;
    cin>>n;

    int new_house[n+1]={0},not_assign[n+1]={0};
    vector< vector<int> > adj(n+1);
    for(int i=1;i<n;i++)
    {
        cin>>x>>y;
        adj[x].push_back(y);
        adj[y].push_back(x);
    }

    int ans=do_tree(adj,0,1,new_house,not_assign);

    if(new_house[1]==0)
    {
        ans+=2;
        int temp=1;
        while(new_house[temp]!=adj[1][0]) temp++;
        new_house[temp]=1;
        new_house[1]=adj[1][0];

    }

    cout<<ans<<endl;
    for(int i=1;i<=n;i++) cout<<new_house[i]<<" ";
    cout<<endl;
}

int32_t main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    solve();
    return 0;
}

/*input
5
1 2
2 3
3 4
4 5
*/

#include <iostream>
#include <unordered_set>
#include <vector>
#include <algorithm>
#include <cassert>

using namespace std;

const int MAXN = 100007;
int N, deg[MAXN], match[MAXN];
vector<int> comp[MAXN], G[MAXN];
unordered_set<int> temp[MAXN];

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0);
	cin >> N;
	for (int i = 0; i < N - 1; ++i) {
		int u, v;
		cin >> u >> v;
		G[u].push_back(v);
		G[v].push_back(u);
		temp[u].insert(v);
		temp[v].insert(u);
	}

	vector<int> st;
	for (int u = 1; u <= N; ++u) if ((int) temp[u].size() == 1) st.push_back(u);
	while (!st.empty()) {
		int u = st.back();
		st.pop_back();
		if (!comp[u].empty()) continue;

		if (temp[u].empty()) {
			comp[G[u][0]].push_back(u);
		} else {
			assert((int) temp[u].size() == 1);
			int v = *temp[u].begin();
			if (comp[v].empty()) {
				for (int w : G[v]) {
					temp[w].erase(v);
					if ((int) temp[w].size() == 1) st.push_back(w);
				}
			}
			comp[v].push_back(u);
		}
	}

	int64_t ans = 0;
	for (int u = 1; u <= N; ++u) if (!comp[u].empty()) {
		ans += (int) comp[u].size();
		comp[u].push_back(u);
		for (int i = 0; i < (int) comp[u].size(); ++i) {
			match[comp[u][i]] = comp[u][(i + 1) % (int) comp[u].size()];
		}
	}
	cout << ans * 2 << '\n';
	for (int u = 1; u <= N; ++u) {
		cout << match[u] << ' ';
	}
}

#include <bits/stdc++.h>
using namespace std;
template<class t> inline t read(t &x){
	char c=getchar();bool f=0;x=0;
	while(!isdigit(c)) f|=c=='-',c=getchar();
	while(isdigit(c)) x=(x<<1)+(x<<3)+(c^48),c=getchar();
	if(f) x=-x;return x;
}
template<class t,class ...A> inline void read(t &x,A &...a){
	read(x);read(a...);
}
template<class t> inline void write(t x){
	if(x<0) putchar('-'),write(-x);
	else{if(x>9) write(x/10);putchar('0'+x%10);}
}

const int N=1e5+5;
vector<int> g[N];
int n,ans1[N],ans2[N];
long long mi,ma;

namespace MIN{

	vector<int> G[N];
	bool v[N];
	int q[N],qn;

	void link(int x,int y){
		G[x].push_back(y);
		G[y].push_back(x);
	}

	void dfs(int x,int fa){
		for(int y:g[x]) if(y^fa) dfs(y,x);
		if(!v[x]){
			if(fa) link(x,fa),v[x]=v[fa]=1;
			else link(g[x][0],x);
			mi++;
		}
	}

	void dfs(int x){
		v[x]=1;q[qn++]=x;
		for(int y:G[x]) if(!v[y]) dfs(y);
	}

	signed main(){
		dfs(1,0);
		for(int i=1;i<=n;i++) v[i]=0;
		for(int i=1;i<=n;i++) if(!v[i]){
			qn=0;dfs(i);
			for(int i=0;i<qn;i++) ans1[q[i]]=q[(i+1)%qn];
		}
	}
}

namespace MAX{

	int sz[N],rt,q[N],qn;

	void getrt(int x,int fa){
		sz[x]=1;
		int ma=0;
		for(int y:g[x]) if(y^fa){
			getrt(y,x);
			sz[x]+=sz[y];
			ma=max(ma,sz[y]);
		}
		if(2*sz[x]>n&&2*ma<=n) rt=x;
	}

	void dfs(int x,int fa){
		sz[x]=1;q[qn++]=x;
		for(int y:g[x]) if(y^fa){
			dfs(y,x);
			sz[x]+=sz[y];
			ma+=sz[y];
		}
	}

	signed main(){
		getrt(1,0);
		dfs(rt,0);
		for(int i=0;i<n;i++) ans2[q[i]]=q[(i+n/2)%n];
	}
}

signed main(){
	read(n);
	for(int i=1,x,y;i<n;i++){
		read(x,y);
		g[x].push_back(y);
		g[y].push_back(x);
	}
	MIN::main();
	write(mi<<1);puts("");
	for(int i=1;i<=n;i++) write(ans1[i]),putchar(' ');
}

/*Code by : Harry_Singh*/
#include<bits/stdc++.h>
#define int long long
#define io ios_base::sync_with_stdio(false),cin.tie(nullptr);
#define ipair pair<int,int>
#define pb push_back
#define f(i,n) for(int i=0;i<n;i++)
#define F(i,n) for(int i=1;i<=n;i++)
#define mod 1000000007
using namespace std;
#define inf 100000000000000009
#define tr cout<<"\n"
// directions
int xx[5]={0,0,1,-1};
int yy[5]={1,-1,0,0};
int dp[1000005]={0};
int sum=0;
vector<int> G[100005];
void dfs(int s,int p)
{
   for(int child:G[s])
   {
   	if(p!=child)
   	{
   		dfs(child,s);
   	}
   }

if(dp[s]==s)swap(dp[p],dp[s]),sum+=2;

if(dp[s]==s)
{
	// some of its immidiate children
	for(int child:G[s])
   {
   	    if(child!=p){swap(dp[s],dp[child]);break;}
   }

}

}

void we_have_choices()
{

int n;
cin>>n;
F(i,n)dp[i]=i;
F(i,n-1)
{
	int u,v;
	cin>>u>>v;
	G[u].pb(v);
	G[v].pb(u);
}
dfs(1,1);
cout<<sum;tr;
F(i,n)cout<<dp[i]<<" ";
tr;

}

int32_t main()
{
#ifndef ONLINE_JUDGE
 freopen("input.txt","r",stdin);
 freopen("op.txt","w",stdout);
#endif
    io;
    int t=1;while (t--)we_have_choices();
    return 0;
}

#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <sstream>
#include <set>
// #pragma GCC optimize(2)

//#define int long long
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define rush() int T;scanf("%d",&T);for(int Ti=1;Ti<=T;++Ti)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mm(i,v) memset(i,v,sizeof i);
#define mp(a, b) make_pair(a, b)
#define pi acos(-1)
#define fi first
#define se second
//你冷静一点，确认思路再敲！！！

using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int, int > PII;
priority_queue< PII, vector<PII>, greater<PII> > que;
stringstream ssin; //  ssin << string   while ( ssin >> int)
const ll LINF = 0x7fffffffffffffffll;

const int N = 4e5 + 5, M = 4e5 + 5, mod = 1e9 + 7, INF = 0x3f3f3f3f;
int n, idx, sum;
int e[M], ne[M], h[N], ans[N];

inline ll read() {
    char c=getchar();ll x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
    return x*f;
}

void add(int a, int b) {
    e[idx] = b;
    ne[idx] = h[a];
    h[a] = idx++;
}

void dfs(int u, int fa) {
    for (int i = h[u]; ~i; i = ne[i]) {
        int v = e[i];
        if (v == fa) continue;
        dfs(v, u);
    }
    if (fa != -1) {
        if (ans[u] == u) {
            swap(ans[u], ans[fa]);
            sum += 2;
        }
    }
}

int main()
{
    // freopen("in.txt","r",stdin);
    // freopen("out.txt","w",stdout);
    n = read();
    mm(h, -1);
    for (int i = 1; i < n; ++i) {
        ans[i] = i;
        int a, b;
        a = read();
        b = read();
        add(a, b);
        add(b, a);
    }
    ans[n] = n;

    dfs(1, -1);

    if (ans[1] == 1) {
        sum += 2;
        for (int i = h[1]; ~i; i = ne[i]) {
            int v = e[i];
            swap(ans[1], ans[v]);
            break;
        }
    }

    cout << sum << '\n';
    for (int i = 1; i <= n; ++i)
        cout << ans[i] << " ";
    puts("");

    #ifndef ONLINE_JUDGE
        system("pause");
    #endif
}