import java.io.BufferedReader
import java.io.InputStreamReader

fun bubbleSort(a: IntArray): Int {
    var cnt = 0
    var isSorted = false

    for (i in 0 until a.size) {
        if (isSorted) break
        isSorted = true
        for (j in a.size-1 downTo i+1) {
            if (a[j] < a[j-1]) {
                run { val t = a[j]; a[j] = a[j - 1]; a[j - 1] = t }
                isSorted = false
                cnt++
            }
        }
    }
    println(a.joinToString(separator = " "))
    return cnt
}

fun main(args: Array<String>) {
    val a = BufferedReader(InputStreamReader(System.`in`))
            .readText()
            .trim()
            .split(regex = """\s+""".toRegex())
            .map { it.toInt() }
            .drop(n = 1)
            .toIntArray()

    println(bubbleSort(a))
}
