#include <iostream>
#include <array>
#include <algorithm>
#include <vector>
#include <bitset>
#include <set>
#include <unordered_set>
#include <cmath>
#include <complex>
#include <deque>
#include <iterator>
#include <numeric>
#include <map>
#include <unordered_map>
#include <queue>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <limits>
#include <iomanip>
#include <functional>
#include <cassert>
using namespace std;

using ll=long long;
template<class T> using V = vector<T>;
template<class T, class U> using P = pair<T, U>;
using vll = V<ll>;
using vvll = V<vll>;
#define rep(i, k, n) for (ll i=k; i<(ll)n; ++i)
#define REP(i, n) rep(i, 0, n)
#define ALL(v) v.begin(),v.end()
template < class T > inline bool chmax(T& a, T b) {if (a < b) { a=b; return true; } return false; }
template < class T > inline bool chmin(T& a, T b) {if (a > b) { a=b; return true; } return false; }
#define DEBUG_VLL(vec) REP(sz, vec.size()) std::cerr<<vec[sz]<<(sz==vec.size()-1?'\n':' ');

const long long MOD = 1000000007;
const long long HIGHINF = (long long)1e18;
const int INF = (int)1e9;

V<int> col;
V< V<int> > edges;

void dfs(int i, int p, int c) {
    col[i] = c;
    for (int e: edges[i]) {
        if (e == p) continue;
        dfs(e, i, c ^ 1);
    }
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n; cin >> n;
    edges.resize(n);
    for (int i = 1; i < n; i++) {
        int a, b; cin >> a >> b;
        a--, b--;
        edges[a].emplace_back(b), edges[b].emplace_back(a);
    }

    col.resize(n, -1);
    dfs(0, -1, 0);
    int cnt0 = 0, cnt1 = 0;
    for (int i = 0; i < n; i++) (col[i] == 0 ? cnt0 : cnt1)++;
    if (cnt0 <= n / 3) {
        V<int> ans(n, -1);
        {
            int i = 1;
            for (int j = 0; j < n; j++) {
                if (col[j] == 0) {
                    ans[j] = i * 3;
                    i++;
                }
            }
            int maxi = (i - 1) * 3;
            i = 1;
            for (int j = 0; j < n; j++) {
                if (col[j] == 1) {
                    ans[j] = i++;
                    if (i % 3 == 0 && i <= maxi) i++;
                }
            }
        }
        for (int i = 0; i < n; i++) cout << ans[i] << '\n';
    } else if (cnt1 <= n / 3) {
        V<int> ans(n, -1);
        {
            int i = 1;
            for (int j = 0; j < n; j++) {
                if (col[j] == 1) {
                    ans[j] = i * 3;
                    i++;
                }
            }
            int maxi = (i - 1) * 3;
            i = 1;
            for (int j = 0; j < n; j++) {
                if (col[j] == 0) {
                    ans[j] = i++;
                    if (i % 3 == 0 && i <= maxi) i++;
                }
            }
        }
        for (int i = 0; i < n; i++) cout << ans[i] << '\n';
    } else {
        V<int> ans(n, -1);
        int idx0 = 0, idx1 = 0;
        for (int i = 0; i < n; i++) {
            if (col[i] == 0 && 3 * idx0 + 1 <= n) {
                ans[i] = 3 * idx0 + 1;
                idx0++;
            } else if (col[i] == 1 && 3 * idx1 + 2 <= n) {
                ans[i] = 3 * idx1 + 2;
                idx1++;
            }
        }
        idx0 = 1;
        for (int i = 0; i < n; i++) if (ans[i] == -1) ans[i] = 3 * idx0++;
        for (int i = 0; i < n; i++) cout << ans[i] << '\n';
    }
    return 0;
}


Measured memory_bytes: 20052