n,q=gets.split.map &:to_i h=gets.split.map &:to_i y=n*q a=h[x=0] h.map{|b|x,y,a=[x+q,y+q,b>a ?x+b-a:x].min,[x+q,y+q,a>b ?y+a-b:y].min,b} p [x,y].min