Problem:
Score : 600 points

Problem Statement

We have a tree with N vertices. The vertices are numbered 1 to N, and the i-th edge connects Vertex a_i and Vertex b_i.

Takahashi loves the number 3. He is seeking a permutation p_1, p_2, \ldots , p_N of integers from 1 to N satisfying the following condition:

For every pair of vertices (i, j), if the distance between Vertex i and Vertex j is 3, the sum or product of p_i and p_j is a multiple of 3.

Here the distance between Vertex i and Vertex j is the number of edges contained in the shortest path from Vertex i to Vertex j.

Help Takahashi by finding a permutation that satisfies the condition.

Constraints

2\leq N\leq 2\times 10^5

1\leq a_i, b_i \leq N

The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N
a_1 b_1
a_2 b_2
\vdots
a_{N-1} b_{N-1}

Output

If no permutation satisfies the condition, print -1.

Otherwise, print a permutation satisfying the condition, with space in between.
If there are multiple solutions, you can print any of them.

Sample Input 1

5
1 2
1 3
3 4
3 5

Sample Output 1

1 2 5 4 3

The distance between two vertices is 3 for the two pairs (2, 4) and (2, 5).

p_2 + p_4 = 6

p_2\times p_5 = 6

Thus, this permutation satisfies the condition.

Solution in Rust (1.15.1) (judge verdict: Wrong Answer)

// This code is generated by [cargo-atcoder](https://github.com/tanakh/cargo-atcoder)
// Original source code:
/*
use competitive::prelude::*;

fn main() {
    input! {
        n: usize,
        ab: [(Usize1, Usize1); n-1],
    }

    let mut g = vec![vec![]; n];
    for (u, v) in ab {
        g[u].push(v);
        g[v].push(u);
    }

    let mut dmap = vec![0; n];

    dfs(0, None, 0, &g, &mut dmap);

    let mut mm = vec![0; 3];

    for &d in dmap.iter() {
        mm[d % 3] += 1;
    }

    let mut xx = vec![0; 3];

    for i in 1..=n {
        xx[i % 3] += 1;
    }

    let mut alloc = None;

    'outer: for a in 0..3 {
        for b in 0..3 {
            for c in 0..3 {
                if a == b || b == c || c == a {
                    continue;
                }

                if mm[0] == xx[a] && mm[1] == xx[b] && mm[2] == xx[c] {
                    alloc = Some(vec![a, b, c]);
                    break 'outer;
                }
            }
        }
    }

    if alloc.is_none() {
        echo!(-1);
        return;
    }

    let mut ans = vec![0; n];
    let alloc = alloc.unwrap();

    let mut cnt = vec![3, 1, 2];

    for i in 0..n {
        let d = dmap[i] % 3;
        ans[i] = cnt[alloc[d]];
        cnt[alloc[d]] += 3;
    }

    echo!(ans);
}

fn dfs(cur: usize, prev: Option<usize>, dep: usize, g: &Vec<Vec<usize>>, dmap: &mut Vec<usize>) {
    dmap[cur] = dep;

    for &next in g[cur].iter() {
        if Some(next) == prev {
            continue;
        }

        dfs(next, Some(cur), dep + 1, g, dmap);
    }
}
*/

fn main() {
    let exe = "/tmp/bin4D1DEFC1";
    std::io::Write::write_all(&mut std::fs::File::create(exe).unwrap(), &decode(BIN)).unwrap();
    std::fs::set_permissions(exe, std::os::unix::fs::PermissionsExt::from_mode(0o755)).unwrap();
    std::process::exit(std::process::Command::new(exe).status().unwrap().code().unwrap())
}

fn decode(v: &str) -> Vec<u8> {
    let mut ret = vec![];
    let mut buf = 0;
    let mut tbl = vec![64; 256];
    for i in 0..64 { tbl[TBL[i] as usize] = i as u8; }
    for (i, c) in v.bytes().filter_map(|c| { let c = tbl[c as usize]; if c < 64 { Some(c) } else { None } }).enumerate() {
        match i % 4 {
            0 => buf = c << 2,
            1 => { ret.push(buf | c >> 4); buf = c << 4; }
            2 => { ret.push(buf | c >> 2); buf = c << 6; }
            3 => ret.push(buf | c),
            _ => unreachable!(),
        }
    }
    ret
}

const TBL: &'static [u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const BIN: &'static str = "<base64-encoded ELF executable removed>";