#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;
typedef long long ll;

vector<int> G[200000];
int depth[200000];
bool used[200000];
int cnto, cnte;
int col[200000];
int N;
void dfs(int v, int d){
    used[v] = true;
    depth[v] = d;
    if(d%2 == 0)cnte++;
    else cnto++;
    for(int i = 0; i < G[v].size(); i++){
        if(!used[G[v][i]]) dfs(G[v][i], d+1);
    }
}

void clear(){
    for(int i = 0; i < N; i++) used[i] = false;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    cin >> N;
    for(int i = 0; i < N-1; i++){
        int a, b;
        cin >> a >> b;
        a--; b--;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    dfs(0, 0);
    clear();
    // for(int i = 0; i < N; i++) cout << depth[i] << ' ';
    // cout << endl;
    // cout << cnto << ' ' << cnte << endl;
    if(cnto <= cnte){
        if(cnte-cnto <= N/3){
            int tmpo = 0;
            int tmpe = 0;
            for(int i = 0; i < N; i++){
                if(depth[i]%2 == 0){
                    if(tmpe*3+2 > N) continue;
                    col[i] = tmpe*3+2;
                    used[i] = true;
                    tmpe++;
                }else{
                    if(tmpo*3+1 > N) continue;
                    col[i] = tmpo*3+1;
                    used[i] = true;
                    tmpo++;
                }
            }
            int tmp = 1;
            for(int i = 0; i < N; i++){
                if(!used[i]) {
                    col[i] = tmp*3;
                    tmp++;
                }
            }
        }else{
            // oを3の倍数で塗る。あとはなんでもいい
            int tmp = 1;
            for(int i = 0; i < N; i++){
                if(depth[i]%2 == 1) {
                    col[i] = tmp*3;
                    tmp++;
                    used[i] = true;
                }
            }
            int cur = 1;
            for(int i = 0; i < N; i++){
                if(depth[i]%2 == 0) {
                    if(tmp*3 <= N){
                        col[i] = tmp*3;
                        tmp++;
                    }else{
                        col[i] = cur;
                        if(cur%3 == 1) cur++;
                        else cur+=2;
                    }
                }
            }
        }
    }else{
        if(cnto-cnte <= N/3){
            int tmpo = 0;
            int tmpe = 0;
            for(int i = 0; i < N; i++){
                if(depth[i]%2 == 0){
                    if(tmpe*3+2 > N) continue;
                    col[i] = tmpe*3+2;
                    used[i] = true;
                    tmpe++;
                }else{
                    if(tmpo*3+1 > N) continue;
                    col[i] = tmpo*3+1;
                    used[i] = true;
                    tmpo++;
                }
            }
            int tmp = 1;
            for(int i = 0; i < N; i++){
                if(!used[i]) {
                    col[i] = tmp*3;
                    tmp++;
                }
            }
        }else{
            // eを3の倍数で塗る。あとはなんでもいい
            int tmp = 1;
            for(int i = 0; i < N; i++){
                if(depth[i]%2 == 0) {
                    col[i] = tmp*3;
                    tmp++;
                    used[i] = true;
                }
            }
            int cur = 1;
            for(int i = 0; i < N; i++){
                if(depth[i]%2 == 1) {
                    if(tmp*3 <= N){
                        col[i] = tmp*3;
                        tmp++;
                    }else{
                        col[i] = cur;
                        if(cur%3 == 1) cur++;
                        else cur+=2;
                    }
                }
            }
        }
    }
    for(int i = 0; i < N; i++){
        cout << col[i];
        if(i == N-1) cout << endl;
        else cout << ' ';
    }
}

Measured memory_bytes: 19200