#include <algorithm>
#include <iostream>
#include <vector>
#include <math.h>
#include <set>
#include <map>
#include <string>
#include <stack>
#include <queue>
#include <iomanip>
#include <numeric>
#include <tuple>
#include <bitset>
#include <complex>
#include <unistd.h>
#include <cassert>
#include <cctype>
#include <random>
#include <time.h>
#define _USE_MATH_DEFINES
#define _GLIBCXX_DEBUG
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> plglg;
typedef pair<double, ll> pdlg;
typedef tuple<int, int, int> tiii;
typedef tuple<ll, ll, ll> tlglglg;
typedef tuple<double, double, double> tddd;
typedef complex<double> xy_t;
typedef vector<ll> vll;
typedef vector< vector<ll> > matrix;
#define REP(i, x, y) for(ll i = (ll)x; i < (ll)y; i++)
#define DREP(i, x, y, d) for(ll i = (ll)x; i < (ll)y; i += (ll)d)
#define PER(i, x, y) for(ll i = (ll)x; i > (ll)y; i--)
#define DPER(i, x, y, d) for(ll i = (ll)x; i > (ll)y; i -= (ll)d)
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
double pi = 3.141592653589793;
ll mod = 1000000007;
int intmax = 2147483647;
int intmin = -2147483648;
ll llmax = 9223372036854775807;
ll llmin = -9223372036854775807;
int iinf = intmax / 8;
ll inf = llmax / 8;
double eps = 1e-12;

struct edge {
    ll to, cost;
};

int V;
vector<edge> G[1000000];
ll d[1000000];

void addedge(int st, int ed, ll co) {
    edge e;
    e.to = ed;
    e.cost = co;
    G[st].push_back(e);
}

//根付き木を作る。parentVectorに親nodeの番号が入る。
//parentVector[root] = -1
//treeRankVectorにはnodeのrankが入る。rootのrankは0
vector<ll> par;
vector<ll> treeRank;
void makeRootedTree(vector<ll> &parentVector, vector<ll> &treeRankVector, ll vecSize, ll root) {
    parentVector = vector<ll>(vecSize);
    treeRankVector = vector<ll>(vecSize);
    assert(root < (ll)parentVector.size());
    parentVector[root] = -1;
    treeRankVector[root] = 0;
    queue<ll> que;
    que.push(root);
    while (!que.empty()) {
        ll node = que.front();
        que.pop();
        REP (i, 0, G[node].size()) {
            edge e = G[node][i];
            if (e.to != parentVector[node]) {
                parentVector[e.to] = node;
                treeRankVector[e.to] = treeRankVector[node] + 1;
                que.push(e.to);
            }
        }
    }
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll N;
    cin >> N;
    REP(i, 0, N - 1) {
        ll a, b;
        cin >> a >> b;
        a--;
        b--;
        addedge(a, b, 0);
        addedge(b, a, 0);
    }
    ll ans[N];
    fill(ans, ans + N, -1);
    makeRootedTree(par, treeRank, N, 0);
    ll wt = 0;
    ll bk = 0;
    REP(i, 0, N) {
        if (treeRank[i] % 2 == 0) {
            wt++;
        } else {
            bk++;
        }
    }
    ll one = N / 3;
    if (N % 3 > 0) {
        one++;
    }
    ll two = N / 3;
    if (N % 3 > 1) {
        two++;
    }
    ll zero = N / 3;
    ll used[N];
    fill(used, used + N, false);
    if (wt <= zero) {
        ll tmp = 1;
        REP(i, 0, N) {
            if (treeRank[i] % 2 == 0) {
                ans[i] = tmp * 3;
                used[tmp * 3 - 1] = true;
                tmp++;
            }
        }
        ll num = 0;
        REP(i, 0, N) {
            if (ans[i] == -1) {
                while(used[num]) {
                    num++;
                }
                ans[i] = num + 1;
                used[num] = true;
            }
        }
    } else if (bk <= zero) {
        ll tmp = 1;
        REP(i, 0, N) {
            if (treeRank[i] % 2 == 1) {
                ans[i] = tmp * 3;
                used[tmp * 3 - 1] = true;
                tmp++;
            }
        }
        ll num = 0;
        REP(i, 0, N) {
            if (ans[i] == -1) {
                while(used[num]) {
                    num++;
                }
                ans[i] = num + 1;
                used[num] = true;
            }
        }
    } else {
        ll tmpone = 0;
        ll tmptwo = 0;
        REP(i, 0, N) {
            if (treeRank[i] % 2 == 0 && tmpone < one) {
                ans[i] = tmpone * 3 + 1;
                used[tmpone * 3] = true;
                tmpone++;
            }
            if (treeRank[i] % 2 == 1 && tmptwo < two) {
                ans[i] = tmptwo * 3 + 2;
                used[tmptwo * 3 + 1] = true;
                tmptwo++;
            }
        }
        ll num = 0;
        REP(i, 0, N) {
            if (ans[i] == -1) {
                while(used[num]) {
                    num++;
                }
                ans[i] = num + 1;
                used[num] = true;
            }
        }
    }
    REP(i, 0, N) {
        cout << ans[i] << " ";
    }
    cout << endl;
}


Measured memory_bytes: 43364