b[9999];m,i;f="%d%d%d\n";c(int*x){i=*x-*1[&x];}main(a){for(;scanf(f,b,&m,&a)*m;printf(f+1,a)){for(i=m*=3;i;scanf(f,b+i,b-~i,b+i+2))i-=3;for(qsort(b,m/3,12,c);m;a=a-b[m+1]?a-b[m+2]?a:b[m+1]:b[m+2])m-=3;}}