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

#define int long long
typedef pair<int, int> P;
#define mod 998244353
#define INF (1LL<<60)
 
#define rep(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)

#define YES puts("YES")
#define Yes puts("Yes")
#define NO  puts("NO")
#define No  puts("No")
#define ALL(v) (v).begin(), (v).end()
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
 
const int dx[4] = { 1, 0, -1, 0 };
const int dy[4] = { 0, 1, 0, -1 };
 
 
#if 1
//-------------
// DUMPマクロ
// https://www.creativ.xyz/dump-cpp-652/

#define repi(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++)

// vector
template <typename T>
istream& operator>>(istream& is, vector<T>& vec) {
    for (T& x : vec) is >> x;
    return is;
}
// pair
template <typename T, typename U>
ostream& operator<<(ostream& os, pair<T, U>& pair_var) {
    os << "(" << pair_var.first << ", " << pair_var.second << ")";
    return os;
}
// vector
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
// map
template <typename T, typename U>
ostream& operator<<(ostream& os, map<T, U>& map_var) {
    os << "{";
    repi(itr, map_var) {
        os << *itr;
        itr++;
        if (itr != map_var.end()) os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
// set
template <typename T>
ostream& operator<<(ostream& os, set<T>& set_var) {
    os << "{";
    repi(itr, set_var) {
        os << *itr;
        itr++;
        if (itr != set_var.end()) os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
 
#define DUMPOUT cerr
 
void dump_func() {
    DUMPOUT << endl;
}
template <class Head, class... Tail>
void dump_func(Head&& head, Tail&& ... tail) {
    DUMPOUT << head;
    if (sizeof...(Tail) > 0) {
        DUMPOUT << ", ";
    }
    dump_func(std::move(tail)...);
}
#ifdef DEBUG
#define DEB
#define dump(...)                                                              \
    DUMPOUT << "  " << string(#__VA_ARGS__) << ": "                            \
            << "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]"        \
            << endl                                                            \
            << "    ",                                                         \
        dump_func(__VA_ARGS__)
#else
#define DEB if (false)
#define dump(...)
#endif


template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
  return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

#endif


using Graph = vector<vector<int>>;

// 二部グラフ判定
vector<int> color;
bool dfs(const Graph &G, int v, int cur = 0) {
    color[v] = cur;
    for (auto next_v : G[v]) {
        // 隣接頂点がすでに色確定していた場合
        if (color[next_v] != -1) {
            if (color[next_v] == cur) return false; // 同じ色が隣接したらダメ
            continue;
        }

        // 隣接頂点の色を変えて、再帰的に探索 (一回でも false が返ってきたら false)
        if (!dfs(G, next_v , 1 - cur)) return false;
    }
    return true;
}


void solve(){
    int N;
    cin >> N;
    Graph G(N);
    rep(i,N-1){
        int a, b;
        cin >> a >> b;
        a--; b--;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    // 探索
    color.assign(N, -1);
    for (int v = 0; v < N; ++v) {
        if (color[v] != -1) continue; // v が探索済みだったらスルー
        dfs(G, v);
    }
    
    int A = 0, B;
    rep(i,N) if(color[i]==1) A++;
    B = N-A;
    
    if(A>B){
        rep(i,N) color[i]=1-color[i];
        swap(A,B);
    }
    
    // A(+1) <= B(0) になっている

    vector<int> ans(N);
    if(A>N/3 && B>N/3){
        // A = 1, B=2
        deque<int> d;
        for(int i=1; i<=N; i++){
            if(i%3==0) d.push_back(i);
        }
        for(int i=1; i<=N; i++){
            if(i%3==2) d.push_back(i);
            if(i%3==1) d.push_front(i);
        }
        rep(i,N){
            if(color[i]==1){ ans[i]=d.front(); d.pop_front();}
            else{ ans[i] = d.back(); d.pop_back();}
        }
    }else if(A<=N/3){
        // A=0
        queue<int> q;
        for(int i=1; i<=N; i++){
            if(i%3==0) q.push(i);
        }
        for(int i=1; i<=N; i++){
            if(i%3!=0) q.push(i);
        }
        rep(i,N){
            if(color[i]==1){ ans[i] = q.front(); q.pop(); }
        }
        rep(i,N){
            if(color[i]==0){ ans[i] = q.front(); q.pop(); }
        }

    }else{
        // B=0
        queue<int> q;
        for(int i=1; i<=N; i++){
            if(i%3==0) q.push(i);
        }
        for(int i=1; i<=N; i++){
            if(i%3!=0) q.push(i);
        }
        rep(i,N){
            if(color[i]==0){ ans[i] = q.front(); q.pop(); }
        }
        rep(i,N){
            if(color[i]==1){ ans[i] = q.front(); q.pop(); }
        }
    }
    rep(i,N){
        cout << ans[i] << " ";
    }
    cout << endl;
}

signed main(){
    cout << fixed << setprecision(18);
    cerr << fixed << setprecision(18);
    solve();
}


Measured memory_bytes: 19456