local mma = math.max
local n = io.read("*n")
local edge = {}
local num = {}
local len = {}
local ccnt, c2cnt, parent = {}, {}, {}
local lim = {}
lim[1] = math.ceil(n / 3)
lim[2] = math.ceil((n - 1) / 3)
lim[3] = math.floor(n / 3)

for i = 1, n do
  edge[i] = {}
  num[i] = 0
  len[i] = -1
  ccnt[i] = 0
  c2cnt[i] = 0
  parent[i] = 0
end
for i = 1, n - 1 do
  local a, b = io.read("*n", "*n")
  table.insert(edge[a], b)
  table.insert(edge[b], a)
end
local tasks = {1}
len[1] = 0
for i = 1, n do
  local src = tasks[i]
  for it = 1, #edge[src] do
    local dst = edge[src][it]
    if len[dst] == -1 then
      len[dst] = len[src] + 1
      parent[dst] = src
      ccnt[src] = ccnt[src] + 1
      c2cnt[src] = c2cnt[src] + #edge[dst] - 1
      table.insert(tasks, dst)
    end
  end
end
local oddcnt = 0
for i = 1, n do
  if len[i] % 2 == 1 then
    oddcnt = oddcnt + 1
  end
end
local use = {0, 0, 0}
if oddcnt < lim[1] then
  for i = 1, n do
    if len[i] % 2 == 1 then
      use[3] = use[3] + 1
      num[i] = use[3] * 3
    end
  end
  for i = 1, n do
    if len[i] % 2 == 0 then
      if use[3] < lim[3] then
        use[3] = use[3] + 1
        num[i] = use[3] * 3
      elseif use[2] < lim[2] then
        use[2] = use[2] + 1
        num[i] = use[2] * 3 - 1
      else
        use[1] = use[1] + 1
        num[i] = use[1] * 3 - 2
      end
    end
  end
elseif (n - oddcnt) < lim[2] then
  for i = 1, n do
    if len[i] % 2 == 0 then
      use[3] = use[3] + 1
      num[i] = use[3] * 3
    end
  end
  for i = 1, n do
    if len[i] % 2 == 1 then
      if use[3] < lim[3] then
        use[3] = use[3] + 1
        num[i] = use[3] * 3
      elseif use[2] < lim[2] then
        use[2] = use[2] + 1
        num[i] = use[2] * 3 - 1
      else
        use[1] = use[1] + 1
        num[i] = use[1] * 3 - 2
      end
    end
  end
else
  for i = 1, n do
    if len[i] % 2 == 1 then
      if use[1] < lim[1] then
        use[1] = use[1] + 1
        num[i] = use[1] * 3 - 2
      else
        use[3] = use[3] + 1
        num[i] = use[3] * 3
      end
    else
      if use[2] < lim[2] then
        use[2] = use[2] + 1
        num[i] = use[2] * 3 - 1
      else
        use[3] = use[3] + 1
        num[i] = use[3] * 3
      end
    end
  end
end
print(table.concat(num, " "))
