#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(),(x).end()
const int mod=1000000007,MAX=200005,INF=1<<30;
vector<int> G[MAX];
int dis[MAX],dis2[MAX];

void DFS(int u,int p){
    for(int to:G[u]){
        if(to==p) continue;
        dis[to]=dis[u]+1;
        DFS(to,u);
    }
}

void kyo(int u,int p){
    for(int to:G[u]){
        if(to==p) continue;
        dis2[to]=dis2[u]+1;
        kyo(to,u);
    }
}

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int N;cin>>N;
    if(N==2){
        cout<<"1 2"<<endl;
        return 0;
    }
    for(int i=0;i<N-1;i++){
        int a,b;cin>>a>>b;
        a--;b--;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    DFS(0,-1);
    
    pair<int,int> ma={-1,-1};
    for(int i=0;i<N;i++){
        ma=max(ma,{dis[i],i});
    }
    
    kyo(ma.second,-1);
    
    ma={-1,-1};
    
    for(int i=0;i<N;i++){
        ma=max(ma,{dis2[i],i});
    }
    
    if(ma.first<=2){
        for(int i=0;i<N;i++){
            cout<<i+1<<" ";
        }
        cout<<endl;
        return 0;
    }
    
    vector<int> ans(N);
    
    int cnt0=0,cnt1=0;
    
    for(int i=0;i<N;i++){
        if(dis[i]%2==0) cnt0++;
        else cnt1++;
    }
    
    int now1=1,now2=2,now3=3;
    vector<int> used(N+1);
    
    if(N/3>=cnt0){
        for(int i=0;i<N;i++){
            if(dis[i]%2==0){
                ans[i]=now3;
                used[now3]=1;
                now3+=3;
            }
        }
        int now=1;
        for(int i=0;i<N;i++){
            if(ans[i]) continue;
            while(used[now]) now++;
            ans[i]=now;
            used[now]=1;
        }
        
        for(int i=0;i<N;i++){
            cout<<ans[i]<<" ";
        }
        cout<<endl;
    }else if(N/3>=cnt1){
        for(int i=0;i<N;i++){
            if(dis[i]%2==1){
                ans[i]=now3;
                used[now3]=1;
                now3+=3;
            }
        }
        int now=1;
        for(int i=0;i<N;i++){
            if(ans[i]) continue;
            while(used[now]) now++;
            ans[i]=now;
            used[now]=1;
        }
        
        for(int i=0;i<N;i++){
            cout<<ans[i]<<" ";
        }
        cout<<endl;
    }else if(cnt0>=(N+2)/3&&cnt1>=(N+1)/3){
        for(int i=0;i<N;i++){
            if(dis[i]%2==1){
                if(now2<=N){
                    ans[i]=now2;
                    now2+=3;
                }else{
                    ans[i]=now3;
                    now3+=3;
                }
            }else{
                if(now1<=N){
                    ans[i]=now1;
                    now1+=3;
                }else{
                    ans[i]=now3;
                    now3+=3;
                }
            }
        }
        
        for(int i=0;i<N;i++){
            cout<<ans[i]<<" ";
        }
        cout<<endl;
        
    }else if(cnt0>=(N+1)/3&&cnt1>=(N+2)/3){
        for(int i=0;i<N;i++){
            if(dis[i]%2==0){
                if(now2<=N){
                    ans[i]=now2;
                    now2+=3;
                }else{
                    ans[i]=now3;
                    now3+=3;
                }
            }else{
                if(now1<=N){
                    ans[i]=now1;
                    now1+=3;
                }else{
                    ans[i]=now3;
                    now3+=3;
                }
            }
        }
        
        for(int i=0;i<N;i++){
            cout<<ans[i]<<" ";
        }
        cout<<endl;
        
    }else{
        cout<<-1<<endl;
    }
}



Measured memory_bytes: 20224