eval"N,*C="+`dd`.split*?,;p -(b=C.pop N).map{a=C.map{|v|v*~N}.sort;b.rotate!.map{|l|m=a.pop+l/2*~N-1;a[a.index{|v|v>m}||N-1,0]=m;m%~N}.min}.max