import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
    std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
    std.typecons, std.container, std.format;

// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on

void main()
{
    long N = lread();
    auto G = new long[][](N);
    foreach (_; 0 .. N - 1)
    {
        long a, b;
        scan(a, b);
        a--, b--;
        G[a] ~= b;
        G[b] ~= a;
    }
    long[2] cnt2;
    auto visited = new bool[](N);
    auto d2 = new long[](N);
    void dfs1(long v, long dist)
    {
        if (visited[v])
            return;
        visited[v] = true;
        d2[v] = dist & 1;
        cnt2[dist & 1]++;
        foreach (w; G[v])
            dfs1(w, dist + 1);
    }

    dfs1(0, 0);

    auto ans = new long[](N);
    long[3] cnt3;
    cnt3[0] = 1;

    foreach (i; 0 .. N)
    {
        if (N / 3 < cnt2[0] && N / 3 < cnt2[1])
        {
            ans[i] = (cnt3[1 + d2[i]]++) * 3 + (1 + d2[i]);
            if (N < ans[i])
                ans[i] = (cnt3[0]++) * 3;
        }
        foreach (j; 0 .. 2)
            if (cnt2[j] <= N / 3)
            {
                if (d2[i] == j)
                    ans[i] = (cnt3[0]++) * 3;
                if (d2[i] == (j + 1) % 2)
                {
                    ans[i] = (cnt3[1]++) * 3 + 1;
                    if (N < ans[i])
                        ans[i] = (cnt3[2]++) * 3 + 2;
                    if (N < ans[i])
                        ans[i] = (cnt3[0]++) * 3;
                }
            }
        if (N < ans[i])
        {
            writeln(-1);
            return;
        }
    }
    ans.map!text().join(' ').writeln();
}
