#include <iostream>
#include <vector>
#include <algorithm>
#include <tuple>
#include <queue>

using namespace std;

typedef long long ll;
#define fastInp cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);

const ll SIZE = 1e5 * 3 + 10, INF = 1e9 * 1000;
vector<vector<ll>> gr;
bool used[SIZE];
ll col[SIZE], c[2];

void dfs(int v) {
    used[v] = 1;
    c[col[v]]++;
    for (auto to : gr[v]) {
        if (!used[to]) {
            col[to] = !col[v];
            dfs(to);
        }
    }
}

int main() {
    fastInp;
    
    ll n;
    cin >> n;

    gr.resize(n);
    for (int i  = 0; i < n - 1; i++) {
        ll u, v;
        cin >> u >> v;
        u--; v--;
        gr[u].push_back(v);
        gr[v].push_back(u);
    }

    dfs(0);

    vector<ll> perm(n);
    vector<vector<ll>> cnt(3);

    
    for (int i = 1; i <= n; i++) {
        cnt[(i % 3)].push_back(i);
    }
    cerr << col[0] << "\n";
    if (c[0] > c[1]) {
        cerr << "baka\n";
        if (c[0] > max(cnt[1].size(), cnt[2].size()) + cnt[0].size()) {
            for (int i = 0; i < n; i++) {
                if (col[i] == 1) {
                    ll v = cnt[0].back();
                    perm[i] = v;
                    cnt[0].pop_back();
                }
            }
            for (int i = 0; i < n; i++) {
                if (perm[i] == 0) {
                    for (int j = 0; j < 3; j++) {
                        if (cnt[j].size() > 0) {
                            ll v = cnt[j].back();
                            cnt[j].pop_back();
                            perm[i] = v;
                            j = 10;
                        }
                    }
                }
            }
            for (auto cur : perm) cout << cur << " ";
            return 0;
        }
        
    } else {
        if (c[1] > max(cnt[1].size(), cnt[2].size()) + cnt[0].size()) {
            for (int i = 0; i < n; i++) {
                if (col[i] == 0) {
                    ll v = cnt[0].back();
                    perm[i] = v;
                    cnt[0].pop_back();
                }
            }
            for (int i = 0; i < n; i++) {
                if (perm[i] == 0) {
                    for (int j = 0; j < 3; j++) {
                        if (cnt[j].size() > 0) {
                            ll v = cnt[j].back();
                            cnt[j].pop_back();
                            perm[i] = v;
                            j = 10;
                        }
                    }
                }
            }
            for (auto cur : perm) cout << cur << " ";
            return 0;
        }
        
    }
    
        ll mxPar = 0, mxInd = 1;
        if (c[1] > c[0]) mxPar = 1;
        if (cnt[2].size() > cnt[1].size()) mxInd = 2;

    for (int i = 0; i < n; i++) {
        if (col[i] == mxPar) {
            if (cnt[mxInd].size() > 0) {
                perm[i] = cnt[mxInd].back();
                cnt[mxInd].pop_back();
            }

        } else {
            ll ind = (!(mxInd - 1)) + 1;

            if (cnt[ind].size() > 0) {
                perm[i] = cnt[ind].back();
                cnt[ind].pop_back();
            }
        }
    }


    for (int i = 0; i < n; i++) {
        if (perm[i] == 0) {
            if (cnt[0].size() == 0) {
                cout << "-1";
                return 0;
            }
            ll v = cnt[0].back();
            cnt[0].pop_back();
            perm[i] = v;
        }
    }
    
    for (auto cur : perm) cout << cur << " ";

    return 0;
}


Measured memory_bytes: 22604