#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <clocale>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

#define IOS ios::sync_with_stdio(false); cin.tie(0);
#define FOR(i, s, n) for(int i = (s), i##_len=(n); i < i##_len; ++i)
#define FORS(i, s, n) for(int i = (s), i##_len=(n); i <= i##_len; ++i)
#define VFOR(i, s, n) for(int i = (s); i < (n); ++i)
#define VFORS(i, s, n) for(int i = (s); i <= (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) FORS(i, 0, n)
#define VREP(i, n) VFOR(i, 0, n)
#define VREPS(i, n) VFORS(i, 0, n)
#define RFOR(i, s, n) for(int i = (s), i##_len=(n); i >= i##_len; --i)
#define RFORS(i, s, n) for(int i = (s), i##_len=(n); i > i##_len; --i)
#define RREP(i, n) RFOR(i, n, 0)
#define RREPS(i, n) RFORS(i, n, 0)
#define ALL(v) (v).begin(), (v).end()
#define SORT(v) sort(ALL(v))
#define RSORT(v) sort(ALL(v), greater<decltype(v[0])>())
#define SZ(x) ((int)(x).size())
#define REV(x) reverse(ALL(x))
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define MT make_tuple
#define BIT(n) (1LL<<(n))
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end())

using ld = long double;
using ll = long long;
using ui = unsigned int;
using ull = unsigned long long;
using Pi_i = pair<int, int>;
using Pll_ll = pair<ll, ll>;
using VB = vector<bool>;
using VC = vector<char>;
using VD = vector<double>;
using VI = vector<int>;
using VLL = vector<ll>;
using VS = vector<string>;
using VSH = vector<short>;
using VULL = vector<ull>;

const int MOD = 1000000007;
const int INF = 1000000000;
const int NIL = -1;
const double EPS = 1E-10;

template<class T, class S>
bool chmax(T &a, const S &b){
    if(a < b){
        a = b; return true;
    }
    return false;
}
template<class T, class S>
bool chmin(T &a, const S &b){
    if(b < a){
        a = b; return true;
    }
    return false;
}

int main(){
    int N; cin >> N;
    vector<VI> to(N), d3(N); VI p(N), odd, even;
    REP(i, N-1){
        int a, b; cin >> a >> b; --a; --b;
        to[a].PB(b); to[b].PB(a);
    }
    VI d(N, -1);
    d[0] = 0;
    queue<int> que;
    que.push(0);
    while(que.size()){
        int e = que.front(); que.pop();
        if(d[e] % 2) odd.PB(e);
        else even.PB(e);
        for(auto ne: to[e]){
            if(d[ne] == -1){
                d[ne] = d[e] + 1;
                que.push(ne);
            }
        }
    }
    VI th, tw, on;
    for(int i = N; i > 0; --i){
        if(i % 3 == 0) th.PB(i);
        else if(i % 3 == 1) on.PB(i);
        else if(i % 3 == 2) tw.PB(i);
    }
    if(odd.size() <= N/3){
        for(auto e: odd){
            p[e] = *(th.rbegin());
            th.pop_back();
        }
        int id = 0;
        for(; !th.empty() && id < even.size(); ++id){
            p[even[id]] = *(th.rbegin());
            th.pop_back();
        }
        for(; !tw.empty() && id < even.size(); ++id){
            p[even[id]] = *(tw.rbegin());
            tw.pop_back();
        }
        for(; !on.empty() && id < even.size(); ++id){
            p[even[id]] = *(on.rbegin());
            on.pop_back();
        }
    }else if(even.size() <= N/3){
        for(auto e: even){
            p[e] = *(th.rbegin());
            th.pop_back();
        }
        int id = 0;
        for(; !th.empty() && id < odd.size(); ++id){
            p[odd[id]] = *(th.rbegin());
            th.pop_back();
        }
        for(; !tw.empty() && id < odd.size(); ++id){
            p[odd[id]] = *(tw.rbegin());
            tw.pop_back();
        }
        for(; !on.empty() && id < odd.size(); ++id){
            p[odd[id]] = *(on.rbegin());
            on.pop_back();
        }
    }else{
        int oid = 0, eid = 0;
        for(; !on.empty() && oid < odd.size(); ++oid){
            p[odd[oid]] = *(on.rbegin());
            on.pop_back();
        }
        for(; !th.empty() && oid < odd.size(); ++oid){
            p[odd[oid]] = *(th.rbegin());
            th.pop_back();
        }
        for(; !tw.empty() && eid < even.size(); ++eid){
            p[even[eid]] = *(tw.rbegin());
            tw.pop_back();
        }
        for(; !th.empty() && eid < even.size(); ++eid){
            p[even[eid]] = *(th.rbegin());
            th.pop_back();
        }
    }
    REP(i, N){
        if(i) cout << " ";
        cout << p[i];
    }
    cout << endl;
    return 0;
}


Measured memory_bytes: 22236