n,b=gets.not_nil!.split.map &.to_i a=(1..n).map{gets.not_nil!.split.map &.to_i} x=a.map{|c|c[0]}.sort.uniq a.map{|c|c[0]=x.index(c[0]).not_nil!} z=0 n.times{|i|n.times{|j|s=t=l=0 f=[0]*n g=[0]*n a.map{|c|a[i][1]>c[1]||c[1]>a[j][1]||(f[d=c[0]]+=1;g[d]+=c[2])} n.times{|k|s+=f[k];t+=g[k];while t>b;s-=f[l];t-=g[l];l+=1 end;z=s if z