#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//#include <boost/multiprecision/cpp_ll.hpp>
//typedef boost::multiprecision::cpp_ll ll;
typedef long double dd;
#define i_7 (ll)(1E9+7)
//#define i_7 998244353
#define i_5 i_7-2
ll mod(ll a){
    ll c=a%i_7;
    if(c>=0)return c;
    return c+i_7;
}
typedef pair<ll,ll> l_l;
ll inf=(ll)1E16;
#define rep(i,l,r) for(ll i=l;i<=r;i++)
#define pb push_back
ll max(ll a,ll b){if(a<b)return b;else return a;}
ll min(ll a,ll b){if(a>b)return b;else return a;}
void Max(ll &pos,ll val){pos=max(pos,val);}//Max(dp[n],dp[n-1]);
void Min(ll &pos,ll val){pos=min(pos,val);}
void Add(ll &pos,ll val){pos=mod(pos+val);}
dd EPS=1E-9;
string alp="abcdefghijklmnopqrstuvwxyz";
#define fastio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);

struct Node{
    ll Rank;
    ll par;
};
vector<ll> v[200001];
Node node[200001];
ll amari[2];
void hoge(ll i){
    rep(j,0,v[i].size()-1){
        if(node[i].par!=v[i][j]){
            node[v[i][j]].par=i;
            node[v[i][j]].Rank=node[i].Rank+1;
            hoge(v[i][j]);
        }
    }
}

int main(){
    
    ll n;cin>>n;
    ll a[n-1],b[n-1];
    rep(i,0,n-2){
        cin>>a[i]>>b[i];
        v[a[i]].pb(b[i]);
        v[b[i]].pb(a[i]);
    }
    ll c[3]={3,1,2};
    
    ll ans[n+1];
    node[1].Rank=1;
    node[1].par=1;
    hoge(1);
    
    rep(i,0,1)amari[i]=0;
    rep(i,1,n){
        if((node[i].Rank)%2==1){
            amari[1]++;
        }else if(node[i].Rank%2==0){
            amari[0]++;
        }
    }
    ll cc=0;
    bool bol=true;
    if(n%3==0){
        if(amari[0]>n*2/3 || amari[1]>n*2/3){
            bol=false;
        }
    }else if(n%3==1){
        if(max(amari[0],amari[1])>(n/3)*2+1){
            bol=false;
        }else if(amari[0]==(n/3)*2+1){
            cc++;
        }
    }else{
        if(max(amari[0],amari[1])>(n/3)*2+1){
            bol=false;
        }
    }
    if(bol==false){
            if(amari[1]>amari[0]){
                cc++;
            }
            rep(i,1,n){
                if((node[i].Rank+cc)%2==0){
                    if(c[1]<=n){
                        ans[i]=c[1];
                        c[1]+=3;
                    }else if(c[2]<=n){
                        ans[i]=c[2];
                        c[2]+=3;
                    }else{
                        ans[i]=c[0];
                        c[0]+=3;
                    }
                }else{
                    if(c[0]<=n){
                        ans[i]=c[0];
                        c[0]+=3;
                    }else if(c[1]<=n){
                        ans[i]=c[1];
                        c[1]+=3;
                    }else{
                        ans[i]=c[2];
                        c[2]+=3;
                    }
                }
            }
        rep(i,1,n){
            cout<<ans[i];
            if(i<n){
                cout<<' ';
            }else{
                cout<<endl;
            }
        }
        return 0;
    }
    
    
    rep(i,1,n){
        if((node[i].Rank+cc)%2==1){
            if(c[1]<=n){
                ans[i]=c[1];
                c[1]+=3;
            }else{
                ans[i]=c[0];
                c[0]+=3;
            }
        }else if((node[i].Rank+cc)%2==0){
            if(c[2]<=n){
                ans[i]=c[2];
                c[2]+=3;
            }else{
                ans[i]=c[0];
                c[0]+=3;
            }
        }
    }
    rep(i,1,n){
        cout<<ans[i];
        if(i<n){
            cout<<' ';
        }else{
            cout<<endl;
        }
    }
    
    return 0;
}




Measured memory_bytes: 24576