#include <bits/stdc++.h>
#define pb emplace_back
using namespace std;

const int maxn = 2e5;
int ans[maxn + 11],col[maxn + 11];
vector <int> edge[maxn + 11],ver[maxn + 11];
int cnt[5];

int R = 0 , B = 0;

void dfs(int x,int fa,int d) {
	col[x] = d % 2;
	if (d % 2 == 1) R++; else B++;
	ver[d].pb(x);
	for (auto v : edge[x]) {
		if (v == fa) continue;
		dfs(v , x , d + 1);
	}
}

int main(){
	int n;
	scanf("%d" , &n);
	for (int i = 1; i < n; i++) {
		int u,v;
		scanf("%d %d",&u,&v);
		edge[u].pb(v);
		edge[v].pb(u);
	} 
	dfs(1 , 0 , 0);
	if (R > B) {
		swap(R , B);
		for (int i = 1; i <= n; i++) col[i] ^= 1;
	}
	if (R <= n / 3) {
		int l = 1; int r = 3;

		for (int i = 1; i <= n; i++) {
			if (col[i]) ans[i] = r , r += 3;
			else {
				if (l <= n) {
					ans[i] = l;
					l++; if (l % 3 == 0) l++;
				}
				else {
					ans[i] = r , r += 3;
				}
			}
		}
	}
	else {
		cnt[1] = 1; cnt[2] = 2; cnt[3] = 3;
		for (int i = 1; i <= n; i++) {
			if (col[i]) {
				if (cnt[1] <= n) ans[i] = cnt[1] , cnt[1] += 3;
				else ans[i] = cnt[3] , cnt[3] += 3;
			}
			else {
				if (cnt[2] <= n) ans[i] = cnt[2] , cnt[2] += 3;
				else ans[i] = cnt[3] , cnt[3] += 3;
			}
		}
	}
	for (int i = 1; i <= n; i++) printf("%d " , ans[i]);
} 
