#include <bits/stdc++.h>
using namespace std;

// 型定義
typedef long long ll;
typedef pair<ll, ll> P;

// forループ
#define REP(i,n) for(ll i=0; i<(ll)(n); ++i)

// 定数宣言
const int INF = 1e9;
const int MOD = 1e9+7;
const ll LINF = 1e18;

// グラフ表現
using Graph = vector<vector<int>>;

// n次元配列の初期化。第２引数の型のサイズごとに初期化していく。
template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}

vector<bool> even;  // 各頂点の加算カウント
vector<bool> seen;  // 訪問済みの点

// BFSのための再帰関数
void bfs(const Graph &G, int v, bool color) {
    seen[v] = true;
    even[v] = color;
    for (auto next_v : G[v]) { 
        if (seen[next_v]) continue;
        bfs(G, next_v, !color); // 再帰的に値を追加していく
    }
}

int main()
{
    int N;
    cin >> N;

    // グラフ
    Graph G(N);

    seen.assign(N, false);
    even.assign(N, true);

    REP(i, N-1){
        int a, b;
        cin >> a >> b;
        G[a-1].push_back(b-1);
        G[b-1].push_back(a-1);
    }

    // 幅優先探索で色分け
    bfs(G, 0, true);

    int even_count = 0;
    int odd_count = 0;
    REP(i, N){
        if(even[i] == true) even_count++;
        if(even[i] == false) odd_count++;
    }

    queue<int> que3n;
    queue<int> que3n1;
    queue<int> que3n2;

    int n_count = 0;
    int n1_count = 0;
    int n2_count = 0;

    REP(i, N){
        if((i+1) % 3 == 0){
            que3n.push(i+1);
            n_count++;
        }
        if((i+1) % 3 == 1){
            que3n1.push(i+1);
            n1_count++;
        }
        if((i+1) % 3 == 2){
            que3n2.push(i+1);
            n2_count++;
        }
    }

    vector<int> ans(N);

    if((even_count >= n2_count) && (odd_count >= n2_count)){
        REP(i, N){
            if(even[i] == true){
                if(!que3n1.empty()){
                    ans[i] = que3n1.front();
                    que3n1.pop();
                }
                else{
                    ans[i] = que3n.front();
                    que3n.pop();
                }
            }
            else{
                if(!que3n2.empty()){
                    ans[i] = que3n2.front();
                    que3n2.pop();
                }
                else{
                    ans[i] = que3n.front();
                    que3n.pop();
                }
            }
        }
        REP(i, N){
            if(i == N-1) cout << ans[i] << endl;
            else{
                cout << ans[i] << " ";
            }
        }
        return 0;
    }
    if(even_count < n2_count){
        REP(i, N){
            if(even[i] == true){
                ans[i] = que3n.front();
                que3n.pop();
            }
            else{
                if(!que3n1.empty()){
                    ans[i] = que3n1.front();
                    que3n1.pop();
                    continue;
                }
                if(!que3n2.empty()){
                    ans[i] = que3n2.front();
                    que3n2.pop();
                    continue;
                }
                ans[i] = que3n.front();
                que3n.pop();
            }
        }
        REP(i, N){
            if(i == N-1) cout << ans[i] << endl;
            else{
                cout << ans[i] << " ";
            }
        }
        return 0;
    }
    if(odd_count < n2_count){
        REP(i, N){
            if(even[i] == false){
                ans[i] = que3n.front();
                que3n.pop();
            }
            else{
                if(!que3n1.empty()){
                    ans[i] = que3n1.front();
                    que3n1.pop();
                    continue;
                }
                if(!que3n2.empty()){
                    ans[i] = que3n2.front();
                    que3n2.pop();
                    continue;
                }
                ans[i] = que3n.front();
                que3n.pop();
            }
        }
        REP(i, N){
            if(i == N-1) cout << ans[i] << endl;
            else{
                cout << ans[i] << " ";
            }
        }
        return 0;
    }
    return 0;
}

Measured memory_bytes: 18048