import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class Greedybooks {
	public static void main(String[] args) throws IOException {
		BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
		String s1 = b.readLine();
		String[] a = s1.split(" ");
		int n = Integer.parseInt(a[0]);
		int m = Integer.parseInt(a[1]);
		int k = Integer.parseInt(a[2]);
		ArrayList<Book> bk = new ArrayList<Book>();
		ArrayList<Book> both = new ArrayList<Book>();
		ArrayList<Book> ai = new ArrayList<Book>();
		ArrayList<Book> bi = new ArrayList<Book>();
		ArrayList<Book> neither = new ArrayList<Book>();
		for (int i = 0; i < n; i++) {
			String s = b.readLine();
			String[] a1 = s.split(" ");
			Book bok = new Book(Integer.parseInt(a1[0]), Integer.parseInt(a1[1]), Integer.parseInt(a1[2]));
			bk.add(bok);
			int y = Integer.parseInt(a1[1]);
			int z = Integer.parseInt(a1[2]);
			if (y == 1 && z == 1) {
				both.add(bok);
			}
			if (y == 0 && z == 0) {
				neither.add(bok);
			}
			if (y == 1 && z == 0) {
				ai.add(bok);
			}
			if (y == 0 && z == 1) {
				bi.add(bok);
			}
		}
		boolean bar = false;
		ArrayList<Book> bky = (ArrayList<Book>) bk.clone();
		Collections.sort(bk, new Sorter());
		Collections.sort(both, new Sorter());
		Collections.sort(bi, new Sorter());
		Collections.sort(ai, new Sorter());
		Collections.sort(neither, new Sorter());
		ArrayList<Book> chosen = new ArrayList<Book>();
		int time = 0;
		int x = 0;
		boolean bal = false;
		while (x < k) {
			if (both.isEmpty() && !ai.isEmpty() && !bi.isEmpty() && chosen.size() + 1 < m) {
				chosen.add(ai.get(0));
				chosen.add(bi.get(0));
				time = time + ai.get(0).getT() + bi.get(0).getT();
				ai.remove(0);
				bi.remove(0);
				x++;
				bal = true;
			}
			if (!both.isEmpty() && (ai.isEmpty() || bi.isEmpty()) && chosen.size() < m) {
				chosen.add(both.get(0));
				time = time + both.get(0).getT();
				both.remove(0);
				x++;
				bal = true;
			}
			if (!both.isEmpty() && !ai.isEmpty() && !bi.isEmpty()) {
				if (both.get(0).getT() >= ai.get(0).getT() + bi.get(0).getT() && chosen.size() + 1 < m) {
					chosen.add(ai.get(0));
					chosen.add(bi.get(0));
					time = time + ai.get(0).getT() + bi.get(0).getT();
					ai.remove(0);
					bi.remove(0);
					x++;
					bal = true;
				} else {
					if (chosen.size() < m) {
						chosen.add(both.get(0));
						time = time + both.get(0).getT();
						both.remove(0);
						x++;
						bal = true;
					}
				}
			}
			if (!bal) {
				break;
			}
			else {
				bal = false;
			}
		}
		if (x < k) {
			System.out.println(-1);
		} else {
			while (chosen.size() < m) {
				chosen.add(bk.get(0));
				time = time + bk.get(0).getT();
				bk.remove(0);
			}
			if (chosen.size() < m) {
				System.out.println(-1);
			} else {
				System.out.println(time);
				for (int i = 0; i < chosen.size(); i++) {
					System.out.print(bky.indexOf(chosen.get(i)) + 1 + " ");
				}
			}
		}
	}
}

class Book {
	int t;
	int a;
	int b;

	public Book(int t, int a, int b) {
		this.t = t;
		this.a = a;
		this.b = b;
	}

	public int getT() {
		return this.t;
	}
}

class Sorter implements Comparator<Book> {
	@Override
	public int compare(Book o1, Book o2) {
		return o1.getT() - o2.getT();
	}
}
