object Main extends App{
    // Your code here!
    import scala.io.Source.stdin
    def sort(nums: Array[Int]): (Array[Int],Int) = {
        var times = 0
        var notSorted = true
        while(notSorted) {
            notSorted = false
            for(i <- 0 until nums.length - 1) {
                (nums(i) > nums(i+1)) match {
                    case true => {
                        var tmp = nums(i)
                        nums(i) = nums(i+1)
                        nums(i+1) = tmp
                        notSorted = true
                        times += 1
                    }
                    case false => -1
                }
            }
        }
        (nums,times)
    }
    /*val input = {for(line <- io.Source.stdin.getLines()) yield {
        line
    }}.toSeq*/
    scala.io.StdIn.readLine
    val inputarr = scala.io.StdIn.readLine.split(" ").map{_.toInt}
    val sorted = sort(inputarr)
    println(sorted._1.mkString(" ") + "\n" + sorted._2)
}

