using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class TEST{
	static void Main(){
		Sol mySol =new Sol();
		mySol.Solve();
	}
}

class Sol{
		
	void lca_init(){
		parent = new int[lmax][];
		for(int i=0;i<lmax;i++){
			parent[i]=new int[N];
		}
		depth=new int[N];
		
		root=0;
		//dfs(root,-1,0);//深さを数えるdfs
		bfs(root,0);//深さを数えるbfs
		
		for(int i=0;i<lmax-1;i++){
			for(int j=0;j<N;j++){
				if(parent[i][j]<0){
					parent[i+1][j] = -1;
				}else{
					parent[i+1][j] = parent[i][parent[i][j]];
				}
			}
		}
		
	}
	void bfs(int strt,int dep_strt){
		Queue<int> Q = new Queue<int>();
		parent[0][strt] = -1;
		depth[strt] = 0;
		Q.Enqueue(strt);
		while(Q.Count>0){
			int now=Q.Dequeue();
			int par=parent[0][now];
			foreach(int v in E[now]){
				if(v!=par){
					parent[0][v]=now;depth[v]=depth[now]+1;
					Q.Enqueue(v);
				}
			}
		}
	}

	List<int>[] E;
	int[][] parent;
	int[] depth;
	static int lmax=4;
	int root;
	
	int anc(int idx, int h){
		//Console.WriteLine("{0} {1}",idx, h);
		if(h == 1) return parent[0][idx];
		if(h == 2) return parent[1][idx];
		if(parent[1][idx] == -1) return -1;
		if(h == 3) return parent[0][parent[1][idx]];
		return -1;
	}
	
	public void Solve(){
		E = new List<int>[N];
		for(int i=0;i<N;i++) E[i] = new List<int>();
		for(int i=0;i<N-1;i++){
			E[A[i]].Add(B[i]);
			E[B[i]].Add(A[i]);
		}
		lca_init();
		
		int[] ans = new int[N];
		for(int i=0;i<N;i++) ans[i] = -1;
		List<int>[] bw = new List<int>[2];
		for(int i=0;i<2;i++) bw[i] = new List<int>();
		for(int i=0;i<N;i++){
			bw[depth[i] % 2].Add(i);
		}
		
		int[] cnt = new int[3];
		cnt[0] = N / 3;
		cnt[1] = N / 3 + (N % 3 != 0 ? 1 : 0);
		cnt[2] = N / 3 + (N % 3 == 2 ? 1 : 0);
		Array.Sort(bw, (x, y) => x.Count.CompareTo(y.Count));
		Queue<int>[] q = new Queue<int>[3];
		for(int i=0;i<3;i++) q[i] = new Queue<int>();
		for(int i=1;i<=N;i++) q[i % 3].Enqueue(i);
		
		if(bw[0].Count < cnt[2]){
			foreach(var n in bw[0]){
				ans[n] = q[0].Dequeue();
			}
			foreach(var n in bw[1]){
				int v = -1;
				if(q[0].Count > 0) v = q[0].Dequeue();
				else if(q[1].Count > 0) v = q[1].Dequeue();
				else if(q[2].Count > 0) v = q[2].Dequeue();
				ans[n] = v;
			}
			
		} else {
			foreach(var n in bw[0]){
				int v = -1;
				if(q[2].Count > 0) v = q[2].Dequeue();
				else if(q[0].Count > 0) v = q[0].Dequeue();
				ans[n] = v;
			}
			foreach(var n in bw[1]){
				int v = -1;
				if(q[1].Count > 0) v = q[1].Dequeue();
				else if(q[0].Count > 0) v = q[0].Dequeue();
				ans[n] = v;
			}
		}
		if(ans.Contains(-1)) throw new Exception("missed");
		
		Console.WriteLine(String.Join(" ",ans));
		
	}
	
	class Pair{
		public int Idx,Dep;
		public Pair(int idx, int dep){
			Idx = idx; Dep = dep;
		}
	}
	
	int N;
	int[] A,B;
	public Sol(){
		N = ri();
		A = new int[N-1];
		B = new int[N-1];
		for(int i=0;i<N-1;i++){
			var d = ria();
			A[i] = d[0] - 1;
			B[i] = d[1] - 1;
		}
	}

	static String rs(){return Console.ReadLine();}
	static int ri(){return int.Parse(Console.ReadLine());}
	static long rl(){return long.Parse(Console.ReadLine());}
	static double rd(){return double.Parse(Console.ReadLine());}
	static String[] rsa(char sep=' '){return Console.ReadLine().Split(sep);}
	static int[] ria(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>int.Parse(e));}
	static long[] rla(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>long.Parse(e));}
	static double[] rda(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>double.Parse(e));}
}


Measured memory_bytes: 59716