#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <tuple>
#include <cstdio>
#include <bitset>
#include <sstream>
#include <iterator>
#include <numeric>
#include <map>
#include <cstring>
#include <set>
#include <functional>
#include <iomanip>

using namespace std;

#define DEBUG_ //!!提出時にコメントアウト!!
#ifdef DEBUG_
	#define dump(x)  cerr << #x << " = " << (x) << endl;
#else
	#define dump(x)  ;
#endif

#define equals(a,b) (fabs((a)-(b)) < EPS)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back

//#define int long long

typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

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 <typename T>
std::string printVector(const std::vector<T> &data)
{
    std::stringstream ss;
    std::ostream_iterator<T> out_it(ss, ", ");
    ss << "[";
    std::copy(data.begin(), data.end() - 1, out_it);
    ss << data.back() << "]";
    return ss.str();
}

template <typename T>
void print_array(const T &ary, int size){
    REP(i,size){
        cout << ary[i] << " ";
    }
    cout << endl;
}

const int MOD = 1e9+7;
const LL LINF = 1001002003004005006ll;
const int INF = 1001001001;
const double EPS = (1e-10);

vector<vector<int>> g;
VI color;

void dfs(int v, int p, int c){
    color[v] = c;
    for(int u : g[v]){
        if(u == p) continue;
        dfs(u,v,c^1);
    }
}

signed main(int argc, char const *argv[])
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << setprecision(12);

    int N; cin >> N;
    g.resize(N);
    color.resize(N);
    VI ans(N);
    REP(i,N-1){
        int a,b; cin >> a >> b;
        a--; b--;
        g[a].eb(b);
        g[b].eb(a);
    }
    dfs(0,-1,0);
    int cnt0 = 0, cnt1 = 0;
    REP(i,N){
        if(color[i] == 0) cnt0++;
        else cnt1++;
    }
    int d;
    int cnt;
    if(cnt0 <= cnt1) {
        d = 0;
        cnt = cnt0;
    }else {
        d = 1;
        cnt = cnt1;
    }

    if(cnt <= N/3){ //余り0だけ突っ込む
        VI used(N+10);
        int cur = 3;
        REP(i,N){
            if(color[i] == d){
                ans[i] = cur;
                used[cur] = 1;
                cur += 3;
            }
        }
        cur = 1;
        REP(i,N){
            if(ans[i] == 0){
                ans[i] = cur;
                used[cur] = 1;
                while(used[cur]){
                    cur++;
                }
            }
        }

    }else{ //余り2と余り0を突っ込む
        VI used(N+10);
        int cur = 2;
        REP(i,N){
            if(color[i] == d){
                ans[i] = cur;
                used[cur] = 1;
                cur += 3;
                if(cur > N){
                    cur = 3;
                }
            }
        }
        cur = 1;
        REP(i,N){
            if(ans[i] == 0){
                ans[i] = cur;
                used[cur] = 1;
                while(used[cur]){
                    cur++;
                }
            }
        }


    }
    REP(i,N){
        cout << ans[i] << " ";
    }
    cout << endl;


}
// 2部グラフの少ない側 cnt < N/2
// 余り2の方が少ない
// 少ない方に余り2と余り0を突っ込む
// 余り2が入り切らなかったら？
//全部余り0を突っ込む


Measured memory_bytes: 17960