#include"bits/stdc++.h"
using namespace std;
typedef long long ll;
const ll MOD = (ll)(1e9+7);
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for(int (i)=0; (i)<(int)(n); (i)++)
int dx[4]={ 1,0,-1,0 };
int dy[4]={ 0,1,0,-1 };

// 0-1 bfs をして 1 と 2 を割り振る
// どっちも 3 に変えても損はしない

int N;
int a[200000], b[200000];
vector<int> e[200000];

int ans[200000];

void dfs(int n, int s, int b){
    ans[n] = s;
    rep(i, e[n].size()){
        if(e[n][i] == b) continue;
        dfs(e[n][i], s^1, n);
    }
}

signed main(){
    cin >> N;
    rep(i, N-1){
        cin >> a[i] >> b[i];
        a[i]--;
        b[i]--;
        e[a[i]].pb(b[i]);
        e[b[i]].pb(a[i]);
    }
    dfs(0, 0, -1);
    rep(i, N) ans[i] += 1;

    bool flg = true;
    int x = 1;
    int y = 2;
    int z = 3;
    vector<int> v;
    rep(i, N){
        if(ans[i] == 1){
            if(x <= N){
                v.pb(x);
                x += 3;
                continue;
            }
            if(z <= N){
                v.pb(z);
                z += 3;
                continue;
            }
            flg = false;
            break;
        }
        if(ans[i] == 2){
            if(y <= N){
                v.pb(y);
                y += 3;
                continue;
            }
            if(z <= N){
                v.pb(z);
                z += 3;
                continue;
            }
            flg = false;
            break;
        }
    }
    if(flg){
        rep(i, N){
            if(i != 0) cout << " ";
            cout << v[i];
        }
        cout << endl;
        return 0;
    }

    flg = true;
    x = 1;
    y = 2;
    z = 3;
    v.clear();
    rep(i, N){
        if(ans[i] == 1){
            if(z <= N){
                v.pb(z);
                z += 3;
            }
            else{
                flg = false;
                break;
            }
        }
        if(ans[i] == 2){
            if(x <= N){
                v.pb(x);
                x += 3;
                continue;
            }
            if(y <= N){
                v.pb(y);
                y += 3;
                continue;
            }
            if(z <= N){
                v.pb(z);
                z += 3;
                continue;
            }
            flg = false;
            break;
        }
    }
    if(flg){
        rep(i, N){
            if(i != 0) cout << " ";
            cout << v[i];
        }
        cout << endl;
        return 0;
    }

    flg = true;
    x = 1;
    y = 2;
    z = 3;
    v.clear();
    rep(i, N){
        if(ans[i] == 2){
            if(z <= N){
                v.pb(z);
                z += 3;
            }
            else{
                flg = false;
                break;
            }
        }
        if(ans[i] == 1){
            if(x <= N){
                v.pb(x);
                x += 3;
                continue;
            }
            if(y <= N){
                v.pb(y);
                y += 3;
                continue;
            }
            if(z <= N){
                v.pb(z);
                z += 3;
                continue;
            }
            flg = false;
            break;
        }
    }
    if(flg){
        rep(i, N){
            if(i != 0) cout << " ";
            cout << v[i];
        }
        cout << endl;
        return 0;
    }
}

Measured memory_bytes: 25208