use std::io::stdin; const INF: usize = 1000000000; fn main() { let mut input = stdin().lines().map(|s| s.unwrap()); let s = input.next().unwrap(); let mut s = s.split_whitespace(); let n: usize = s.next().unwrap().parse().unwrap(); let m: usize = s.next().unwrap().parse().unwrap(); let mut x = Vec::with_capacity(n); let mut a = Vec::with_capacity(n); let mut b = Vec::with_capacity(n); for line in input.take(n) { let mut line = line.split_whitespace(); let xi: usize = line.next().unwrap().parse().unwrap(); let ai: usize = line.next().unwrap().parse().unwrap(); let bi: usize = line.next().unwrap().parse().unwrap(); x.push(xi); a.push(ai); b.push(bi); } let mut x = x; let a = a; let b = b; let mut by_a: Vec<_> = (0..n).collect(); by_a.sort_by_key(|&i| a[i]); let by_a = by_a; let mut by_b: Vec<_> = (0..n).collect(); by_b.sort_by_key(|&i| b[i]); let by_b = by_b; let mut res = INF; let mut two = 0; let mut three = 0; for xi in &mut x { // のちのループが sa = 0, sb = max から始まるので、全員がちょうど xi + 1 個の問題を解ける *xi += 1; if *xi >= 2 { two += 1; } if *xi >= 3 { three += 1; } } let mut by_a_i = 0; let mut by_b_i = 0; let mut sb = 100001; for sa in 0..100002 { while by_a_i < n && a[by_a[by_a_i]] < sa { let i = by_a[by_a_i]; x[i] -= 1; if x[i] == 1 { two -= 1; } if x[i] == 2 { three -= 1; } by_a_i += 1; } while two < m && sb > 0 { sb -= 1; while by_b_i < n && b[by_b[n - 1 - by_b_i]] >= sb { let i = by_b[n - 1 - by_b_i]; x[i] += 1; if x[i] == 2 { two += 1; } if x[i] == 3 { three += 1; } by_b_i += 1; } } // println!("sa: {}, sb: {}, t: {}", sa, sb, three); if two >= m { res = res.min(three); } } println!("{}", res); }