using System;
using System.Collections.Generic;
using CompLib.Util;

public class Program
{
    private int N;
    private List<int>[] Edges;

    // 3で割ったあまり
    private int[] M;

    public void Solve()
    {
        var sc = new Scanner();
        N = sc.NextInt();
        Edges = new List<int>[N];
        for (int i = 0; i < N; i++)
        {
            Edges[i] = new List<int>();
        }

        for (int i = 0; i < N - 1; i++)
        {
            int a = sc.NextInt() - 1;
            int b = sc.NextInt() - 1;
            Edges[a].Add(b);
            Edges[b].Add(a);
        }

        // 2分グラフ
        // 片方 N/3以下
        // 片方 全部 0 残り適当
        // N/3より大きい
        // 片側 1 もう片側2 0 適当
        // 

        int[] d = new int[N];
        for (int i = 0; i < N; i++)
        {
            d[i] = -1;
        }

        var q = new Queue<int>();
        q.Enqueue(0);
        d[0] = 0;
        while (q.Count > 0)
        {
            var p = q.Dequeue();
            foreach (int i in Edges[p])
            {
                if (d[i] != -1) continue;
                d[i] = (d[p] + 1) % 2;
                q.Enqueue(i);
            }
        }

        var la = new List<int>();
        var lb = new List<int>();
        for (int i = 0; i < N; i++)
        {
            if (d[i] % 2 == 0) la.Add(i);
            else lb.Add(i);
        }

        if (la.Count > lb.Count)
        {
            var t = la;
            la = lb;
            lb = t;
        }

        var ans = new int[N];
        if (la.Count <= N / 3)
        {
            int m = 3;
            foreach (int i in la)
            {
                ans[i] = m;
                m += 3;
            }

            int o = (N + 2) / 3;
            int t = (N + 1) / 3;
            int tt = 1;
            for (int i = 0; i < o; i++)
            {
                ans[lb[i]] = tt;
                tt += 3;
            }

            tt = 2;
            for (int i = o; i < o + t; i++)
            {
                ans[lb[i]] = tt;
                tt += 3;
            }

            for (int i = o + t; i < lb.Count; i++)
            {
                ans[lb[i]] = m;
                m += 3;
            }
        }
        else
        {
            int z = N / 3;
            int o = (N + 2) / 3;
            int t = (N + 1) / 3;
            int tmp = 2;
            int tmp2 = 3;
            for (int i = 0; i < t; i++)
            {
                ans[la[i]] = tmp;
                tmp += 3;
            }

            for (int i = t; i < la.Count; i++)
            {
                ans[la[i]] = tmp2;
                tmp2 += 3;
            }

            tmp = 1;
            for (int i = 0; i < o; i++)
            {
                ans[lb[i]] = tmp;
                tmp += 3;
            }

            for (int i = o; i < lb.Count; i++)
            {
                ans[lb[i]] = tmp2;
                tmp2 += 3;
            }
        }

        Console.WriteLine(string.Join(" ", ans));
    }


    public static void Main(string[] args) => new Program().Solve();
}

namespace CompLib.Util
{
    using System;
    using System.Linq;

    class Scanner
    {
        private string[] _line;
        private int _index;
        private const char Separator = ' ';

        public Scanner()
        {
            _line = new string[0];
            _index = 0;
        }

        public string Next()
        {
            while (_index >= _line.Length)
            {
                _line = Console.ReadLine().Split(Separator);
                _index = 0;
            }

            return _line[_index++];
        }

        public int NextInt() => int.Parse(Next());
        public long NextLong() => long.Parse(Next());
        public double NextDouble() => double.Parse(Next());
        public decimal NextDecimal() => decimal.Parse(Next());
        public char NextChar() => Next()[0];
        public char[] NextCharArray() => Next().ToCharArray();

        public string[] Array()
        {
            _line = Console.ReadLine().Split(Separator);
            _index = _line.Length;
            return _line;
        }

        public int[] IntArray() => Array().Select(int.Parse).ToArray();
        public long[] LongArray() => Array().Select(long.Parse).ToArray();
        public double[] DoubleArray() => Array().Select(double.Parse).ToArray();
        public decimal[] DecimalArray() => Array().Select(decimal.Parse).ToArray();
    }
}

Measured memory_bytes: 52192