#include<bits/stdc++.h>
#define st first
#define nd second 
using  namespace std;
using pii = pair<int, int>;
using ll = long long;
const int N = 2e5 + 10;
std::vector<int> gr[N];
stack<int> bip[2];
void dfs(int node,int from, int value){
	bip[value].push(node);
	for(auto it: gr[node]){
		if(it == from) continue;
		dfs(it, node, 1 - value);
	}
	return;

}
map<pii, int> mp;
int sz[2], ans[N];
bool comp(int &lhs, int &rhs){
	return sz[lhs] < sz[rhs];
}
int main(){
	int n,x,y;
	scanf("%d",&n);
	for(int i =1; i <= n-1;i++){
		scanf("%d%d",&x,&y);
		gr[x].push_back(y);
		gr[y].push_back(x);
	}
	dfs(1, 0, 0);

	sz[0] = (int)bip[0].size(), sz[1] = (int)bip[1].size();
	fprintf(stderr, "%d\n", sz[0] + sz[1]);
	int k  = sz[0] > sz[1] ? 0 : 1;
	for(int i = 1; i <= n; i+=3){
		ans[bip[k].top()] = i;
		bip[k].pop();
	}
	sz[0] = (int)bip[0].size(), sz[1] = (int)bip[1].size();
	k = sz[0] > sz[1] ? 0 : 1;
	for(int i = 2; i <= n; i+=3){
		ans[bip[k].top()] = i;
		bip[k].pop();
	}
	int st = 3;
	for(int i = 1;i <= n;i++){
		if(ans[i] == 0) ans[i] = st, st+=3;
		printf("%d ",ans[i]);
	}



}