#include<bits/stdc++.h>
using namespace std;

int main() {
    int64_t N;
    cin >> N;
    vector<vector<int64_t>> connect(N);
    for (int64_t i = 0; i < N - 1; i++) {
        int64_t a, b;
        cin >> a >> b;
        a--; b--;
        connect[a].push_back(b);
        connect[b].push_back(a);
    }

    //適当に根を取ってdfsして
    stack<int64_t> st;
    vector<int64_t> depth(N, -1);
    st.push(0);
    depth[0] = 0;
    while (!st.empty()) {
        int64_t t = st.top();
        st.pop();
        for (int64_t next_node : connect[t]) {
            if (depth[next_node] != -1) {
                continue;
            }

            depth[next_node] = depth[t] + 1;
            st.push(next_node);
        }
    }

    int64_t odd_num = 0;
    for (int64_t i = 0; i < N; i++) {
        if (depth[i] % 2 == 1) {
            odd_num++;
        }
    }

    int64_t even_num = N - odd_num;
    if (odd_num > even_num) {
        swap(odd_num, even_num);
        for (int64_t i = 0; i < N; i++) {
            depth[i]++;
        }
    }

    //odd_num < even_numが保証

    if (odd_num <= N / 3) {
        //少ないので3で埋めてしまう
        int64_t num1 = 1;
        int64_t num2 = 2;
        int64_t num3 = 3;
        vector<int64_t> ans(N);
        for (int64_t i = 0; i < N; i++) {
            if (depth[i] % 2 == 1) {
                //3, 6, 9, ...を入れていく
                if (num3 <= N) {
                    ans[i] = num3;
                    num3 += 3;
                } else if (num1 <= N) {
                    ans[i] = num1;
                    num1 += 3;
                } else {
                    cout << -1 << endl;
                    return 0;
                }
            } else {
                //2, 5, 8, ...を入れていく
                //Nを超えていれば1, 4, 7, ...を入れていく
                //それもNを超えていれば3, 6, 9, ...を入れていく
                if (num2 <= N) {
                    ans[i] = num2;
                    num2 += 3;
                } else if (num1 <= N) {
                    ans[i] = num1;
                    num1 += 3;
                } else if (num3 <= N) {
                    ans[i] = num3;
                    num3 += 3;
                } else {
                    cout << -1 << endl;
                    return 0;
                }
            }
        }

        for (int64_t i = 0; i < N; i++) {
            cout << ans[i] << " \n"[i == N - 1];
        }

    } else {
        int64_t num1 = 1;
        int64_t num2 = 2;
        int64_t num3 = 3;
        vector<int64_t> ans(N);
        for (int64_t i = 0; i < N; i++) {
            if (depth[i] % 2 == 0) {
                //1, 4, 7, ...を入れていく
                //Nを超えていれば3, 6, 9, ...を入れていく
                if (num1 <= N) {
                    ans[i] = num1;
                    num1 += 3;
                } else if (num3 <= N) {
                    ans[i] = num3;
                    num3 += 3;
                } else {
                    cout << -1 << endl;
                    return 0;
                }
            } else {
                //2, 5, 8, ...を入れていく
                //Nを超えていれば3, 6, 9, ...を入れていく
                if (num2 <= N) {
                    ans[i] = num2;
                    num2 += 3;
                } else if (num3 <= N) {
                    ans[i] = num3;
                    num3 += 3;
                } else {
                    cout << -1 << endl;
                    return 0;
                }
            }
        }

        for (int64_t i = 0; i < N; i++) {
            cout << ans[i] << " \n"[i == N - 1];
        }

    }
}

Measured memory_bytes: 18024