import java.io.*;
import java.util.*;
 
public class e1 {
	public static PrintWriter out;
	public static InputReader in;
	public static void makePre(Pair arr[], int ln)
	{
		int s = 0;
		for(int i=0;i<ln;i++)
		{
			s+=arr[i].a;
			arr[i].a=s;
		}
	}
	static class Pair{
		public int a,b;
		public Pair(int a, int b){
			this.a=a;
			this.b=b;
		}
	}
	public static void main(String[] args)throws IOException {
        in = new InputReader(System.in);
        out = new PrintWriter(System.out);
        
		int n = in.nextInt();
		int m = in.nextInt();
		int k = in.nextInt();
		Pair bob[] = new Pair[n];
		Pair alice[] = new Pair[n];
		Pair com[] = new Pair[n];
		ArrayList<Pair> other = new ArrayList<Pair>();
		Arrays.fill(bob,new Pair(-1,-1)); Arrays.fill(alice,new Pair(-1,-1));
		Arrays.fill(com,new Pair(-1,-1));
		int b=0,a=0,c=0;
		for(int t = 0; t < n; t++){
			int ti = in.nextInt();
			int ai = in.nextInt();
			int bi = in.nextInt();
			if(ai==1 && bi==1)
			{
				com[c++]=new Pair(ti,t);
			}
			else if(ai==1 && bi==0)
			{
				alice[a++]=new Pair(ti,t);
			}
			else if(ai==0 && bi==1)
			{
				bob[b++]=new Pair(ti,t);
			}
			else
			{
				other.add(new Pair(ti,t));
			}
		}
		mergesort(alice,0,a-1);
		mergesort(bob,0,b-1);
		mergesort(com,0,c-1);
		makePre(com,c); makePre(bob,b); makePre(alice,a);
		int ans = Integer.MAX_VALUE;
		int aopt = -1, bopt=-1, copt=-1;
		for(int i=0;i<=k;i++)
		{
			if(i>0 && com[i-1].a==-1)
				continue;
			int sm=0;
			if(i>0)
				sm = com[i-1].a;
			if(i==k){
				if(sm<ans)
				{
					aopt = 0; bopt=0; copt=i;
				}
				ans = Math.min(ans,sm);
			}
			else
			{
				if(k-i>b)
					continue;
				sm+=bob[k-i-1].a;
				if(k-i>a)
					continue;
				sm+=alice[k-i-1].a;
			}
			if((k-i+k-i+i)>m)
				continue;
			if(sm<ans)
			{
				aopt = k-i; bopt=k-i; copt=i;
			}
			ans = Math.min(ans,sm);
		}
		if(ans==Integer.MAX_VALUE)
			out.println(-1);
		else{
			for(int i=aopt;i<n;i++)
			{
				if(alice[i].a<0)
					break;
				if(i==0)
					other.add(alice[i]);
				else
					other.add(new Pair(alice[i].a-alice[i-1].a,alice[i].b));
			}
			for(int i=bopt;i<n;i++)
			{
				if(bob[i].a<0)
					break;
				if(i==0)
					other.add(bob[i]);
				else
					other.add(new Pair(bob[i].a-bob[i-1].a,bob[i].b));
			}
			for(int i=copt;i<n;i++)
			{
				if(com[i].a<0)
					break;
				if(i==0)
					other.add(com[i]);
				else
					other.add(new Pair(com[i].a-com[i-1].a,com[i].b));
			}
			// out.println("Copt "+copt+" bopt "+bopt+" aopt "+aopt);
			Collections.sort(other,(p1,p2) -> p1.a-p2.a);
			// out.println(other);
			for(int i=0;i<m-(copt+bopt+aopt);i++)
			{
				ans+=other.get(i).a;
			}
			out.println(ans);
			for(int i=0;i<copt;i++){
				out.print((com[i].b+1)+" ");
			}
			for(int i=0;i<bopt;i++){
				out.print((alice[i].b+1)+" ");
				out.print((bob[i].b+1)+" ");
			}
			for(int i=0;i<m-(copt+bopt+aopt);i++){
				out.print((other.get(i).b+1)+" ");
			}
			out.println();
		}
        out.close();
	}
	public static void merge(Pair[] arr, int first,int mid,int last){

		Pair a[]=new Pair[mid-first+1];

		Pair b[]=new Pair[last-mid];

		// int c[]=new int[mid-first+1];

		// int d[]=new int[last-mid];

		int pahela,dusra;	
		pahela=dusra=0;

		for(pahela=0;pahela<mid-first+1;pahela++)
		{
			a[pahela]=arr[first+pahela];
			// c[pahela]=indexes[first+pahela];
		} 

		for(dusra=0;dusra<last-mid;dusra++)
		{
			b[dusra]=arr[mid+1+dusra];
			// d[dusra]=indexes[mid+1+dusra];
		}

		pahela=0;
		dusra=0;
		int maha=first;

		while(pahela<mid-first+1 && dusra<last-mid)
		{
			if(a[pahela].a<=b[dusra].a)
			{
				arr[maha]=a[pahela];
				// indexes[maha]=c[pahela];
				maha++;
				pahela++;
			}

			else
			{
				arr[maha]=b[dusra];
				// indexes[maha]=d[dusra];
				maha++;
				dusra++;
			}
		}

		while(pahela<mid-first+1)
		{
			arr[maha]=a[pahela];
			// indexes[maha]=c[pahela];
			maha++;
			pahela++;
		}

		while(dusra<last-mid){
			arr[maha]=b[dusra];
			// indexes[maha]=d[dusra];
			maha++;
			dusra++;
		}
	}
	public static void mergesort(Pair arr[],int first,int last)
	{
		if(first < last) {
		int mid=(first+last)/2;
		mergesort(arr,first,mid);
		mergesort(arr,mid+1,last);
		merge(arr,first,mid,last);
	}
	}
	
    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

    }

}