import java.io._
import java.util.StringTokenizer
import scala.util.control.Breaks.{breakable,break}
import scala.collection.mutable.{Map, Stack, Queue, PriorityQueue, Set}

object Main extends App {
  val in = new InputReader(System.in)
  val pw = new PrintWriter(System.out)

  val N = in.next().toInt
  val graph = Array.fill[List[Int]](N+1)(Nil)
  for (i <- 1 until N) {
    val a, b = in.next().toInt
    graph(b) = a :: graph(b)
    graph(a) = b :: graph(a)
  }

  //
  val q = Queue[(Int, Int)]() // (node, color)
  val colors = Array.fill[Int](N+1)(-1)

  q.enqueue((1, 0))
  while (!q.isEmpty) {
    val (node, color) = q.dequeue()
    if (colors(node) == -1) {
      colors(node) = color
      for (e <- graph(node)) {
        q.enqueue((e, (color + 1) % 2))
      }
    }
  }


  var c1 = 0
  var c2 = 1
  var g1 = colors.count(_ == 0)
  var g2 = colors.count(_ == 1)

  val piyo = N / 3
  val hoge = N % 3
  val mods = Array(piyo, piyo + Math.min(1, hoge), piyo + Math.max(0, hoge-1))

  if (g1 > g2) {
    c1 = 1; c2 = 0

    val tmp = g1
    g1 = g2
    g2 = tmp
  }

  val ans = new Array[Int](N+1)
  val counter = new Array[Int](3)
  if (N/3 >= g1) {
    for (i <- 1 to N) {
      val now = colors(i)
      if (now == c1) {
        ans(i) = counter(0) * 3 + 3
        counter(0) += 1
      }
      else {
        if (counter(1) < mods(1)) {
          val m = 1
          ans(i) = counter(m) * 3 + m
          counter(m) += 1
        }
        else if (counter(2) < mods(2)) {
          val m = 2
          ans(i) = counter(m) * 3 + m
          counter(m) += 1
        }
        else {
          ans(i) = counter(0) * 3 + 3
          counter(0) += 1
        }
      }
    }

    println(ans.drop(1).mkString(" "))
  }
  else if (mods(1) <= g1 && mods(2) <= g2) {
    for (i <- 1 to N) {
      val now = colors(i)
      if (now == c1) {
        val m = 1
        if (counter(m) < mods(m)){
          ans(i) = counter(m) * 3 + m
          counter(m) += 1
        }
        else {
          ans(i) = counter(0) * 3 + 3
          counter(0) += 1
        }
      }
      else { // now == c2
        val m = 2
        if (counter(m) < mods(m)){
          ans(i) = counter(m) * 3 + m
          counter(m) += 1
        }
        else {
          ans(i) = counter(0) * 3 + 3
          counter(0) += 1
        }
      }
    }


    println(ans.drop(1).mkString(" "))
  }
  else {
    println(-1)
  }
}

object Sub {
  def print2DimArray[A](mat: Array[Array[A]]): Unit = {
    mat.foreach(lin => println(lin.deep))
  }
}

class InputReader(stream: InputStream) {
  private[this] val reader = new BufferedReader(new InputStreamReader(stream))
  private[this] var tokenizer: StringTokenizer = new StringTokenizer(reader.readLine())
  def next(): String = {
    while (!tokenizer.hasMoreTokens()) {
      tokenizer = new StringTokenizer(reader.readLine())
    }
    tokenizer.nextToken()
  }
}
