#include <bits/stdc++.h>
using namespace std;
vector<pair<long long, long long>> segment, segment2, segment3;
long long n, m, k, t, need, ans = 1000000000000000000;
vector<pair<long long, long long>> vv[4];
vector<long long> v[3];
void add(long long curr) {
  long long val = curr - t + 1;
  while (curr) {
    segment[curr].first += 1;
    segment[curr].second += val;
    curr /= 2;
  }
}
void add2(long long curr) {
  long long val = curr - t + 1;
  while (curr) {
    segment2[curr].first += 1;
    segment2[curr].second += val;
    curr /= 2;
  }
}
void add3(long long curr) {
  long long val = curr - t + 1;
  while (curr) {
    segment3[curr].first += 1;
    segment3[curr].second += val;
    curr /= 2;
  }
}
void del(long long curr) {
  long long val = curr - t + 1;
  while (curr) {
    segment3[curr].first -= 1;
    segment3[curr].second -= val;
    curr /= 2;
  }
}
pair<long long, long long> getans1(long long cnt) {
  long long curr = 1, ret = 0;
  while (curr < t) {
    if (segment[2 * curr].first > cnt) {
      curr *= 2;
    } else {
      ret += segment[2 * curr].second;
      cnt -= segment[2 * curr].first;
      curr = 2 * curr + 1;
    }
  }
  ret += cnt * (curr - t + 1);
  return {ret, curr - t + 1};
}
long long getans2(long long a, long long b, long long curr, long long l,
                  long long r) {
  if (b < l || a > r) {
    return 0;
  }
  if (a <= l && b >= r) {
    if (segment2[curr].first <= need) {
      need -= segment2[curr].first;
      return segment2[curr].second;
    } else if (curr >= t) {
      long long ret = need * (curr - t + 1);
      need = 0;
      return ret;
    } else {
      if (segment2[2 * curr].first >= need) {
        return getans2(a, b, 2 * curr, l, (l + r) / 2);
      } else {
        need -= segment2[2 * curr].first;
        return segment2[2 * curr].second +
               getans2(a, b, 2 * curr + 1, (l + r) / 2 + 1, r);
      }
    }
  }
  long long mid = (l + r) / 2;
  return getans2(a, b, 2 * curr, l, mid) +
         getans2(a, b, 2 * curr + 1, mid + 1, r);
}
long long getans3(long long a, long long b, long long curr, long long l,
                  long long r) {
  if (b < l || a > r) {
    return 0;
  }
  if (a <= l && b >= r) {
    if (segment3[curr].first <= need) {
      need -= segment3[curr].first;
      return segment3[curr].second;
    } else if (curr >= t) {
      long long ret = need * (curr - t + 1);
      need = 0;
      return ret;
    } else {
      if (segment3[2 * curr].first >= need) {
        return getans3(a, b, 2 * curr, l, (l + r) / 2);
      } else {
        need -= segment3[2 * curr].first;
        return segment3[2 * curr].second +
               getans3(a, b, 2 * curr + 1, (l + r) / 2 + 1, r);
      }
    }
  }
  long long mid = (l + r) / 2;
  return getans3(a, b, 2 * curr, l, mid) +
         getans3(a, b, 2 * curr + 1, mid + 1, r);
}
long long gett(long long toadd) {
  long long ret = 0;
  pair<long long, long long> v = getans1(toadd);
  ret += v.first;
  need = m - toadd - k;
  ret += getans2(1, v.second, 1, 1, t);
  if (need) {
    ret += getans3(v.second + 1, t, 1, 1, t);
  }
  return ret;
}
int main() {
  ios_base::sync_with_stdio(false);
  cout.tie(NULL);
  cin.tie(NULL);
  cin >> n >> m >> k;
  t = pow(2, ceil(log2(10001)));
  segment.resize(2 * t, {0, 0});
  segment2.resize(2 * t, {0, 0});
  segment3.resize(2 * t, {0, 0});
  for (long long i = 0; i < n; i += 1) {
    long long a, b, c;
    cin >> a >> b >> c;
    if (b && c) {
      v[1].push_back(a);
      vv[1].push_back({a, i});
      add3(a + t - 1);
    } else if (b) {
      v[0].push_back(a);
      vv[0].push_back({a, i});
    } else if (c) {
      v[2].push_back(a);
      vv[2].push_back({a, i});
      add3(a + t - 1);
    } else {
      vv[3].push_back({a, i});
      add2(a + t - 1);
      add3(a + t - 1);
    }
  }
  for (long long i = 0; i < 3; i += 1) {
    sort(v[i].begin(), v[i].end());
    sort(vv[i].begin(), vv[i].end());
  }
  sort(vv[3].begin(), vv[3].end());
  long long p0 = -1, p1 = -1, f = 0, s = 0, sum = 0;
  for (long long i = 0; i < v[1].size(); i += 1) {
    f += 1;
    s += 1;
    p1 += 1;
    sum += v[1][i];
    if (f == k) {
      break;
    }
  }
  if (f != k) {
    for (long long i = 0; i < v[0].size(); i += 1) {
      f += 1;
      p0 += 1;
      sum += v[0][i];
      if (f == k) {
        break;
      }
    }
  }
  long long pos = p0 + 1;
  for (long long i = 0; i < v[2].size(); i += 1) {
    add(v[2][i] + t - 1);
  }
  for (long long i = p1 + 1; i < v[1].size(); i += 1) {
    add(v[1][i] + t - 1);
  }
  for (int i = p0 + 1; i < v[0].size(); i += 1) {
    add3(v[0][i]);
  }
  if (f != k || v[2].size() + s < k || 2 * k - s > m) {
    cout << -1 << endl;
    return 0;
  } else {
    ans = min(ans, sum + gett(k - s));
  }
  for (long long x = p0 + 1; x < min(k, (long long)v[0].size()); x += 1) {
    sum += v[0][x];
    sum -= v[1][p1];
    s -= 1;
    p1 -= 1;
    if (2 * k - s > m) {
      break;
    }
    add(v[1][p1 + 1] + t - 1);
    del(v[0][x]);
    long long val = sum + gett(k - s);
    if (val <= ans) {
      ans = val;
      pos = x + 1;
    }
  }
  cout << ans << endl;
  vector<long long> ret;
  for (long long i = 0; i < pos; i += 1) {
    ret.push_back(vv[0][i].second);
  }
  for (long long i = pos; i < k; i += 1) {
    ret.push_back(vv[1][i - pos].second);
  }
  long long g1 = pos, g2 = k - pos, g3 = 0, g4 = 0;
  for (long long i = 0; i < pos; i += 1) {
    if (g2 < v[1].size() && g3 < v[2].size()) {
      if (v[1][g2] < v[2][g3]) {
        ret.push_back(vv[1][g2].second);
        g2 += 1;
      } else {
        ret.push_back(vv[2][g3].second);
        g3 += 1;
      }
    } else if (g2 < v[1].size()) {
      ret.push_back(vv[1][g2].second);
      g2 += 1;
    } else {
      ret.push_back(vv[2][g3].second);
      g3 += 1;
    }
  }
  long long need = m - g1 - g2 - g3 - g4;
  for (long long i = 0; i < need; i += 1) {
    long long v1 = 100000, v2 = 100000, v3 = 100000, v4 = 100000;
    if (g1 < v[0].size()) {
      v1 = vv[0][g1].first;
    }
    if (g2 < v[1].size()) {
      v2 = vv[1][g2].first;
    }
    if (g3 < v[2].size()) {
      v3 = vv[2][g3].first;
    }
    if (g4 < vv[3].size()) {
      v4 = vv[3][g4].first;
    }
    if (v1 <= min({v2, v3, v4})) {
      ret.push_back(vv[0][g1].second);
      g1 += 1;
    } else if (v2 <= min({v1, v3, v4})) {
      ret.push_back(vv[1][g2].second);
      g2 += 1;
    } else if (v3 <= min({v1, v2, v4})) {
      ret.push_back(vv[2][g3].second);
      g3 += 1;
    } else {
      ret.push_back(vv[3][g4].second);
      g4 += 1;
    }
  }
  assert(ret.size() == m);
  sort(ret.begin(), ret.end());
  for (long long i : ret) {
    cout << i + 1 << " ";
  }
  cout << endl;
}
