#include <bits/stdc++.h>
using namespace std;
#define all(V) V.begin(),V.end()
using ll = long long;

vector<int> G[200010];
int B[200010], nb[200010];
int col[3];

void dfs(int c, int v) {
	if (B[c] > 0)return;
	B[c] = v;
	col[v]++;
	for (int i : G[c]) {
		dfs(i, 3 - v);
	}
}

int main() {
	for (int i = 0;i < 200010;i++)B[i] = 0;
	int N, a, b;
	cin >> N;
	for (int i = 1;i < N;i++) {
		cin >> a >> b;
		a--;b--;
		G[a].push_back(b);
		G[b].push_back(a);
	}
	dfs(0, 1);
	int c1 = N / 3;
	if (N % 3 > 0)c1++;

	int c2 = N / 3;
	if (N % 3 == 2)c2++;

	int c3 = N / 3;

	if (col[1] <= c3) {
		for (int i = 0;i < N;i++) {
			if (B[i] == 1)nb[i] = 0;
			else {
				if (c2 > 0)nb[i] = 2, c2--;
				else if (c1 > 0)nb[i] = 1, c1--;
				else nb[i] = 0;
			}
		}
	}
	else if (col[2] <= c3) {
		//2to0
		for (int i = 0;i < N;i++) {
			if (B[i] == 2) nb[i] = 0;
			else {
				if (c2 > 0)nb[i] = 2, c2--;
				else if (c1 > 0)nb[i] = 1, c1--;
				else nb[i] = 0;
			}
		}
	}
	else {
		//1や2から0を作る
		for (int i = 0;i < N;i++) {
			if (B[i] == 1) {
				if (c1 > 0)nb[i] = 1, c1--;
				else nb[i] = 0;
			}
			else {
				if (c2 > 0)nb[i] = 2, c2--;
				else nb[i] = 0;
			}
		}
	}

	c1 = 1, c2 = 2, c3 = 3;
	for (int i = 0;i < N;i++) {
		if (nb[i] == 1) {
			cout << c1 << " ";
			c1 += 3;
		}
		else if (nb[i] == 2) {
			cout << c2 << " ";
			c2 += 3;
		}
		else {
			cout << c3 << " ";
			c3 += 3;
		}
	}
	cout << endl;
}