#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <memory>
#include <complex>
#include <numeric>
#include <cstdio>
#include <iomanip>
#include <random>

#define REP(i,m,n) for(int i=int(m);i<int(n);i++)
#define RREP(i,m,n) for(int i=int(n)-1;i>=int(m);--i)
#define EACH(i,c) for (auto &(i): c)
#define all(c) begin(c),end(c)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort(begin(c),end(c))
#define pb emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
//#define int long long

#ifdef LOCAL
#define DEBUG(s) cout << (s) << endl
#define dump(x)  cerr << #x << " = " << (x) << endl
#define BR cout << endl;
#else
#define DEBUG(s) do{}while(0)
#define dump(x) do{}while(0)
#define BR 
#endif
using namespace std;

using UI = unsigned int;
using UL = unsigned long;
using LL = long long;
using ULL = unsigned long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VLL = vector<LL>;
using VVLL = vector<VLL>;
using VS = vector<string>;
using PII = pair<int,int>;
using VP = vector<PII>;
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; }

void predfs(VVI &es, VI &oe, const int &n, int i, int p, int cnt) {
    oe[cnt%2]++;
    for (auto &dst: es[i]) {
        if (dst == p) continue;
        predfs(es,oe,n,dst,i,cnt + 1);
    }
}

void dfs1(VVI &es, VI &cs, VI &vs, const int &n, int i, int p, int cnt) {
    if (cnt % 2 == 0) {
        if (cs[0] * 3 + 1 <= n) {
            vs[i] = cs[0] * 3 + 1;
            cs[0]++;
        } else {
            vs[i] = cs[2] * 3 + 3;
            cs[2]++;
        }
    } else {
        if (cs[1] * 3 + 2 <= n) {
            vs[i] = cs[1] * 3 + 2;
            cs[1]++;
        } else {
            vs[i] = cs[2] * 3 + 3;
            cs[2]++;
        }
    }
    for (auto &dst: es[i]) {
        if (dst == p) continue;
        dfs1(es,cs,vs,n,dst,i,cnt + 1);
    }
}

void dfs2(VVI &es, VI &cs, VI &vs, const int &n, int i, int p, int cnt) {
    if (cnt % 2) {
        if (cs[0] * 3 + 1 <= n) {
            vs[i] = cs[0] * 3 + 1;
            cs[0]++;
        } else {
            vs[i] = cs[2] * 3 + 3;
            cs[2]++;
        }
    } else {
        if (cs[1] * 3 + 2 <= n) {
            vs[i] = cs[1] * 3 + 2;
            cs[1]++;
        } else {
            vs[i] = cs[2] * 3 + 3;
            cs[2]++;
        }
    }
    for (auto &dst: es[i]) {
        if (dst == p) continue;
        dfs2(es,cs,vs,n,dst,i,cnt + 1);
    }
}

void dfs3(VVI &es, VI &cs, VI &vs, const int &n, int i, int p, int cnt) {
    if (cnt % 2 == 0) {
        vs[i] = cs[2] * 3 + 3;
        cs[2]++;
    } else {
        if (cs[0] * 3 + 1 <= n) {
            vs[i] = cs[0] * 3 + 1;
            cs[0]++;
        } else if (cs[1] * 3 + 2 <= n) {
            vs[i] = cs[1] * 3 + 2;
            cs[1]++;
        } else {
            vs[i] = cs[2] * 3 + 3;
            cs[2]++;
        }
    }
    for (auto &dst: es[i]) {
        if (dst == p) continue;
        dfs3(es,cs,vs,n,dst,i,cnt + 1);
    }
}

void dfs4(VVI &es, VI &cs, VI &vs, const int &n, int i, int p, int cnt) {
    if (cnt % 2 == 1) {
        vs[i] = cs[2] * 3 + 3;
        cs[2]++;
    } else {
        if (cs[0] * 3 + 1 <= n) {
            vs[i] = cs[0] * 3 + 1;
            cs[0]++;
        } else if (cs[1] * 3 + 2 <= n) {
            vs[i] = cs[1] * 3 + 2;
            cs[1]++;
        } else {
            vs[i] = cs[2] * 3 + 3;
            cs[2]++;
        }
    }
    for (auto &dst: es[i]) {
        if (dst == p) continue;
        dfs4(es,cs,vs,n,dst,i,cnt + 1);
    }
}

void solve() {
    int n;
    cin >> n;
    VVI es(n);
    int a,b;
    REP(i,0,n-1) {
        cin >> a >> b;
        --a; --b;
        es[a].push_back(b);
        es[b].push_back(a);
    }
    VI cs(3), vs(n);
    VI nums = {n / 3, (n + 2) / 3, (n + 1) / 3};
    VI oe(2);
    predfs(es,oe,n,0,-1,0);

    if (oe[0] >= nums[1] && oe[1] >= nums[2]) {
        dfs1(es,cs,vs,n,0,-1,0);
    } else if (oe[1] >= nums[1] && oe[0] >= nums[2]) {
        dfs2(es,cs,vs,n,0,-1,0);
    } else if (oe[0] <= nums[0]) {
        dfs3(es,cs,vs,n,0,-1,0);
    } else if (oe[1] <= nums[0]) {
        dfs4(es,cs,vs,n,0,-1,0);
    } else {
        cout << -1 << endl;
        return;
    }

    REP(i,0,n) cout << vs[i] << " ";
    cout << endl;
}

signed main() {
    solve();
    
    return 0;
}

Measured memory_bytes: 26880