#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <cassert>

using namespace std;
typedef long long LL;
template <typename F, typename S> std::ostream& operator<<(ostream& os, const pair<F,S>& p) {
    os << "(" << p.first << "," << p.second << ")"; return os; }
template <typename T> ostream& operator<<(ostream& os, const vector<T>& v) {
    os << "["; for(const T& a: v){ os << a << ", "; } os << "]"; return os; }
template <typename K, typename V> ostream& operator<<(ostream& os, const map<K,V>& m) {
    os << "{"; for(const auto& p: m){ os << p.first <<":"<< p.second << ", "; } os << "}"; return os; }

void calcDepth(LL cur, LL parent, LL d, vector<vector<LL>>& edge, vector<LL>& depth){
    depth[cur] = d;
    for(LL next: edge[cur]){
        if(next == parent) continue;
        calcDepth(next, cur, d+1, edge, depth);
    }
}

void build1(LL n, vector<LL>& depth, vector<LL>& assign, LL r){
    vector<vector<LL>> nums(3);
    for(LL i = 1; i <= n; ++i){
        nums[i%3].push_back(i);
    }
    for(LL i = 1; i <= n; ++i){
        LL d = depth[i-1] % 2;
        if(d == r){
            assert(!nums[0].empty());
            assign[i-1] = nums[0].back();
            nums[0].pop_back();
        }else{
            if(!nums[2].empty()){
                assign[i-1] = nums[2].back();
                nums[2].pop_back();
            }else if(!nums[1].empty()){
                assign[i-1] = nums[1].back();
                nums[1].pop_back();
            }else{
                assign[i-1] = nums[0].back();
                nums[0].pop_back();
            }
        }
    }
    for(vector<LL>& nv: nums){
        assert(nv.empty());
    }
}

void build2(LL n, vector<LL>& depth, vector<LL>& assign, LL r){
    vector<vector<LL>> nums(3);
    for(LL i = 1; i <= n; ++i){
        nums[i%3].push_back(i);
    }
    for(LL i = 1; i <= n; ++i){
        LL d = depth[i-1] % 2;
        if(d == r){
            if(!nums[1].empty()){
                assign[i-1] = nums[1].back();
                nums[1].pop_back();
            }else{
                assign[i-1] = nums[0].back();
                nums[0].pop_back();
            }
        }else{
            if(!nums[2].empty()){
                assign[i-1] = nums[2].back();
                nums[2].pop_back();
            }else{
                assign[i-1] = nums[0].back();
                nums[0].pop_back();
            }
        }
    }
    for(vector<LL>& nv: nums){
        assert(nv.empty());
    }
}

int main(){
    LL n;
    cin >> n;
    vector<vector<LL>> edge(n);
    for(LL i = 0; i < n-1; ++i){
        LL a, b;
        cin >> a >> b;
        --a; --b;
        edge[a].push_back(b);
        edge[b].push_back(a);
    }
    vector<LL> depth(n);
    calcDepth(0, -1, 0, edge, depth);

    LL cnt0 = 0;
    LL cnt1 = 0;
    for(LL d: depth){
        if(d % 2 == 0){
            cnt0 += 1;
        }else{
            cnt1 += 1;
        }
    }

    vector<LL> assign(n);
    if(cnt0 <= n / 3){
        build1(n, depth, assign, 0);
    }else if(cnt1 <= n / 3){
        build1(n, depth, assign, 1);
    }else if(cnt0 >= cnt1){
        build2(n, depth, assign, 0);
    }else{
        build2(n, depth, assign, 1);
    }

    for(LL i = 0; i < n; ++i){
        if(i+1 < n){
            cout << assign[i] << " ";
        }else{
            cout << assign[i] << endl;
        }
    }
    return 0;
}



Measured memory_bytes: 24428