/*
    Author:zeke
    
    pass System Test!
    GET AC!!
*/
#include <iostream>
#include <queue>
#include <vector>
#include <iostream>
#include <vector>
#include <string>
#include <cassert>
#include <algorithm>
#include <functional>
#include <cmath>
#include <queue>
#include <set>
#include <stack>
#include <deque>
#include <map>
#include <iomanip>
#include <utility>
#include <stack>
#include <bitset>
using ll = long long;
using ld = long double;
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define rep3(var, min, max) for (ll(var) = (min); (var) < (max); ++(var))
#define repi3(var, min, max) for (ll(var) = (max)-1; (var) + 1 > (min); --(var))
#define Mp(a, b) make_pair((a), (b))
#define F first
#define S second
#define Icin(s) \
    ll(s);      \
    cin >> (s);
#define Scin(s) \
    ll(s);      \
    cin >> (s);
template <class T>
bool chmax(T &a, const T &b)
{
    if (a < b)
    {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
bool chmin(T &a, const T &b)
{
    if (b < a)
    {
        a = b;
        return 1;
    }
    return 0;
}
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef vector<V> VV;
typedef vector<P> VP;
ll mod = 1e9 + 7;
ll MOD = 1e9 + 7;
ll INF = 1e18;
VV gragh;
V vec;
void dfs(ll now,ll prev,ll type){
    if(vec[now]!=-1){
        return;
    }
    vec[now]=type;
    for(auto i:gragh[now]){
        if(i==prev){
            continue;
        }
        dfs(i,now,type^1);
    }
}
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n;
    cin>>n;
    gragh.resize(n);
    rep(i,n-1){
        ll a,b;
        cin>>a>>b;
        a--;b--;
        gragh[a].push_back(b);
        gragh[b].push_back(a);
    }
    vec.resize(n,-1);
    dfs(0,-1,1);
    ll blue=0;
    ll red=0;
    V R;
    V B;
    rep(i,n){
        if(vec[i]){
            blue++;
            B.push_back(i);
        }else{
            red++;
            R.push_back(i);
        }
    }
   /* for(auto i:B)cout<<i<<" ";
    cout<<endl;
    for(auto i:R)cout<<i<<" ";
    cout<<endl;*/
    V have(n);
    queue<ll> q;
    queue<ll> q1;
    rep3(i,1,n+1){
        if(i%3!=0){
            q.push(i);
        }else{
            q1.push(i);
        }
    }
    if(red*3<=n){
        rep(i,red){
            have[R[i]]=3*(i+1);
        }
        ll temp=3*(red+1);
        rep(i,blue){
            if(temp<=n){
                have[B[i]]=temp;
                temp+=3;
                continue;
            }
            have[B[i]]=q.front();
            q.pop();
        }
        
    }else if(blue*3<=n){
        rep(i,blue){
            have[B[i]]=3*(i+1);
        }
        ll temp=3*(blue+1);
        rep(i,red){
            if(temp<=n){
                have[R[i]]=temp;
                temp+=3;
                continue;
            }
            have[R[i]]=q.front();
            q.pop();
        }
    }else{
        ll temp=1;
        rep(i,red){
            if(temp<=n){
                have[R[i]]=temp;
                temp+=3;
            }else{
                have[R[i]]=q1.front();
                q1.pop();
            }
        }
        temp=2;
        rep(i,blue){
            if(temp<=n){
                have[B[i]]=temp;
                temp+=3;
            }else{
                have[B[i]]=q1.front();
                q1.pop();
            }
        }
    }
    for(auto i:have){
        cout<<i<<" ";
    }
    cout<<endl;
    
}


Measured memory_bytes: 21612