import std.stdio, std.string, std.conv, std.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;

enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;


void main() {
    int N;
    scan(N);
    auto adj = new int[][](N, 0);
    foreach (i ; 0 .. N - 1) {
        int ai, bi;
        scan(ai, bi);
        ai--, bi--;
        adj[ai] ~= bi;
        adj[bi] ~= ai;
    }

    auto col = new int[](N);
    col[] = -1;

    auto cc = new int[](3);

    void color(int v, int p, int num) {
        col[v] = num;
        cc[num]++;
        foreach (u ; adj[v]) if (u != p && col[u] == -1) {
            int nex = num == 1 ? 2 : 1;
            color(u, v, nex);
        }
    }

    color(0, -1, 1);

    assert(cc[1] + cc[2] == N);

    int les = 1, many = 2;
    if (cc[1] > cc[2]) {
        swap(les, many);
    }

    assert(cc[les] <= cc[many]);

    auto p = new int[](N);
    p[] = -1;

    auto div = new int[][](3, 0);
    foreach (i ; 1 .. N + 1) {
        div[i % 3] ~= i;
    }

    // 少ない方に全て2が収まるとき
    if (cc[les] >= div[2].length) {
        foreach (i ; 0 .. N) {
            if (col[i] == les) {
                if (!div[2].empty) {
                    p[i] = div[2].front;
                    div[2].popFront;
                }
                else {
                    p[i] = div[0].front;
                    div[0].popFront;
                }
            }
        }
    }
    else {
        // 収まらないときは全て0で埋め尽くすことが出来る
        assert(cc[les] < div[2].length);
        assert(cc[les] <= div[0].length);

        foreach (i ; 0 .. N) {
            if (col[i] == les) {
                assert(!div[0].empty);
                p[i] = div[0].front;
                div[0].popFront;
            }
        }
    }

    // 最後に余った奴はなんでもいいので割り当てる
    foreach (i ; 0 .. N) {
        if (p[i] == -1) {
            bool ok = false;

            foreach (k ; 0 .. 3) {
                if (!div[k].empty) {
                    p[i] = div[k].front;
                    div[k].popFront();
                    ok = true;
                    break;
                }
            }

            assert(ok);
        }
    }

    writefln("%(%s %)", p);
}





void scan(T...)(ref T args) {
    auto line = readln.split;
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront;
    }
    assert(line.empty);
}

void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}

bool chmin(T, U...)(ref T x, U args) {
    bool isChanged;
    foreach (arg; args) if (x > arg) {
        x = arg;
        isChanged = true;
    }
    return isChanged;
}

bool chmax(T, U...)(ref T x, U args) {
    bool isChanged;
    foreach (arg; args) if (x < arg) {
        x = arg;
        isChanged = true;
    }
    return isChanged;
}



