#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
#pragma warning(disable : 4996)
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<math.h>
#include<iomanip>
#include<set>
#include<numeric>
#include<cstring>
#include<cstdio>
#include<functional>
#include<bitset>
#include<limits.h>
#include<cassert>
#include<iterator>
#include<complex>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<time.h>
#include<random>
#include<array>


using namespace std;

#define REP(i, n) for(int i = 0;i < n;++i)
#define REPR(i, n) for(int i = n-1;i >= 0;--i)
#define FOR(i, m, n) for(int i = m;i < n;++i)
#define FORR(i, m, n) for(int i = m-1;i >= n;--i)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define REVERSE(v,n) reverse(v,v+n);
#define VREVERSE(v) reverse(v.begin(), v.end())
#define ll long long
#define print(x) cout<<(x)<<'\n'
#define pe(x) cout<<(x)<<" "
#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define lb(v,n) lower_bound(v.begin(), v.end(), (n))
#define ub(v,n) upper_bound(v.begin(), v.end(), (n))
#define int long long
//#define double long double
#define all(x) (x).begin(), (x).end()
#define print_space(v) REP(i,v.size())cout << v[i] << ((i == v.size() - 1) ? "\n" : " ")
template<typename T1, typename T2> inline void chmin(T1 & a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> inline void chmax(T1& a, T2 b) { if (a < b) a = b; }
typedef pair<int, int>P;
std::random_device rd;
std::mt19937 mt(rd());
constexpr int MOD = 1e9+7; constexpr int MAX = 200020;
const double pi = acos(-1); constexpr double EPS = 1e-8;
constexpr ll INF = 1e18;

const int MAX_V = 202000;
const int MAX_LOG_V = 24;
vector<int> G[MAX_V];
int root;//根の番号

int parent[MAX_LOG_V][MAX_V];//親を2^k回辿って到達する頂点
int depth[MAX_V];

void dfs(int v, int p, int d) {
	parent[0][v] = p;
	depth[v] = d;
	for (int i = 0; i < G[v].size(); i++) {
		if (G[v][i] != p)dfs(G[v][i], v, d + 1);
	}
}

void init(int V) {
	dfs(root, -1, 0);
	for (int k = 0; k + 1 < MAX_LOG_V; k++) {
		for (int v = 0; v < V; v++) {
			if (parent[k][v] < 0)parent[k + 1][v] = -1;
			else parent[k + 1][v] = parent[k][parent[k][v]];
		}
	}
}

int lca(int u, int v) {
	if (depth[u] > depth[v]) {
		swap(u, v);
	}
	for (int k = 0; k < MAX_LOG_V; k++) {
		if ((depth[v] - depth[u]) >> k & 1) {
			v = parent[k][v];
		}
	}
	if (u == v)return u;
	for (int k = MAX_LOG_V - 1; k >= 0; k--) {
		if (parent[k][u] != parent[k][v]) {
			u = parent[k][u];
			v = parent[k][v];
		}
	}
	return parent[0][u];
}

vector<int>g[200020];
vector<int>v[3];
int col[200020];
int id[3] = {};
int cnt[3] = {}, rem[3] = {};
void solve() {
	int N; cin >> N;
	root = 0;
	REP(i, N - 1) {
		int u, v; cin >> u >> v;
		u--, v--;
		G[u].push_back(v);
		G[v].push_back(u);
	}
	init(N);
	REP(i, N) {
		if (depth[i] & 1) {
			cnt[1]++;
			col[i] = 1;
		}
		else {
			cnt[2]++;
			col[i] = 2;
		}
	}
	FOR(i, 1, N + 1) {
		int c = i % 3;
		rem[c]++;
		v[c].push_back(i);
	}
	vector<int>ans(N);
	if (cnt[1] > rem[1] + rem[0]) {
		REP(i, N) {
			if (depth[i] % 2 == 0) {
				ans[i] = v[0][id[0]++];
			}
		}
		vector<int>r;
		for (auto x : v[1])r.push_back(x);
		for (auto x : v[2])r.push_back(x);
		while (id[0] < v[0].size())r.push_back(v[0][id[0]++]);
		int idx = 0;
		REP(i, N) {
			if (depth[i] % 2 == 1)ans[i] = r[idx++];
		}
	}
	else if (cnt[2] > rem[1] + rem[0]) {
		REP(i, N) {
			if (depth[i] % 2 == 1) {
				ans[i] = v[0][id[0]++];
			}
		}
		vector<int>r;
		for (auto x : v[1])r.push_back(x);
		for (auto x : v[2])r.push_back(x);
		while (id[0] < v[0].size())r.push_back(v[0][id[0]++]);
		int idx = 0;
		REP(i, N) {
			if (depth[i] % 2 == 0)ans[i] = r[idx++];
		}
	}
	else {

		if (cnt[1] > cnt[2]) {
			cnt[0] = cnt[1] = cnt[2] = 0;
			REP(i, N) {
				if (depth[i] & 1) {
					col[i] = 1;
				}
				else {
					col[i] = 2;
				}
			}
			REP(i, N) {
				int c = col[i];
				if (cnt[c] == rem[c]) {
					col[i] = 0;
					cnt[0]++;
				}
				else cnt[c]++;
			}
			REP(i, N) {
				int c = col[i];
				//pe(c); print(id[c]);
				ans[i] = v[c][id[c]];
				id[c]++;
			}
		}
		else {
			cnt[0] = cnt[1] = cnt[2] = 0;
			REP(i, N) {
				if (depth[i] % 2 == 0) {
					col[i] = 1;
				}
				else {
					col[i] = 2;
				}
			}
			REP(i, N) {
				int c = col[i];
				if (cnt[c] == rem[c]) {
					col[i] = 0;
					cnt[0]++;
				}
				else cnt[c]++;
			}
			REP(i, N) {
				int c = col[i];
				//pe(c); print(id[c]);
				ans[i] = v[c][id[c]];
				id[c]++;
			}
		}
	}

	print_space(ans);
}

signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	//int q; cin >> q;
	//while (q--)
	solve();
}



Measured memory_bytes: 64616