/* ---------- STL Libraries ---------- */
// IO library
#include <cstdio>

#include <fstream>
#include <iomanip>
#include <ios>
#include <iostream>

// algorithm library
#include <algorithm>

#include <cmath>
#include <numeric>
#include <random>
#include <cstring>
#include <cassert>

// container library
#include <array>
#include <bitset>
#include <deque>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
#include <stack>

// type
#include <cstdint>

/* ---------- Namespace ---------- */
using namespace std;

/* ---------- Type ---------- */
using ll = long long;
#define int ll
#define P pair<ll, ll>

/* ---------- Constants  */
const double PI = 3.141592653589793238462643383279;
const ll MOD = 1e9 + 7;
const int INF = 1LL << 55;

/* v-v-v-v-v-v-v-v-v Main Part v-v-v-v-v-v-v-v-v */
int N;
vector<int> table[200005];
bool mark[200005];

signed main() {
    cin.sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    cin >> N;
    for (int i = 0; i < N-1; i++) {
        int s, t;
        cin >> s >> t;
        s--; t--;
        table[s].push_back(t);
        table[t].push_back(s);
    }

    auto dfs = [&](auto f, int node, int par, bool fill) -> void {
        mark[node] = fill;
        for (int next : table[node]) {
            if (next == par) continue;
            f(f, next, node, !fill);
        }
    };

    dfs(dfs, 0, -1, true);

    int black_cnt = 0;
    int white_cnt = 0;
    for (int i = 0; i < N; i++) {
        if (mark[i]) black_cnt++;
        else white_cnt++;
    }

    vector<int> ans(N);
    int zero = 3;
    int one = 1;
    int two = 2;
    if (black_cnt > N/3 && white_cnt > N/3) {
        for (int i = 0; i < N; i++) {
            if (mark[i]) {
                if (one <= N) {
                    ans[i] = one;
                    one += 3;
                } else {
                    ans[i] = zero;
                    zero += 3;
                }
            } else {
                if (two <= N) {
                    ans[i] = two;
                    two += 3;
                } else {
                    ans[i] = zero;
                    zero += 3;
                }
            }

        }
    } else if (black_cnt <= N/3) {
        for (int i = 0; i < N; i++) {
            if (mark[i]) {
                ans[i] = zero;
                zero += 3;
            } else {
                if (one <= N) {
                    ans[i] = one;
                    one += 3;
                } else if (two <= N) {
                    ans[i] = two;
                    two += 3;
                } else {
                    ans[i] = zero;
                    zero += 3;
                }
            }
        }
    } else if (white_cnt <= N/3) {
        for (int i = 0; i < N; i++) {
            if (!mark[i]) {
                ans[i] = zero;
                zero += 3;
            } else {
                if (one <= N) {
                    ans[i] = one;
                    one += 3;
                } else if (two <= N) {
                    ans[i] = two;
                    two += 3;
                } else {
                    ans[i] = zero;
                    zero += 3;
                }
            }
        }
    }

    for (int i = 0;i < N; i++) {
        cout << ans[i];
        if (i != N-1) cout << " ";
    }
    cout << endl;

    return 0;
}

Measured memory_bytes: 16768