import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;

public class B {
	static BufferedReader br;
	static PrintWriter pw;
	static int inf = (int) 1e9;
	static long[] memo;

	public static void main(String[] args) throws NumberFormatException, IOException, InterruptedException {
		br = new BufferedReader(new InputStreamReader(System.in));
		pw = new PrintWriter(System.out);

		StringTokenizer st = new StringTokenizer(br.readLine());
		int n = Integer.parseInt(st.nextToken());
		int m = Integer.parseInt(st.nextToken());
		int k = Integer.parseInt(st.nextToken());
		ArrayList<pair> arr = new ArrayList<>();
		ArrayList<pair> arr1 = new ArrayList<>();
		ArrayList<pair> arr2 = new ArrayList<>();
		ArrayList<triple> arr3 = new ArrayList<>();
		ArrayList<pair> arr4 = new ArrayList<>();
		for (int i = 0; i < n; i++) {
			st = new StringTokenizer(br.readLine());
			int t = Integer.parseInt(st.nextToken());
			int x = Integer.parseInt(st.nextToken());
			int y = Integer.parseInt(st.nextToken());
			if (x == 1 && y == 1)
				arr.add(new pair(t, i));
			else if (x == 1 && y == 0) {
				arr1.add(new pair(t, i));
				arr4.add(new pair(t, i));
			} else if (x == 0 && y == 1) {
				arr2.add(new pair(t, i));
				arr4.add(new pair(t, i));
			} else
				arr4.add(new pair(t, i));
		}
		Collections.sort(arr1);
		Collections.sort(arr2);

		int min = Math.min(arr1.size(), arr2.size());
		for (int i = 0; i < min; i++) {
			arr3.add(new triple(arr1.get(i).x + arr2.get(i).x, arr1.get(i).y, arr2.get(i).y));
		}
		Collections.sort(arr);
		Collections.sort(arr3);
		Collections.sort(arr4);
		if (arr.size() + arr3.size() < k || m < k)
			pw.println(-1);
		else {
			long sum = 0;
			int i = 0;
			int j = 0;
			int z = 0;
			ArrayList<Integer> ans = new ArrayList<>();
			HashSet<Integer> hs = new HashSet<>();
//			System.out.println(arr3);
//			System.out.println(arr4);
			while (m > 0) {

				pair p1 = new pair(1000000000, -1);
				pair p3 = new pair(1000000000, -1);
				triple p2 = new triple(1000000000, -1, -1);

				if (k > 0) {
					if (i == arr.size()) {
						p2 = arr3.get(j);
						j++;
						m -= 2;
						sum += p2.x;
						ans.add(p2.y + 1);
						ans.add(p2.z + 1);
						hs.add(p2.y + 1);
						hs.add(p2.z + 1);
						
					} else if (j == arr3.size() && m > 1) {
						p1 = arr.get(i);
						i++;
						m--;
						sum += p1.x;
						ans.add(p1.y + 1);
					} else {
						p1 = arr.get(i);
						p2 = arr3.get(j);
						if (p1.x <= p2.x || m == 1) {
							i++;
							m--;
							sum += p1.x;
							ans.add(p1.y + 1);
						} else {
							j++;
							m -= 2;
							sum += p2.x;
							ans.add(p2.y + 1);
							ans.add(p2.z + 1);
							hs.add(p2.y + 1);
							hs.add(p2.z + 1);
						}

					}
					k--;
				} else {
					if (i != arr.size())
						p1 = arr.get(i);

					if (j != arr3.size() && m > 1)
						p2 = arr3.get(j);

					if (z != arr4.size()) {
						p3 = arr4.get(z);
						while (z < arr4.size() && hs.contains(p3.y + 1)) {
							p3 = arr4.get(++z);
						}
						if (z == arr4.size())
							p3 = new pair(1000000000, -1);
					}
					min = Math.min(p1.x, Math.min(p2.x, p3.x));
//					System.out.println(i + " " + j + " " + z);
//					System.out.println(p1.x + " " + p2.x + " " + p3.x);
					if (min == p1.x) {
						p1 = arr.get(i);
						i++;
						m--;
						sum += p1.x;
						ans.add(p1.y + 1);
					}

					else if (min == p2.x && m > 1) {
						j++;
						m -= 2;
						sum += p2.x;
						ans.add(p2.y + 1);
						ans.add(p2.z + 1);
					} else {
						z++;
						m--;
						sum += p3.x;
						ans.add(p3.y + 1);
//						System.out.println(p3.y+1+" "+z);
					}
//					System.out.println(ans);
				}
			}
			pw.println(sum);
			for (int x : ans)
				pw.print(x + " ");
		}

		pw.flush();
	}

	static int[][] ok;

	static boolean delete(int min, ArrayList<Integer> rem, TreeSet<Integer>[] hs, int i, ArrayList<Character> ans) {
		ArrayList<Integer> del = new ArrayList<>();
		for (int k = 0; k < rem.size(); k++) {
			int x = rem.get(k);
			Integer idx2 = hs[x].higher(min);
			if (idx2 != null) {
				boolean f2 = true;
				for (int j = i + 1; j < 26; j++) {
					Integer temp = hs[j].lower(a.length);
					f2 &= temp == null || temp > idx2;
				}
				for (int j = k + 1; j < rem.size(); j++) {
					Integer temp = hs[rem.get(j)].lower(a.length);
					f2 &= temp == null || temp > idx2;
				}
				if (f2) {
					ans.add((char) ('a' + x));
					min = idx2;
					del.add(x);
				}
			}
		}
		rem.removeAll(del);
		return del.size() > 0;
	}

	static void dfs(int x, int y) {
		if (valid(x, y) && a[x][y] != '*' && !vis[x][y]) {
			vis[x][y] = true;
			ok[x][y] = 1;
			dfs(x - 1, y);
			dfs(x + 1, y);
			dfs(x, y - 1);
			dfs(x, y + 1);
		}
	}

	static long dp(int r) {
		if (r == 0)
			return 1;
		if (r < 0)
			return 0;
		if (memo[r] != -1)
			return memo[r];
		return memo[r] = dp(r - 1) + dp(r - 2) + dp(r - 3);
	}

	static char[][] a;
	static int n, m;

	static boolean valid(int x, int y) {
		return x > -1 && x < n && y > -1 && y < m;
	}

	static boolean[][] vis;

	static ArrayList<Edge> edgeList;
	static ArrayList<edge1>[] adj;
	static int V;

	static int kruskal() // O(E log E)
	{
		int mst = 0;
		Collections.sort(edgeList);
		UnionFind uf = new UnionFind(n);
		for (Edge e : edgeList)
			if (uf.union(e.u, e.v))
				mst += e.w;
		return mst;
	}

	static class Edge implements Comparable<Edge> {
		int u, v, w;

		Edge(int a, int b, int c) {
			u = a;
			v = b;
			w = c;
		}

		public int compareTo(Edge e) {
			return w - e.w;
		}
	}

	static class edge1 implements Comparable<edge1> {
		int v, w;

		edge1(int b, int c) {
			v = b;
			w = c;
		}

		public int compareTo(edge1 e) {
			return w - e.w;
		}
	}

	static class UnionFind {
		int[] p, rank;

		UnionFind(int N) {
			p = new int[N];
			rank = new int[N];
			for (int i = 0; i < N; i++)
				p[i] = i;
		}

		int findSet(int x) {
			return p[x] == x ? x : (p[x] = findSet(p[x]));
		}

		boolean union(int x, int y) {
			x = findSet(x);
			y = findSet(y);
			if (x == y)
				return false;

			if (rank[x] > rank[y])
				p[y] = x;
			else {
				p[x] = y;
				if (rank[x] == rank[y])
					++rank[y];
			}
			return true;
		}
	}

	static class pair implements Comparable<pair> {
		int x;
		int y;

		public pair(int d, int u) {
			x = d;
			y = u;
		}

		public int compareTo(pair o) {
			return x - o.x;
		}
		@Override
		public String toString() {
			// TODO Auto-generated method stub
			return x+" "+y;
		}

	}

	static class triple implements Comparable<triple> {
		int x;
		int y;
		int z;

		public triple(int a, int b, int c) {
			x = a;
			y = b;
			z = c;
		}

		public int compareTo(triple o) {
			return x - o.x;
		}
		public String toString() {
			// TODO Auto-generated method stub
			return x+" "+y+" "+z;
		}
	}

	static int[] nxtarr() throws IOException {
		StringTokenizer st = new StringTokenizer(br.readLine());
		int[] a = new int[st.countTokens()];
		for (int i = 0; i < a.length; i++) {
			a[i] = Integer.parseInt(st.nextToken());
		}
		return a;
	}

	static long pow(long a, long e) // O(log e)
	{
		long res = 1;
		while (e > 0) {
			if ((e & 1) == 1)
				res *= a;
			a *= a;
			e >>= 1;
		}
		return res;
	}

	static long gcd(long a, long b) {
		if (a == 0)
			return b;
		return gcd(b % a, a);
	}
}
