import java.util.*; import java.io.*; public class Main { static int[] dp; public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] first = br.readLine().split(" ", 2); int n = Integer.parseInt(first[0]); int m = Integer.parseInt(first[1]); PriorityQueue aQueue = new PriorityQueue(new Comparator() { public int compare(Person p1, Person p2) { return p1.a - p2.a; } }); PriorityQueue bQueue = new PriorityQueue(new Comparator() { public int compare(Person p1, Person p2) { return p2.b - p1.b; } }); int comp3 = 0; int comp2 = 0; for (int i = 0; i < n; i++) { Person p = new Person(br.readLine()); if (p.count >= 3) { comp3++; comp2++; continue; } else if (p.count >= 2) { comp3++; comp2++; } else if (p.count >= 1) { comp2++; } aQueue.add(p); bQueue.add(p); } int min = comp3; while (true) { if (comp2 < m) { if (bQueue.size() == 0) { break; } int tmp = bQueue.peek().b; while (bQueue.size() > 0 && tmp == bQueue.peek().b) { Person p = bQueue.poll(); p.count++; if (p.count == 2) { comp3++; } else if (p.count == 1) { comp2++; } } } else { min = Math.min(min, comp3); if (aQueue.size() == 0) { break; } int tmp = aQueue.peek().a; while (aQueue.size() > 0 && tmp == aQueue.peek().a) { Person p = aQueue.poll(); p.count--; if (p.count == 1) { comp3--; } else if (p.count == 0) { comp2--; } } } } System.out.println(min); } static class Person { int a; int b; int count; public Person(int count, int a, int b) { this.a = a; this.b = b; this.count = count; } public Person(String a, String b, String c) { this(Integer.parseInt(a), Integer.parseInt(b), Integer.parseInt(c)); } public Person(String[] arr) { this(arr[0], arr[1], arr[2]); } public Person(String s) { this(s.split(" ", 3)); } } }