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);
    auto cnt = new int[](3);
    col[] = -1;

    void nibu(int v, int c) {
        col[v] = c;
        cnt[c]++;
        foreach (u ; adj[v]) if (col[u] == -1) {
            nibu(u, c == 1 ? 2 : 1);
        }
    }

    nibu(0, 1);

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

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

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

    if (rem[les].length.to!int <= cnt[les]) {
        foreach (i ; 0 .. N) {
            if (col[i] == les) {
                if (rem[les].empty) {
                    p[i] = rem[0].front;
                    rem[0].popFront();
                }
                else {
                    p[i] = rem[les].front;
                    rem[les].popFront();
                }
            }
        }
    }
    else {
        assert(rem[0].length >= cnt[les]);
        foreach (i ; 0 .. N) {
            if (col[i] == les) {
                assert(!rem[0].empty);
                p[i] = rem[0].front;
                rem[0].popFront;
            }
        }
    }

    foreach (i ; 0 .. N) {
        if (p[i] == -1) {
            foreach (j ; 0 .. 3) {
                if (!rem[j].empty) {
                    p[i] = rem[j].front;
                    rem[j].popFront;
                    break;
                }
            }
        }
    }

    assert(p.all!"a >= 0");

    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;
}

void yes(bool ok, string y = "Yes", string n = "No") {
    return writeln(ok ? y : n);
}
