#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
int ans[N], col[N];
vector<int> adjlist[N];
vector<int> sets[2];
void dfs(int u, int p){
    sets[col[u]].push_back(u);
    for (auto v : adjlist[u]){
        if (v == p) continue;
        col[v] = 1-col[u];
        dfs(v,u);
    }
}
int main(){
    int n;
    scanf("%d",&n);
    for (int i = 1; i < n; i++){
        int a,b;
        scanf("%d%d",&a,&b);
        adjlist[a].push_back(b);
        adjlist[b].push_back(a);
    }
    col[1] = 1;
    dfs(1,-1);
    if (sets[0].size() > sets[1].size()) swap(sets[0],sets[1]);
    int X = n/3;
    int R = sets[0].size(), B = sets[1].size();
    int lim1 = (n+2)/3, lim2 = (n+1)/3;
    if (R <= X){
        for (auto x : sets[0]) ans[x] = 3;
        for (auto x : sets[1]){
            if (lim1) {
                lim1--;
                ans[x] = 1;
            }
            else if (lim2){
                lim2--;
                ans[x] = 2;
            }
            else ans[x] = 3;
        }
    }
    else{
        for (auto x : sets[0]){
            if (lim1) {
                lim1--;
                ans[x] = 1;
            }
            else ans[x] = 3;
        }
        for (auto x : sets[1]){
            if (lim2) {
                lim2--;
                ans[x] = 2;
            }
            else ans[x] = 3;
        }
    }
    int cur[4]; iota(cur,cur+4,0);
    for (int i = 1; i <= n; i++){
        printf("%d ",cur[ans[i]]);
        cur[ans[i]]+=3;
    }
}
