#include<bits/stdc++.h>
using namespace std;
//#include<boost/multiprecision/cpp_int.hpp>
//using boost::multiprecision::cpp_int;

#define int long long
#define cint cpp_int
#define endl "\n"
#define REP(i,a,n) for(int i=a;i<n;++i)
#define REPR(i,a,n) for(int i=a;i>n;--i)
#define RUP(a,b) (((a)+(b)-1)/(b))
#define ALL(v) (v).begin(),(v).end()
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define MOD 1000000007
#define INF LLONG_MAX/2
#define PI acos(-1.0)
#define SUM(v) accumulate((v).begin(),(v).end(), 0ll)

typedef long long ll;
typedef pair<int,int> Pii;
typedef tuple<int,int,int> Tiii;
typedef vector<int> Vi;
typedef vector<Vi> VVi;
typedef vector<Pii> VPii;
typedef vector<string> Vs;
typedef priority_queue<int> PQi;

template<class T> inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T> inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
void vout(Vi &v){for(int i=0;i<(v).size();i++) cout<<v[i]<<" ";cout<<endl;}
void vout(Vs &v){for(int i=0;i<(v).size();i++) cout<<v[i]<<" ";cout<<endl;}
void vvout(VVi &v){for(int i=0;i<(v).size();i++) vout(v[i]);}
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return a/gcd(a,b)*b;}
int mypow(int x, int n, int m){if(n==0)return 1;if(n%2==0)return mypow(x*x%m,n/2,m);else return x*mypow(x,n-1,m)%m;}
int digit(int k,int i){string s = to_string(k);return s[s.size()-i]-'0';}
void uniq(Vi &v){sort(v.begin(),v.end()); v.erase(unique(v.begin(),v.end()),v.end());} //vectorの同じ要素消す
int ctoi(char c){if(c>='0'&&c<='9'){return c-'0';}return 0;}
int leng(int n){string s=to_string(n); return s.size();} //intの桁数
int digitsum(int n){int ret=0; while(n>0){ret+=n%10;n/=10;}return ret;} //桁和
int lesscount(int x,Vi &a){return lower_bound(a.begin(),a.end(),x)-a.begin();} //vector aにおけるx未満の数の個数
int orlesscount(int x,Vi &a){return upper_bound(a.begin(),a.end(),x)-a.begin();} //vector aにおけるx以下の数の個数
int morecount(int x,Vi &a){return a.size()-orlesscount(x,a);} //vector aにおけるxより大きい数の個数
int ormorecount(int x,Vi &a){return a.size()-lesscount(x,a);} //vector aにおけるx以上の数の個数
int count(int x,Vi &a) {return upper_bound(ALL(a),x)-lower_bound(ALL(a),x);} //vector aにおけるxの個数
Vi accum(Vi &v){Vi ret((v).size()+1);REP(i,0,(v).size()) ret[i+1]=ret[i]+v[i];return ret;}
bool comp(Pii a,Pii b){
    if(a.second != b.second) return a.second<b.second;
    else return a.first<b.first;
}

struct edge{int to,cost;};
struct graph{
    int N;  
    vector< vector<edge> > G;
    Vi dist;
    graph(int n){
        init(n);
    }
    void init(int n){
        N = n;
        G.resize(N);
        dist.resize(N);
        REP(i,0,N){
            dist[i] = INF;
        }
    }
    void add_edge(int s,int t,int cost=1){
        edge e;
        e.to = t, e.cost = cost;
        G[s].push_back(e);
    }
    void bfs(int s=0){
        REP(i,0,N){
            dist[i] = INF;
        }
        dist[s]=0;
        queue<int> q;
        q.push(s);
        while(!q.empty()){
            int now=q.front();q.pop();
            //終了条件などの処理
            for(auto e : G[now]){
                if(dist[e.to]==INF){
                    dist[e.to] = dist[now]+e.cost;
                    q.push(e.to);
                }
            }
        }
    }
};

signed main(){cin.tie(0);cout.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);
    int n;
    cin>>n;
    Vi a(n-1),b(n-1);
    REP(i,0,n-1){
        cin>>a[i]>>b[i];
        a[i]--; b[i]--;
    }

    graph G(n);

    REP(i,0,n-1){
        G.add_edge(a[i],b[i]);
        G.add_edge(b[i],a[i]);
    }

    G.bfs();

    int dist_odd=0,dist_even=0;
    REP(i,0,n){
        if(G.dist[i]%2==1) dist_odd++;
        else dist_even++;
    }


    Vi ans(n,-1);
    if(dist_odd <= n/3){
        queue<int> oddsq,otherq;
        REP(i,1,n+1){
            if(i%3==0 && i<=dist_odd*3) oddsq.push(i);
            else otherq.push(i);
        }
        REP(i,0,n){
            if(G.dist[i]%2==1){
                ans[i]=oddsq.front();
                oddsq.pop();
            }else{
                ans[i]=otherq.front();
                otherq.pop();
            }
        }
    }else if(dist_even <= n/3){
        queue<int> evenq,otherq;
        REP(i,1,n+1){
            if(i%3==0 && i<=dist_even*3) evenq.push(i);
            else otherq.push(i);
        }
        REP(i,0,n){
            if(G.dist[i]%2==0){
                ans[i]=evenq.front();
                evenq.pop();
            }else{
                ans[i]=otherq.front();
                otherq.pop();
            }
        }
    }else{
        int count1=0,count2=0,count3=0;
        REP(i,0,n){
            if(G.dist[i]%2==1){
                if(count1*3+1 <= n){
                    ans[i]=count1*3+1;
                    count1++;
                }else{
                    ans[i]=count3*3+3;
                    count3++;
                }
            }
            if(G.dist[i]%2==0){
                if(count2*3+2 <= n){
                    ans[i]=count2*3+2;
                    count2++;
                }else{
                    ans[i]=count3*3+3;
                    count3++;
                }
            }
        }
    }
    vout(ans);
    return 0;
}


Measured memory_bytes: 23908