#include <bits/stdc++.h>
using namespace std;
 
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
#define FOR(i, m, n) for (int i = (m); i < (n); i++)
#define FORR(i, m, n) for (int i = (m); i >= (n); i--)
#define REP(i, n) FOR(i, 0, (n))
#define REPR(i, n) FORR(i, (n) - 1, 0)
#define REP1(i, n) FOR(i, 1, (n) + 1)
#define REPS(c, s) for (char c : s)
#define ALL(c) (c).begin(), (c).end()
#define SORT(c) sort(ALL(c))
#define REV(c) reverse(ALL(c))
#define sz(v) (int)v.size()
#define endl '\n'
template<class T> inline bool chmin(T& a, T b) {if (a > b) {a = b; return true;} return false;}
template<class T> inline bool chmax(T& a, T b) {if (a < b) {a = b; return true;} return false;}
template<class T> inline void printv(vector<T>& v) {int n = sz(v); REP(i, n) cout << v[i] << (i == n - 1 ? endl : ' ');}
template<class T> inline void printlnv(vector<T>& v) {int n = sz(v); REP(i, n) cout << v[i] << endl;}
const int MOD = 1000000007;
const int INF = 1000000001;
const ll LINF = 1000000001000000001LL;
 
void solve();
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(numeric_limits<double>::max_digits10);
    solve();
    return 0;
}

void solve() {
    int n;
    cin >> n;
    vvi g(n, vi());
    int a, b;
    REP(i, n - 1) {
        cin >> a >> b;
        a--; b--;
        g[a].emplace_back(b);
        g[b].emplace_back(a);
    }

    vi r(2);
    vi d(n);
    set<pair<int, int>> dp;
    d[0] = 0;
    r[0]++;
    dp.emplace(0, 0);
    function<void(int, int)> dfs = [&](int c, int p) {
        if (c != 0) {
            d[c] = d[p] + 1;
            dp.emplace(d[c], c);
            r[d[c] % 2]++;
        }
        if (c != 0 && sz(g[c]) == 1) return;
        for (auto x : g[c]) {
            if (x == p) continue;
            dfs(x, c);
        }
    };

    dfs(0, -1);

    int m1 = 1;
    int m2 = 2;
    int m0 = 3;
    vi ans(n);
    int cur = 1;
    // cout << "q? " << qqq[0] << "/" << qqq[1] << endl;
    for (auto v : dp) {
        // cout << v.first << "/" << v.second << endl;
        if (v.first % 2 == 0) {
            if (r[0] <= n / 3) {
                ans[v.second] = m0;
                m0 += 3;
            } else {
                if (m1 <= n) {
                    ans[v.second] = m1;
                    m1 += 3;
                } else {
                    if (r[1] <= n / 3) {
                        if (m2 <= n) {
                            ans[v.second] = m2;
                            m2 += 3;
                        } else {
                            ans[v.second] = m0;
                            m0 += 3;
                        }
                    } else {
                        if (m0 <= n) {
                            ans[v.second] = m0;
                            m0 += 3;
                        } else {
                            ans[v.second] = m2;
                            m2 += 3;
                        }
                    }
                }
            }
        } else if (v.first % 2 == 1) {
            if (r[1] <= n / 3) {
                ans[v.second] = m0;
                m0 += 3;
            } else {
                if (m2 <= n) {
                    ans[v.second] = m2;
                    m2 += 3;
                } else {
                    if (r[0] <= n / 3) {
                        if (m1 <= n) {
                            ans[v.second] = m1;
                            m1 += 3;
                        } else {
                            ans[v.second] = m0;
                            m0 += 3;
                        }
                    } else {
                        if (m0 <= n) {
                            ans[v.second] = m0;
                            m0 += 3;
                        } else {
                            ans[v.second] = m1;
                            m1 += 3;
                        }
                    }
                }
            }
        }
    }

    printv(ans);
}


Measured memory_bytes: 27520