object Main extends App {
  import scala.io.StdIn._

  val move = Array.tabulate(10){_ ⇒ Array.tabulate(10){_ ⇒ 0}}
  val n = readInt
  val numbers = readLine.trim.toCharArray.map(_.asDigit)
  for (i ← numbers.indices.tail){
    move(numbers(i - 1))(numbers(i)) += 1
  }
  val r = calPermutationMin()
  println(r / 1000000)
  println((r / 1000) % 1000)
  println(r % 1000)

  def calPermutationMin(): Long= {
    def sub(prev: Array[Int], rest: Int = 9, flag: Long = 0, cost: Int = 0, min: Long = 0, minCost: Int = Int.MaxValue): (Long, Int)= {
      if (rest == 0) {
        if (minCost > cost) (prev.foldRight(0L)((a, b) ⇒ a + b * 10), cost)
        else (min, minCost)
      }else {
        var m = min
        var mc = minCost
        for (i ← 1 to 9 if (flag & (1 << i)) == 0){
          prev(rest - 1) = i
          val c = (rest until 9).map(idx ⇒ diff(idx, rest - 1) * (move(i)(prev(idx)) + move(prev(idx))(i))).sum
          val (nm, nmc) = sub(prev, rest - 1, flag | (1 << i), cost + c, m, mc)
          m = nm
          mc = nmc
        }
        (m, mc)
      }
    }
    sub(Array.tabulate(9){_ ⇒ 0})._1
  }
  def diff(a: Int, b: Int): Int = {
    math.abs((a / 3) - (b / 3)) + math.abs((a % 3) - (b % 3))
  }
  implicit class Scope[T](val value: T) extends AnyVal {
    @inline
    def let[A](func: T ⇒ A): A = func(value)
  }
}
