//#include <tourist>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> p;
const int INF = 1e9;
const ll LINF = ll(1e18) + 1;
const int MOD = 1000000007;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
const int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1}, Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
#define yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define no cout << "No" << endl
#define NO cout << "NO" << endl
#define rep(i, n) for (int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()
#define debug(v)          \
    cout << #v << ":";    \
    for (auto x : v)      \
    {                     \
        cout << x << ' '; \
    }                     \
    cout << endl;
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;
}
//cout<<fixed<<setprecision(15);有効数字15桁
//-std=c++14
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
int n;
vector<vector<int>> v;
vector<int> depth;
void dfs(int k,int prev,int dep){
    depth[k]=dep;
    rep(i,v[k].size()){
        if(v[k][i]==prev)continue;
        dfs(v[k][i],k,dep+1);
    }
}
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> n;
    v.resize(n);
    depth.resize(n);
    vector<vector<int>>three(3);
    for(int i=1;i<=n;i++){
        if(i%3==0){
            three[0].push_back(i);
        }
        else if(i%3==1){
            three[1].push_back(i);
        }
        else{
            three[2].push_back(i);
        }
    }
    for (int i = 0; i < n-1; i++)
    {
        int temp,temp1;
        cin >> temp>>temp1;
        temp--;
        temp1--;
        v[temp1].push_back(temp);
        v[temp].push_back(temp1);
    }
    dfs(0,-1,0);
    int zero=0;
    int one=0;
    int two=0;
    vector<int> ans(n);
    /*
    debug(three[0]);
    debug(three[1]);
    debug(three[2]);
    debug(depth);
    */
    int count0=0;
    int count1=0;
    rep(i,n){
        if(depth[i]%2==1){
            count1++;
        }
        else{
            count0++;
        }
    }
    if(count0<=n/3){
        rep(i,n){
            if(depth[i]%2==0){
                ans[i]=three[0][zero];
                zero++;
            }
        }
        rep(i,n){
            if(depth[i]%2==1){
                if(zero<three[0].size()){
                    ans[i]=three[0][zero];
                    zero++;
                }
                else if(one<three[1].size()){
                    ans[i]=three[1][one];
                    one++;
                }
                else{
                    ans[i]=three[2][two];
                    two++;
                }
            }
        }
    }
    else if(count1<=n/3){
        rep(i,n){
            if(depth[i]%2==1){
                ans[i]=three[0][zero];
                zero++;
            }
        }
        rep(i,n){
            if(depth[i]%2==0){
                if(zero<three[0].size()){
                    ans[i]=three[0][zero];
                    zero++;
                }
                else if(one<three[1].size()){
                    ans[i]=three[1][one];
                    one++;
                }
                else{
                    ans[i]=three[2][two];
                    two++;
                }
            }
        }
    }
    else{
    rep(i,n){
        if(depth[i]%2==0&&one<three[1].size()){
            ans[i]=three[1][one];
            one++;
        }
        else if(depth[i]%2==1&&two<three[2].size()){
            ans[i]=three[2][two];
            two++;
        }
        else{
            ans[i]=three[0][zero];
            zero++;
        }
    }
    }
    rep(i,n)
    cout << ans[i] << "\n";
}


Measured memory_bytes: 18552