#include <iostream>
#include <map>
#include <set>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <fstream>
#include <bitset>
#include <queue>
#include <stack>
#include <deque>
#include <complex>
#include <iomanip>
#include <stdio.h>
#include <string.h>
#include <random>
#include <functional>

using std::cin;
using std::cout;
using std::cerr;
using std::endl;

using std::map;
using std::set;
using std::bitset;
using std::vector;
using std::string;
using std::multimap;
using std::multiset;
using std::deque;
using std::queue;
using std::stack;
using std::pair;
using std::iterator;

using std::sort;
using std::stable_sort;
using std::reverse;
using std::max_element;
using std::min_element;
using std::unique;
using std::ios_base;
using std::swap;
using std::fill;

using std::setprecision;
using std::fixed;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef string S;

ll min(ll a, ll b) {return a < b ? a : b;}
ll min(int a, ll b) {return a < b ? a : b;}
ll min(ll a, int b) {return a < b ? a : b;}
ll min(int a, int b) {return a < b ? a : b;}

ll max(ll a, ll b) {return a > b ? a : b;}
ll max(int a, ll b) {return a > b ? a : b;}
ll max(ll a, int b) {return a > b ? a : b;}
ll max(int a, int b) {return a > b ? a : b;}

namespace MySpace{

};

#define F(i, n) for (int (i) = 0; (i) != (n); (i)++)
#define fi first
#define se second
#define re return
#define all(x) (x).begin(), (x).end()
#define int long long

const int N = 501000;
const int MOD = 1e9 + 7;

int n;
vector<int> graph[N];
int color[N];
int cc[N];

void dfs(int v, int col)
{
    color[v] = col;
    cc[col]++;
    for (auto u : graph[v])
    {
        if (color[u] == 0)
        {
            dfs(u, 3 - col);
        }
    }
}

int perm[N];

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    for (int i = 0; i < n - 1; i++)
    {
        int a, b;
        cin >> a >> b;
        graph[a].push_back(b);
        graph[b].push_back(a);
    }
    dfs(1, 1);
    int a = n / 3, b = n / 3, c = n / 3;
    if (n % 3 == 1) a++;
    if (n % 3 == 2) a++, b++;
    if (c >= cc[1] || c >= cc[2])
    {
        if (c < cc[1])
        {
            swap(cc[1], cc[2]);
            for (int i = 1; i <= n; i++) color[i] = 3 - color[i];
        }
        int x = 3;
        vector<int> kek(n + 5);
        for (int i = 1; i <= n; i++)
        {
            if (x <= n && color[i] == 1)
            {
                perm[i] = x;
                kek[x] = 1;
                x += 3;
            }
        }
        x = 1;
        for (int i = 1; i <= n; i++)
        {
            if (perm[i] == 0)
            {
                perm[i] = x;
                x++;
                while (kek[x])
                    x++;
            }
        }
        for (int i = 1; i <= n; i++) cout << perm[i] << " ";
        return 0;
    }
    int x = 1;
    for (int i = 1; i <= n; i++)
    {
        if (x <= n && color[i] == 1)
        {
            perm[i] = x;
            x += 3;
        }
    }
    x = 2;
    for (int i = 1; i <= n; i++)
    {
        if (x <= n && color[i] == 2)
        {
            perm[i] = x;
            x += 3;
        }
    }
    x = 3;
    for (int i = 1; i <= n; i++)
    {
        if (perm[i] == 0)
        {
            perm[i] = x;
            x += 3;
        }
    }
    for (int i = 1; i <= n; i++) cout << perm[i] << " ";
}


Measured memory_bytes: 31360