import java.io.*;
import java.util.*;
import java.util.Map.Entry;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

public class Main {

    static boolean check(int s, boolean isOdd) {
        int l = 0;
        boolean checking = isOdd;
        for (int i = 1; i <= n; i++) {
            if (!checking) {
                l++;
                checking = true;
            } else {
                if (a[i] <= s) {
                    l++;
                    checking = false;
                }
            }
        }
        return l >= k;
    }

    static int bs(int low, int high) {
        while(low < high) {
            int mid = (low + high) / 2;
            if (check(mid, true) || check(mid, false)) {
                high = mid;
            } else {
                low = mid + 1;
            }
        }
        return low;
    }

    static long t;
    static long n;
    static long m;
    static long k;
    static long x;
    static long y;
    static long[] a;
    static long[] a2;
    static long[] a3;
    static List<Long> aa;
    static List<Long> aa2;
    static List<Long> aa3;
    static Long[] A;
    static String s;
    static String s2;
    static long c;
    static long c2;
    static long ans;
    static String anss;
    static char[] cs;
    static long max;
    static long maxi;
    static Map<Long, Long> mapll;
    static long mod;

    public static void main(String[] args) {
        MyScanner sc = new MyScanner();
        out = new PrintWriter(new BufferedOutputStream(System.out));

        // Start writing your solution here. -------------------------------------
        n = sc.nextInt();
        m = sc.nextInt();
        k = sc.nextInt();
        List<Pair<Long, Integer>> aa = new ArrayList<>();
        List<Pair<Long, Integer>> aa2 = new ArrayList<>();
        List<Pair<Long, Integer>> aa3 = new ArrayList<>();
        List<Pair<Long, Integer>> aa4 = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            t = sc.nextInt();
            x = sc.nextInt();
            y = sc.nextInt();
            if (x == 1 && y == 1) {
                aa.add(new Pair(t, i + 1));
            } else if (x == 1) {
                aa2.add(new Pair(t, i + 1));
            } else if (y == 1) {
                aa3.add(new Pair(t, i + 1));
            } else {
                aa4.add(new Pair(t, i + 1));
            }
        }
        aa.sort(Comparator.comparing(o -> o.l));
        aa2.sort(Comparator.comparing(o -> o.l));
        aa3.sort(Comparator.comparing(o -> o.l));

        ans = 0;
        List<Integer> ansi = new ArrayList<>();
        c = 0;
        if ((aa.size() + aa2.size()) < k || (aa.size() + aa3.size()) < k) {
            ans = -1;
        } else {
            int i, i2, i3;
            for (i = 0; i < k && i < aa.size(); i++) {
                ans += aa.get(i).l;
                ansi.add(aa.get(i).r);
                c++;
            }
            for (i2 = 0; i2 < (k - i); i2++) {
                ans += aa2.get(i2).l;
                ansi.add(aa2.get(i2).r);
                c++;
            }
            for (i3 = 0; i3 < (k - i); i3++) {
                ans += aa3.get(i3).l;
                ansi.add(aa3.get(i3).r);
                c++;
            }
            i--;
            while (c < m && i >= 0 && i2 < aa2.size() && i3 < aa3.size()) {
                if (aa.get(i).l > (aa2.get(i2).l + aa3.get(i3).l)) {
                    ans -= aa.get(i).l;
                    ansi.remove(aa.get(i).r);
                    ans += aa2.get(i2).l + aa3.get(i3).l;
                    ansi.add(aa2.get(i2).r);
                    ansi.add(aa3.get(i3).r);
                    i--;
                    i2++;
                    i3++;
                    c++;
                } else {
                    break;
                }
            }
            i++;
            if (i < aa.size()) {
                aa = aa.subList(i, aa.size());
            } else {
                aa = new ArrayList<>();
            }
            if (i2 < aa2.size()) {
                aa.addAll(aa2.subList(i2, aa2.size()));
            }
            if (i3 < aa3.size()) {
                aa.addAll(aa3.subList(i3, aa3.size()));
            }
            aa.addAll(aa4);
            aa.sort(Comparator.comparing(o -> o.l));
            i = 0;
            while (c < m) {
                ans += aa.get(i).l;
                ansi.add(aa.get(i).r);
                c++;
            }
            if (c != m) {
                ans = -1;
            }
        }
        out.println(ans);
        if (ans != -1) {
            for (int i = 0; i < ansi.size(); i++) {
                out.print(ansi.get(i));
                out.print(" ");
            }
        }

        // Stop writing your solution here. -------------------------------------
        out.close();
    }

    static int countMatches(String s, char c) {
        return s.length() - s.replaceAll(String.valueOf(c), "").length();
    }

    public static class Pair<L,R> {
        private L l;
        private R r;
        public Pair(L l, R r){
            this.l = l;
            this.r = r;
        }
        public L getL(){ return l; }
        public R getR(){ return r; }
        public void setL(L l){ this.l = l; }
        public void setR(R r){ this.r = r; }
    }

    //-----------PrintWriter for faster output---------------------------------
    public static PrintWriter out;

    //-----------MyScanner class for faster input----------
    public static class MyScanner {

        BufferedReader br;
        StringTokenizer st;

        public MyScanner() {
            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;
        }

    }
    //--------------------------------------------------------
}