
import java.io.*;
import java.util.*;

public class GFG {

  private static int i,j,k,l,n,m,books;
  public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
//    int t = Integer.parseInt(br.readLine().trim());
//    while (t-- != 0) {
//      int n = Integer.parseInt(br.readLine().trim());
//      char c[]=br.readLine().trim().toCharArray();
      StringTokenizer st = new StringTokenizer(br.readLine().trim());
      n = Integer.parseInt(st.nextToken());
      m = Integer.parseInt(st.nextToken());
      int kk = Integer.parseInt(st.nextToken());
      ArrayList<int[]> A[]=new ArrayList[4];
      for(i=0;i<4;i++)
        A[i]=new ArrayList<>();
      int ans=0;
      for(i=0;i<n;i++){
        st = new StringTokenizer(br.readLine().trim());
        int time=Integer.parseInt(st.nextToken());
        int al=Integer.parseInt(st.nextToken());
        int bo=Integer.parseInt(st.nextToken());
        int a[]=new int[]{time,i+1};
        if(al==1&&bo==0)
          A[0].add(a);
        else if(al==0&&bo==1)
          A[1].add(a);
        else if(al==1&&bo==1)
          A[2].add(a);
        else
          A[3].add(a);
      }
      if(A[0].size()+A[2].size()<kk||A[1].size()+A[2].size()<kk)
        out.println(-1);
      else{
        for(i=0;i<4;i++)
          Collections.sort(A[i],(b,c)->b[0]-c[0]);
//        out.println(A[0]);
//        out.println(A[1]);
//        out.println(A[2]);
//        out.println(A[3]);
        i=0;j=0;k=0;l=0;
        int cnt=0;
		books=0;
        while(i<A[0].size()&&j<A[1].size()&&k<A[2].size()&&cnt!=kk) {
          int sum = A[0].get(i)[0] + A[1].get(j)[0],c=A[2].get(k)[0];
          if (sum < c) {
            ans += sum;
            books+=2;
            i++;
            j++;
          }
          else {
            ans += c;
            books++;
            k++;
          }
          cnt++;
        }
        if(i==A[0].size()||j==A[1].size()){
          while(cnt!=kk){
            cnt++;
            books++;
            ans+=A[2].get(k++)[0];
          }
        }
        else if(k==A[2].size()){
          while(cnt!=kk){
            cnt++;
            books+=2;
            ans+=A[0].get(i++)[0] + A[1].get(j++)[0];
          }
        }
        while(books>m) {
          if (k == A[2].size() || i == 0 || j == 0) {
            ans = -1;
            break;
          }
          books --;
          ans = ans - A[0].get(--i)[0] - A[1].get(--j)[0] + A[2].get(k++)[0];
        }
        if(books<m)
          ans+=find(A);
	  if(n==19683&&m==507&&kk==254)
          out.println(books+" "+cnt);
        out.println(ans);
        if(ans!=-1){
          for(int y=0;y<i;y++)
            out.print(A[0].get(y)[1]+" ");
          for(int y=0;y<j;y++)
            out.print(A[1].get(y)[1]+" ");
          for(int y=0;y<k;y++)
            out.print(A[2].get(y)[1]+" ");
          for(int y=0;y<l;y++)
            out.print(A[3].get(y)[1]+" ");
          out.println();
        }
      }
    out.close();
  }

  private static int find(ArrayList<int[]>[] A) {
    int ans=0;
    while(books++!=m){
      int a=Integer.MAX_VALUE,b=Integer.MAX_VALUE,c=Integer.MAX_VALUE,d=Integer.MAX_VALUE;
      if(i<A[0].size()){
        a=A[0].get(i)[0];
      }
      if(j<A[1].size()){
        b=A[1].get(j)[0];
      }
      if(k<A[2].size()){
        c=A[2].get(k)[0];
      }
      if(l<A[3].size()){
        d=A[3].get(l)[0];
      }
      int min=Math.min(a,Math.min(b,Math.min(c,d)));
      if(min==a)
        i++;
      else if(min==b)
        j++;
      else if(min==c)
        k++;
      else
        l++;
      ans+=min;
    }
    return ans;
  }
}

