#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;

int n;
V< V<int> > edges;

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

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

    if (c1 <= n / 3) {
        V<int> ans(n, 0);
        V<int> used(n + 1, 0);
        int tmp = 3;
        for (int i = 0; i < n; i++) {
            if (col[i] == 1) {
                ans[i] = tmp;
                used[tmp] = 1;
                tmp += 3;
            }
        }
        tmp = 1;
        for (int i = 0; i < n; i++) {
            if (ans[i] == 0) {
                ans[i] = tmp;
                used[tmp] = 1;
                tmp++;
                if (used[tmp] == 1) tmp++;
            }
        }
        for (int i = 0; i < n; i++) {
            cout << ans[i] << (i == n - 1 ? '\n' : ' ');
        }
    } else if (c0 <= n / 3) {
        V<int> ans(n, 0);
        V<int> used(n + 1, 0);
        int tmp = 3;
        for (int i = 0; i < n; i++) {
            if (col[i] == 0) {
                ans[i] = tmp;
                used[tmp] = 1;
                tmp += 3;
            }
        }
        tmp = 1;
        for (int i = 0; i < n; i++) {
            if (ans[i] == 0) {
                ans[i] = tmp;
                used[tmp] = 1;
                tmp++;
                if (used[tmp] == 1) tmp++;
            }
        }
        for (int i = 0; i < n; i++) {
            cout << ans[i] << (i == n - 1 ? '\n' : ' ');
        }
    } else {
        V<int> ans(n, 0);
        int tmp = 1;
        for (int i = 0; i < n; i++) {
            if (col[i] == 0) {
                ans[i] = tmp;
                tmp += 3;
                if (tmp > n) break;
            }
        }
        tmp = 2;
        for (int i = 0; i < n; i++) {
            if (col[i] == 1) {
                ans[i] = tmp;
                tmp += 3;
                if (tmp > n) break;
            }
        }
        tmp = 3;
        for (int i = 0; i < n; i++) {
            if (ans[i] == 0) {
                ans[i] = tmp;
                tmp += 3;
            }
        }
        for (int i = 0; i < n; i++) {
            cout << ans[i] << (i == n - 1 ? '\n' : ' ');
        }
    }
    return 0;
}


Measured memory_bytes: 20352