#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <queue>
#include <bitset>
#include <stack>
#include <functional>

#ifdef LOCAL
    #define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
    #define eprintf(...) 42
#endif

#define rep_(i, a_, b_, a, b, ...) for (int i = (a), i##_len = (b); i < i##_len; ++i)
#define rep(i, ...) rep_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define reprev_(i, a_, b_, a, b, ...) for (int i = (b-1), i##_min = (a); i >= i##_min; --i)
#define reprev(i, ...) reprev_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define all(x) (x).begin(), (x).end()
template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }
template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair <int,int> P;
typedef long double ld;

int main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n; cin >> n;
    vector<vector<int> > graph(n);
    rep (i, n - 1) {
        int a, b; cin >> a >> b; a--; b--;
        graph[a].push_back(b);
        graph[b].push_back(a);
    }
    vector<bool> used(n);
    vector<int> dist(n); rep (i, n) dist[i] = -1;
    int dist_count[2] = {};
    int start = -1;
    rep (i, n) {
        if (graph[i].size() == 1) start = i;
    }
    eprintf("start: %d\n", start);
    priority_queue<P> que;
    used[start] = true;
    dist[start] = 0;
    dist_count[0] = 1;
    que.emplace(0, start);
    while (!que.empty()) {
        P p = que.top(); que.pop();
        for (int i : graph[p.second]) {
            if (used[i]) continue;
            used[i] = true;
            dist[i] = p.first + 1;
            dist_count[1 - p.first % 2]++;
            que.emplace(p.first + 1, i);
        }
    }
    rep (i, n) eprintf("%d ", dist[i]); eprintf("\n");
    eprintf("%d %d\n", dist_count[0], dist_count[1]);

    vector<int> perm(n);
    int used_max[3] = {3, 1, 2};
    if (max(dist_count[0], dist_count[1]) * 3 < 2 * n) {
        bool chk = (dist_count[0] > dist_count[1]);
        rep (i, n) {
            if ((dist[i] % 2) == 1) {
                if (used_max[1] > n) {
                    perm[i] = used_max[0];
                    used_max[0] += 3;
                } else {
                    perm[i] = used_max[1];
                    used_max[1] += 3;
                }
            } else {
                if (used_max[2] > n) {
                    perm[i] = used_max[0];
                    used_max[0] += 3;
                } else {
                    perm[i] = used_max[2];
                    used_max[2] += 3;
                }
            }
        }
    } else {
        bool chk = (dist_count[0] > dist_count[1]);
            rep (i, n) {
                if ((dist[i] % 2) ^ chk == 1) {
                    if (used_max[2] <= n) {
                        perm[i] = used_max[2];
                        used_max[2] += 3;
                    } else if (used_max[1] <= n) {
                        perm[i] = used_max[1];
                        used_max[1] += 3;
                    } else {
                        perm[i] = used_max[0];
                        used_max[0] += 3;
                    }
                } else {
                    perm[i] = used_max[0];
                    used_max[0] += 3;
                }
            }
    }
    rep (i, n) cout << perm[i] << (i != i_len - 1 ? ' ' : '\n');
    return 0;
}

Measured memory_bytes: 15980