Problem:
Score : 400 points

Problem Statement

Joisino is planning to record N TV programs with recorders.

The TV can receive C channels numbered 1 through C.

The i-th program that she wants to record will be broadcast from time s_i to time t_i (including time s_i but not t_i) on Channel c_i.

Here, there will never be more than one program that are broadcast on the same channel at the same time.

When the recorder is recording a channel from time S to time T (including time S but not T), it cannot record other channels from time S-0.5 to time T (including time S-0.5 but not T).

Find the minimum number of recorders required to record the channels so that all the N programs are completely recorded.

Constraints

1≤N≤10^5

1≤C≤30

1≤s_i<t_i≤10^5

1≤c_i≤C

If c_i=c_j and i≠j, either t_i≤s_j or s_i≥t_j.

All input values are integers.

Input

Input is given from Standard Input in the following format:

N C
s_1 t_1 c_1
:
s_N t_N c_N

Output

When the minimum required number of recorders is x, print the value of x.

Sample Input 1

3 2
1 7 2
7 8 1
8 12 1

Sample Output 1

2

Two recorders can record all the programs, for example, as follows:

With the first recorder, record Channel 2 from time 1 to time 7. The first program will be recorded. Note that this recorder will be unable to record other channels from time 0.5 to time 7.

With the second recorder, record Channel 1 from time 7 to time 12. The second and third programs will be recorded. Note that this recorder will be unable to record other channels from time 6.5 to time 12.

Sample Input 2

3 4
1 3 2
3 4 4
1 4 3

Sample Output 2

3

There may be a channel where there is no program to record.

Sample Input 3

9 4
56 60 4
33 37 2
89 90 3
32 43 1
67 68 3
49 51 3
31 32 3
70 71 1
11 12 3

Sample Output 3

2

Solution in C# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    public static IEnumerable<int> Range(int start, int count) => Enumerable.Range(start, count);

    static int N, C;

    public class StartEnd
    {
        public StartEnd(int start, int end)
        {
            this.Start = start;
            this.End = end;
        }

        public override string ToString()
        {
            return $"{Start}-{End}";
        }

        public int Start { get; set; }
        public int End { get; set; }
    }

    public static void Main(string[] args)
    {
        N = Input.NextInt();
        C = Input.NextInt();
        var seByChannels = new Dictionary<int, List<StartEnd>>();
        foreach (var _ in Range(0, N))
        {
            var vals = Input.LineInt();
            StartEnd toAdd = new StartEnd(vals[0], vals[1]);
            var channel = vals[2];
            if (!seByChannels.ContainsKey(channel))
            {
                seByChannels[channel] = new List<StartEnd>();
            }
            var toRemoveIndex = new List<int>();
            var ses = seByChannels[channel];
            foreach (var index in Range(0, ses.Count))
            {
                var se = ses[index];
                if (toAdd.End < se.Start)
                {
                }
                else if (toAdd.End == se.Start)
                {
                    toRemoveIndex.Add(index);
                    toAdd.End = se.End;
                }
                else
                {
                    // toAdd.End > se.Start
                    if (toAdd.Start > se.End)
                    {
                    }
                    else
                    {
                        // toAdd.Start <= se.End
                        toRemoveIndex.Add(index);
                        toAdd.Start = Math.Min(toAdd.Start, se.Start);
                    }
                }
            }
            toRemoveIndex.Reverse();
            foreach (var index in toRemoveIndex)
            {
                ses.RemoveAt(index);
            }
            ses.Add(toAdd);
        }

        var recorders = new List<List<StartEnd>>();
        foreach (var se in seByChannels.SelectMany(n => n.Value))
        {
            foreach (var recorder in recorders)
            {
                foreach (var bangumi in recorder)
                {
                    if ((se.End < bangumi.Start)
                     || (se.Start > bangumi.End))
                    {
                        // OK
                    }
                    else
                    {
                        goto L_GoNextRecorder;
                    }
                }
                // we can record this recorder.
                recorder.Add(se);
                goto L_GoNextSe;
            L_GoNextRecorder:
                ;
            }
            // could not found recorder. Add one recoder and record se in the recorder
            {
                var recorder = new List<StartEnd>();
                recorders.Add(recorder);
                recorder.Add(se);
            }
        L_GoNextSe:
            ;
        }
        Console.WriteLine(recorders.Count);
    }

    public static string Str(Dictionary<int, List<StartEnd>> dic)
    {
        return string.Join(", ", 
            dic.Select(n => $"{n.Key}:{string.Join(", ", n.Value)}"));
    }
}

public static class Input
{
    private static Queue<string> q = new Queue<string>();
    private static void Confirm() { if (q.Count == 0) foreach (var s in Console.ReadLine().Split(' ')) q.Enqueue(s); }
    public static int NextInt() { Confirm(); return int.Parse(q.Dequeue()); }
    public static long NextLong() { Confirm(); return long.Parse(q.Dequeue()); }
    public static string NextString() { Confirm(); return q.Dequeue(); }
    public static double NextDouble() { Confirm(); return double.Parse(q.Dequeue()); }
    public static int[] LineInt() { return Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); }
    public static long[] LineLong() { return Console.ReadLine().Split(' ').Select(long.Parse).ToArray(); }
    public static string[] LineString() { return Console.ReadLine().Split(' ').ToArray(); }
    public static double[] LineDouble() { return Console.ReadLine().Split(' ').Select(double.Parse).ToArray(); }
    public static void Lines(int N, out int[] a)
    {
        a = new int[N];
        for (int i = 0; i < N; i++)
        {
            var vals = LineInt();
            a[i] = vals[0];
        }
    }
    public static void Lines(int N, out int[] a, out int[] b)
    {
        a = new int[N];
        b = new int[N];
        for (int i = 0; i < N; i++)
        {
            var vals = LineInt();
            a[i] = vals[0];
            b[i] = vals[1];
        }
    }
    public static void Lines(int N, out int[] a, out int[] b, out int[] c)
    {
        a = new int[N];
        b = new int[N];
        c = new int[N];
        for (int i = 0; i < N; i++)
        {
            var vals = LineInt();
            a[i] = vals[0];
            b[i] = vals[1];
            c[i] = vals[2];
        }
    }
}