// #define STOPIT
#include<bits/stdc++.h>
#define rep(i,n,m) for(int i = (n); i <(m); i++)
#define rrep(i,n,m) for(int i = (n) - 1; i >=(m); i--)
using namespace std;
using ll = long long;

void debug_impl() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_impl(Head head, Tail... tail) { cerr << " " << head; debug_impl(tail...); }
#ifndef STOPIT
    #define debug(...)\
    do {\
        cerr << boolalpha << "[" << #__VA_ARGS__ << "]:";\
        debug_impl(__VA_ARGS__);\
        cerr << std::noboolalpha;\
    } while (false)
#else
    #define debug(...) {}
#endif

template < typename Container, typename Value = typename Container::value_type, enable_if_t<!is_same< Container, string >::value, nullptr_t> = nullptr>
istream& operator>> (istream& is, Container& v)
  { for (auto & x : v) { is >> x; } return is; }

template < typename Container, typename Value = typename Container::value_type, enable_if_t<!is_same< Container, string >::value, nullptr_t> = nullptr >
ostream& operator<< (ostream& os, Container const& v) {
  os << "{";
  for (auto it = v.begin(); it != v.end(); it++)
    {os << (it != v.begin() ? "," : "") << *it;}
  return os << "}";
}
int main()
{
    int N;
    cin >> N;

    vector<int> edge[N];
    rep(i, 0, N-1)
    {
        int a, b;
        cin >> a >> b;
        --a, --b;
        edge[a].push_back(b);
        edge[b].push_back(a);
    }

    // bfs with coloring
    queue<int> black, white;
    {
        queue<pair<int, int>> que;
        unordered_set<int> visited;
        que.push(make_pair(0, 0));
        black.push(0);
        visited.insert(0);
        while (que.size()>0)
        {
            int now, color;
            tie(now, color) = que.front();
            que.pop();

            color = (color+1)%2;
            for (int next: edge[now])
            {
                if (visited.count(next)>0) continue;

                que.push(make_pair(next, color));
                visited.insert(next);

                if (color) white.push(next);
                else black.push(next);
            }
        }
    }
    // while (!black.empty())
    // {
    //     cout << black.front() << ' ' ;
    //     black.pop();
    // }
    // cout << "*********" << endl;
    // while (!white.empty())
    // {
    //     cout << white.front() << ' ' ;
    //     white.pop();
    // }
    // cout << "*********" << endl;
    

    if (black.size()<white.size()) swap(black, white);

    vector<int> ans(N, -1);
    rep(i, 1, N+1)
    {
        if (i%3==1)
        {
            int v = black.front();
            black.pop();
            ans[v] = i;
            // cout << v << ' ' << i << endl;
        }
    }

    if (black.size()<white.size()) swap(black, white);

    rep(i, 1, N+1)
    {
        if (i%3==2)
        {
            int v = black.front();
            black.pop();
            ans[v] = i;
        }
    }

    rep(i, 1, N+1)
    {
        if (i%3==0)
        {
            int v;
            if (!black.empty())
            {
                v = black.front();
                black.pop();
            }
            else
            {
                v = white.front();
                white.pop();
            }
            // cout << v << '-' << i << endl;
            ans[v] = i;
        }
    }
    rep(i, 0, N) printf("%d ", ans[i]);
    cout << endl;
    return 0;
}


Measured memory_bytes: 22168