// #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;
using ll = long long int;
using int64 = long long int;
 
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
 
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const int INF = 1LL << 29;
const ll LONGINF = 1LL << 60;
const ll MOD = 1000000007LL;
 
int main() {
    int N; cin >> N;
    vector< vector<int> > G(N);
    for(int i=0; i+1<N; i++) {
        int u, v; scanf("%d%d", &u, &v);
        u--; v--;
        G[u].emplace_back(v);
        G[v].emplace_back(u);
    }

    vector< vector<int> > mod(3);
    bool ok = true;
    vector<int> ans(N), col(N), free_v(N, 1);
    for(int i=0; i<N; i++) {
        if(G[i].size() < 2) continue;

        vector<int> f(G[i].size());
        int cnt = 0;
        for(size_t j=0; j<G[i].size(); j++) {
            int v = G[i][j];
            f[j] = (G[v].size() >= 2);
            cnt += f[j];
        }
        for(size_t j=0; j<G[i].size(); j++) {
            int v = G[i][j];
            if(cnt - f[j] > 0) free_v[v] = false;
        }
    }
    for(int i=0; i<N; i++) fprintf(stderr, "%d ", free_v[i]);
    fprintf(stderr, "\n");
    
    auto dfs = [&](auto&& f, int cur, int par) -> void {
        int c = col[cur] + 1;
        if(!free_v[cur]) {
            if(mod[c].size() > 0) {
                ans[cur] = mod[c].back();
                mod[c].pop_back();
            }
            else if(mod[0].size() > 0) {
                ans[cur] = mod[0].back();
                mod[0].pop_back();
            }
            else {
                ok = false;
            }
        }

        for(auto to : G[cur]) {
            if(to == par) continue;
            col[to] = col[cur] ^ 1;
            f(f, to, cur);
        }
    };

    for(int c=0; c<2; c++) {
        for(int k=0; k<3; k++) mod[k].clear();
        for(int k=1; k<=N; k++) {
            mod[k%3].emplace_back(k);
        }

        ok = true;
        col[0] = c;
        dfs(dfs, 0, -1);

        if(ok) {
            for(int i=0; i<N; i++) {
                if(!free_v[i]) continue;
                for(int k=0; k<3; k++) {
                    if(mod[k].size()) {
                        ans[i] = mod[k].back();
                        mod[k].pop_back();
                        break;
                    }
                }
            }
            for(int i=0; i<N; i++) printf("%d%c", ans[i], " \n"[i + 1 == N]);
            return 0;
        }
    }

    
    {
        int cnt_color[2] = {};
        for(int i=0; i<N; i++) {
            cnt_color[ col[i] ]++;
        }

        for(int c=0; c<2; c++) {
            for(int k=0; k<3; k++) mod[k].clear();
            for(int k=1; k<=N; k++) {
                mod[k%3].emplace_back(k);
            }

            fprintf(stderr, "cnt = %d\n", cnt_color[c]);
            if(cnt_color[c] <= N / 3) {
                for(int i=0; i<N; i++) {
                    if(col[i] == c) {
                        ans[i] = mod[0].back();
                        mod[0].pop_back();
                    }
                    else {
                        bool determine = false;
                        for(int k=1; k<3; k++) {
                            if(mod[k].empty()) continue;
                            determine = true;
                            ans[i] = mod[k].back();
                            mod[k].pop_back();
                            break;
                        }
                        if(!determine) {
                            ans[i] = mod[0].back();
                            mod[0].pop_back();
                        }
                    }
                }
                for(int i=0; i<N; i++) printf("%d%c", ans[i], " \n"[i + 1 == N]);
                return 0;
            }
        }
    }
    cout << -1 << endl;
    return 0;
}



Measured memory_bytes: 17656