#include <bits/stdc++.h>
using namespace std;

using ll = long long int;
using P = pair<int, int>;
using P3 = pair<ll,P>;
using PP = pair<P, P>;
constexpr int INF = 1 << 30;
constexpr ll MOD = ll(1e9) + 7;
constexpr int di[] = {0, 1, 0, -1};
constexpr int dj[] = {1, 0, -1, 0};
constexpr double EPS = 1e-8;

struct UnionFindTree {
    vector<int> par;
    vector<int> rank;
    vector<int> siz;
    vector<int> od, ev;

    void init(int n) {
        par.resize(n);
        rank.resize(n);
        siz.resize(n);
        od.resize(n); ev.resize(n);
        for (int i = 0; i < n; i++) {
            par[i] = i;
            rank[i] = 0;
            siz[i] = 1;
        }
    }
    int find(int x) {
        if (par[x] == x) {
            return x;
        } else {
            return par[x] = find(par[x]);
        }
    }
    void unite(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y) return;
        if (rank[x] < rank[y]) swap(x, y);
        if (rank[x] == rank[y]) rank[x]++;
        par[y] = x;
        siz[x] += siz[y];
        od[x] += od[y];
        ev[x] += ev[y];
    }
    bool is_same(int x, int y) {
        return find(x) == find(y);
    }
    int size(int x) {
        x = find(x);
        return siz[x];
    }
    int odd(int x){
        x = find(x);
        return od[x];
    }
    int even(int x){
        x = find(x);
        return ev[x];
    }
};

vector<vector<int> > g;
vector<int> col, bip;
vector<bool> used;

void dfs(int now, int pre){
    for(auto nxt : g[now]){
        if(nxt == pre) continue;
        col[nxt] = (col[now]+1)%3;
        dfs(nxt, now);
    }
}

void bipart(int now, int pre){
    for(auto nxt : g[now]){
        if(nxt == pre) continue;
        bip[nxt] = bip[now]^1;
        bipart(nxt, now);
    }
}

int solve(){
    int n;
    cin >> n;
    g.resize(n);
    col.resize(n);
    used.resize(n+1);
    bip.resize(n);
    UnionFindTree uf;
    uf.init(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);
    }

    bipart(0,-1);
    int ev=0, od=0;
    for(int i=0;i<n;i++){
        if(bip[i]){
            od++;
        }
        else{
            ev++;
        }
    }
    vector<int> ans(n,-1);
    bool ok = true;
    if(od <= n/3){
        for(int i=0,x=3;i<n;i++){
            if(bip[i]){
                ans[i] = x;
                used[x] = true;
                x+=3;
            }
        }
        for(int i=0,x=1;i<n;i++){
            if(!bip[i]){
                while(used[x]) x++;
                ans[i] = x;
                used[x] = true;
            }
        }
    }else if(ev <= n/3){
        for(int i=0,x=3;i<n;i++){
            if(!bip[i]){
                ans[i] = x;
                used[x] = true;
                x+=3;
            }
        }
        for(int i=0,x=1;i<n;i++){
            if(bip[i]){
                while(used[x]) x++;
                ans[i] = x;
                used[x] = true;
            }
        }
    }else{
        for(int i=0,x=1;i<n&&x<=n;i++){
            if(bip[i]){
                ans[i] = x;
                used[x] = true;
                x+=3;
            }
        }
        for(int i=0,x=2;i<n&&x<=n;i++){
            if(!bip[i]){
                ans[i] = x;
                used[x] = true;
                x+=3;
            }
        }
        for(int i=0,x=3;i<n&&x<=n;i++){
            if(ans[i] == -1){
                ans[i] = x;
                used[x] = true;
                x+=3;
            }
        }
        for(int i=0;i<n;i++){
            if(ans[i] < 0) ok = false;
        }
    }
    if(ok){
        for(int i=0;i<n;i++){
            cout << ans[i] << " \n"[i==n-1];
        }
    }else{
        cout << -1 << endl;
    }


    // for(int i=0;i<n;i++){
    //     if(!used[i]){
    //         fill(col.begin(), col.end(), 0);
    //         dfs(i,-1);
    //         for(int j=0;j<n;j++){
    //             if(col[j] == 0){
    //                 uf.unite(i,j);
    //                 used[j] = true;
    //             }
    //         }
    //     }
    // }

    return 0;
}

int main(){
    solve();
    // while(solve()==0);
    // int t; cin >> t; for(;t>0;t--) solve();
    return 0;
}

Measured memory_bytes: 21504