#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <cmath>
#include <string>
#include <iomanip>
#include <deque>
#include <queue>
#include <stack>
#include <set>
#include <complex>
#include <ctime>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef int itn;
const ll LINF = 1e18;
const int INF = 1e9;

//マクロ定義
#define vvint(vec,n,m,l) vector<vector<int>> vec(n, vector<int>(m,l));	// lで初期化
#define vvll(vec,n,m,l) vector<vector<ll>> vec(n,vector<ll>(m,l));
#define vint vector<int>
#define pint pair<int,int>
#define rep(i,a) for(int i=0;i<(a);i++)
#define all(x) (x).begin(),(x).end()
#define debug system("pause")				//デバッグ用
#define ret return 0

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; }

using Graph = vector<vector<ll>>;

#define ketasitei setprecision(15) //15桁表示

const ll MOD = 1000000007;
const double PI = 3.1415926535897932;


int main(void)
{
	cin.tie(0);
	ios::sync_with_stdio(false);

	ll n;
	cin >> n;
	vector<ll> a(n-1), b(n-1);

	Graph g(n);

	rep(i, n-1)
	{
		cin >> a[i] >> b[i];
		g[a[i]-1].push_back(b[i]-1);
		g[b[i]-1].push_back(a[i]-1);
	}

	queue<pair<ll,ll>> dfs;
	rep(i, g[0].size())
	{
		dfs.emplace(g[0][i],1);
	}

	vector<ll> red, blue;
	red.push_back(0);
	vector<ll> watched(n, 0);
	watched[0] = 1;

	while (!dfs.empty())
	{
		pair<ll,ll> p = dfs.front();
		dfs.pop();

		if (watched[p.first])
			continue;

		if (p.second % 2)
		{
			blue.push_back(p.first);
		}
		else
		{
			red.push_back(p.first);
		}

		watched[p.first] = 1;
		rep(i, g[p.first].size())
		{
			dfs.emplace(g[p.first][i], p.second+1);
		}

	}

	ll zero = n / 3;

	ll rs = red.size(), bs = blue.size();

	vector<ll> ans(n);

	ll one = n / 3 + (n % 3 > 0);
	ll two = n / 3 + (n % 3 == 2);
	ll o = 0, t = 0, z = 0;

	if (rs<=n/3)
	{
		for (int i = 0; i < rs; i++)
		{
			ans[red[i]] = i * 3 + 3;
			z++;
		}

		for (int i = 0; i < n - rs; i++)
		{
			if (one)
			{
				ans[blue[i]] = o * 3 + 1;
				o++;
				one--;
			}
			else if(two)
			{
				ans[blue[i]] = t * 3 + 2;
				t++;
				two--;
			}
			else
			{
				ans[blue[i]] = z * 3 + 3;
				z++;
			}
		}
	}
	else if (bs <= n / 3)
	{
		for (int i = 0; i < bs; i++)
		{
			ans[blue[i]] = i * 3 + 3;
			z++;
		}

		for (int i = 0; i < n - bs; i++)
		{
			if (one)
			{
				ans[red[i]] = o * 3 + 1;
				o++;
				one--;
			}
			else if(two)
			{
				ans[red[i]] = t * 3 + 2;
				t++;
				two--;
			}
			else
			{
				ans[red[i]] = z * 3 + 3;
				z++;
			}
		}
	}
	else
	{
		ll rnow = 0, bnow = 0;
		for (int i = 0; i < n / 3 + (n % 3 > 0); i++)
		{
			if (rs < bs)
			{
				ans[blue[bnow]] = bnow *3 + 1;
				bnow++;
			}
			else
			{
				ans[red[rnow]] = rnow * 3 + 1;
				rnow++;
			}
		}
		for (int i = 0; i < n / 3 + (n % 3 == 2); i++)
		{
			if (rs < bs)
			{
				ans[red[rnow]] = rnow * 3 + 2;
				rnow++;
			}
			else
			{
				ans[blue[bnow]] = bnow * 3 + 2;
				bnow++;
			}
		}
		
		ll rrnow = rnow, bbnow = bnow;
		ll zeronow = 0;
		for (int i=0;i+rrnow<red.size();i++)
		{
			ans[red[rnow]] = zeronow * 3 + 3;
			rnow++;
			zeronow++;
		}
		for (int i = 0; i + bbnow < blue.size(); i++)
		{
			ans[blue[bnow]] = zeronow * 3 + 3;
			bnow++;
			zeronow++;
		}

	}

	cout << ans[0];

	rep(i, n-1)
	{
		cout << " " << ans[i+1];
	}
	cout << endl;

	return 0;
}

Measured memory_bytes: 24424