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 v0;
    long d = () {
        long id = -1;
        long maxdist = -1;
        auto visited = new bool[](N);
        void dfs0(long v, long dist)
        {
            if (visited[v])
                return;
            visited[v] = true;
            if (maxdist < dist)
            {
                id = v;
                maxdist = dist;
            }
            foreach (w; G[v])
                dfs0(w, dist + 1);
        }

        dfs0(0, 0);
        maxdist = -1;
        visited[] = false;
        dfs0(id, 0);
        v0 = id;
        return maxdist;
    }();

    if (d < 3)
    {
        iota(N).map!"a+1"().map!text().join(' ').writeln();
        return;
    }

    auto ans = new long[](N);
    ans[] = -1;
    long[3] cnt;
    cnt[0] = 1;
    void dfs(long v, long dist)
    {
        if (ans[v] != -1)
            return;
        if (dist % 2 == 0)
        {
            if (cnt[1] * 3 + 1 <= N)
                ans[v] = (cnt[1]++) * 3 + 1;
            else
                ans[v] = (cnt[0]++) * 3;
        }
        if (dist % 2 == 1)
        {
            if (cnt[2] * 3 + 2 <= N)
                ans[v] = (cnt[2]++) * 3 + 2;
            else
                ans[v] = (cnt[0]++) * 3;
        }
        // writefln("%s %s %s %s", ans, cnt, v, dist);
        if (N < ans[v])
        {
            writeln(-1);
        }
        else
            foreach (w; G[v])
                dfs(w, dist + 1);
    }

    dfs(v0, 0);
    ans.map!text().join(' ').writeln();
}
