//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i<n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
template<class T> using PQ = priority_queue<T>;
template<class T> using PQG = priority_queue<T, vector<T>, greater<T> >;
const int INF = 100010001;
const ll LINF = (ll)INF*INF*10;
template<typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) {return a < b && (a = b, true);}
template<typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) {return a > b && (a = b, true);}
template<typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) { return is >> p.first >> p.second;}
template<typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) { return os << p.first << ' ' << p.second;}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    vvi G(n);
    rep(i, n-1) {
        int a, b;
        cin >> a >> b;
        a--; b--;
        G[a].push_back(b);
        G[b].push_back(a);
    }

    vector<int> num(n);
    queue<int> one, two, zero;
    rep(i, n) {
        if(i%3 == 0) one.push(i+1);
        else if(i%3 == 1) two.push(i+1);
        else zero.push(i+1);
    }
    num[0] = one.front();
    one.pop();
    bool restart = false;
    vvi lst(2);
    lst[0].push_back(0);
    function<void(int, int, bool)> dfs = [&](int i, int j, bool odd) {
        for(int ne:G[i]) {
            if(ne == j) continue;
            if(odd) {
                lst[0].push_back(ne);
                if(!one.empty()) {
                    num[ne] = one.front();
                    one.pop();
                } else {
                    if(zero.empty()) {
                        restart = true;
                    }
                    else {
                        num[ne] = zero.front();
                        zero.pop();
                    }
                }
            }
            else {
                lst[1].push_back(ne);
                if(!two.empty()) {
                    num[ne] = two.front();
                    two.pop();
                } else {
                    if(zero.empty()) {
                        restart = true;
                    }
                    else {
                        num[ne] = zero.front();
                        zero.pop();
                    }
                }
            }
            dfs(ne, i, !odd);
        }
    };
    dfs(0, -1, false);
    if(restart) {
        int x = 0;
        if(lst[0].size() > lst[1].size()) x = 1;
        while(!one.empty()) one.pop();
        while(!two.empty()) two.pop();
        rep(i, n) {
            if(i%3 == 0) one.push(i+1);
            else if(i%3 == 1) two.push(i+1);
            else zero.push(i+1);
        }
        rep(i, lst[x].size()) {
            num[lst[x][i]] = zero.front();
            zero.pop();
        }
        rep(i, lst[x^1].size()) {
            if(!one.empty()) {
                num[lst[x^1][i]] = one.front();
                one.pop();
            } else if(!two.empty()) {
                num[lst[x^1][i]] = two.front();
                two.pop();
            } else {
                num[lst[x^1][i]] = zero.front();
                zero.pop();
            }
        }
    }
    rep(i, n) {
        cout << num[i] << (i == n-1?'\n':' ');
    }
}

Measured memory_bytes: 30332