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

public class Reading_Books {

	static class FastReader {
		BufferedReader br;
		StringTokenizer st;

		public FastReader() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		String next() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

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

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

		double nextDouble() {
			return Double.parseDouble(next());
		}

		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}

	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		FastReader t = new FastReader();
		PrintWriter o = new PrintWriter(System.out);
		int n = t.nextInt();
		int m = t.nextInt();
		int k = t.nextInt();
		List<Pair> both = new ArrayList<>();
		List<Pair> alice = new ArrayList<>();
		List<Pair> bob = new ArrayList<>();
		List<Pair> none = new ArrayList<>();
		Set<Integer> ans = new HashSet<>();

		for (int i = 0; i < n; ++i) {
			int val = t.nextInt();
			int x = t.nextInt();
			int y = t.nextInt();

			if (x == 1 && y == 1)
				both.add(new Pair(i + 1, val));
			else if (x == 1)
				alice.add(new Pair(i + 1, val));
			else if (y == 1)
				bob.add(new Pair(i + 1, val));
			else {
				none.add(new Pair(i + 1, val));
			}
		}

		Collections.sort(both, (Pair x, Pair y) -> x.val - y.val);
		Collections.sort(alice, (Pair x, Pair y) -> x.val - y.val);
		Collections.sort(bob, (Pair x, Pair y) -> x.val - y.val);
		Collections.sort(none, (Pair x, Pair y) -> x.val - y.val);

		if (Math.min(alice.size(), bob.size()) + both.size() >= k) {
			int a = 0, b = 0, c = 0, d = 0;
			int cur = 0;
			long time = 0;
			long p = 0;
			boolean flag = true;

			while (cur < k && a < both.size() && b < alice.size() && c < bob.size()) {
				if (bob.get(c).val + alice.get(b).val <= both.get(a).val) {
					time = time + bob.get(c).val + alice.get(b).val;

					if (p + 2 <= m) {
						cur++;
						p += 2;

						c++;
						b++;

						ans.add(bob.get(c - 1).idx);
						ans.add(alice.get(b - 1).idx);
					} else {
						if (b == -1 || c == -1 || a == both.size()) {
							flag = false;
							break;
						}

						time = time - (bob.get(c).val + alice.get(b).val);
						time = time + both.get(a).val;

						ans.add(both.get(a).idx);

						cur++;
						p++;
						a++;
					}

				} else {
					time = time + both.get(a).val;

					if (p + 1 <= m) {
						a++;
						cur++;
						p++;

						ans.add(both.get(a - 1).idx);
					} else {
						if (b == -1 || c == -1 || a >= both.size() - 1) {
							flag = false;
							break;
						}

						time = time - (bob.get(c - 1).val + alice.get(b - 1).val);
						time = time + both.get(a + 1).val;

						ans.remove(bob.get(c - 1).idx);
						ans.remove(alice.get(c - 1).idx);
						ans.add(both.get(a).idx);
						ans.add(both.get(a + 1).idx);

						a += 2;
						cur++;
						b--;
						c--;
					}
				}
			}

			while (cur < k && a < both.size()) {
				time = time + both.get(a).val;

				if (p + 1 <= m) {
					a++;
					cur++;
					p++;

					ans.add(both.get(a - 1).idx);
				} else {
					if (b == -1 || c == -1 || a >= both.size() - 1) {
						flag = false;
						break;
					}

					time = time - (bob.get(c - 1).val + alice.get(b - 1).val);
					time = time + both.get(a + 1).val;

					ans.remove(bob.get(c - 1).idx);
					ans.remove(alice.get(c - 1).idx);
					ans.add(both.get(a).idx);
					ans.add(both.get(a + 1).idx);

					a += 2;
					cur++;
					b--;
					c--;
				}
			}

			while (cur < k && b < alice.size() && c < bob.size()) {
				time = time + bob.get(c).val + alice.get(b).val;

				if (p + 2 <= m) {
					cur++;
					p += 2;

					c++;
					b++;

					ans.add(bob.get(c - 1).idx);
					ans.add(alice.get(b - 1).idx);
				} else {
					if (b == -1 || c == -1 || a == both.size()) {
						flag = false;
						break;
					}

					time = time - (bob.get(c).val + alice.get(b).val);
					time = time + both.get(a).val;

					ans.add(both.get(a).idx);

					cur++;
					p++;
					a++;
				}
			}

			while (p < m) {
				int v1 = a < both.size() ? both.get(a).val : 10001;
				int v2 = b < alice.size() ? alice.get(b).val : 10001;
				int v3 = c < bob.size() ? bob.get(c).val : 10001;
				int v4 = d < none.size() ? none.get(d).val : 10001;

				if (v1 == v2 && v2 == v3 && v3 == v4 && v4 == 10001) {
					break;
				}

				if (v1 <= v2 && v1 <= v3 && v1 <= v4) {
					time = time + v1;

					ans.add(both.get(a).idx);

					p++;
					a++;
				} else if (v2 <= v1 && v2 <= v3 && v2 <= v4) {
					time = time + v2;

					ans.add(alice.get(b).idx);

					p++;
					b++;
				} else if (v3 <= v2 && v3 <= v1 && v3 <= v4) {
					time = time + v3;

					ans.add(bob.get(c).idx);

					p++;
					c++;
				} else {
					time = time + v4;

					ans.add(none.get(d).idx);

					p++;
					d++;
				}
			}

			if (p == m && flag) {
				o.println(time);

				for (int idx : ans) {
					o.print(idx + " ");
				}
			} else {
				o.println("-1");
			}

		} else
			o.println("-1");

		o.flush();
		o.close();
	}

	private static class Pair {
		int idx;
		int val;

		public Pair(int idx, int val) {
			this.idx = idx;
			this.val = val;
		}
	}
}
