n,k=gets.split.map(&:to_i) aa=gets.split.map(&:to_i) @cnt=0 def solve_sort!(arr,left,right,hop) return if left >= right i=right j=right-hop while (left<=j) if arr[j]>arr[i] arr[i],arr[j]=arr[j],arr[i] @cnt+=1 end i-=hop j-=hop end solve_sort!(arr,left+hop,right,hop) end def sorted?(aa) aa.inject{|s,e|!s&&s>e ? false : e} end k.times{|i| solve_sort!(aa,0,aa.size-1-i,k) } p sorted?(aa) ? @cnt : -1