def func():

    tvs = []

    for i in range(num):

        tvs.append((-1, -1))

    for i in range(len(bans)):

        now = bans[i][0]

        can = bans[i][2]

        tvs = [tv for tv in tvs if tv[1] == can] + [tv for tv in tvs if tv[1] != can]

        for j, tv in enumerate(tvs):

            if tv[1] == -1 or (tv[1] == can and tv[0] <= now) or tv[0] <= now - 1:

                del tvs[j]

                tvs.append((bans[i][1], can))

                break

        else:

            return False

    return True





n, C = list(map(int, input().split()))



bans = []

for i in range(n):

    bans.append(tuple(map(int, input().split())))

bans.sort(key=lambda x: x[0])



for num in range(1, 1024):

    if func():

        print(num)

        break

def func():

    tvs = []

    for i in range(num):

        tvs.append((-1, -1))

    for i in range(len(bans)):

        now = bans[i][0]

        can = bans[i][2]

        # tvs = [tv for tv in tvs if tv[1] == can] + [tv for tv in tvs if tv[1] != can]

        for j, tv in enumerate(tvs):

            if tv[1] == -1 or (tv[1] == can and tv[0] <= now) or tv[0] <= now - 1:

                del tvs[j]

                tvs.append((bans[i][1], can))

                break

        else:

            return False

    return True





n, C = list(map(int, input().split()))



bans = []

for i in range(n):

    bans.append(tuple(map(int, input().split())))

bans.sort(key=lambda x: x[0])



for num in range(1, 1024):

    if func():

        print(num)

        break

def func():
    tvs = []
    for i in range(num):
        tvs.append((-1, -1))
    for i in range(len(bans)):
        now = bans[i][0]
        can = bans[i][2]
        tvs = [tv for tv in tvs if tv[1] == can] + [tv for tv in tvs if tv[1] != can]
        for j, tv in enumerate(tvs):
            if tv[1] == -1 or (tv[1] == can and tv[0] <= now) or tv[0] <= now - 1:
                del tvs[j]
                tvs.append((bans[i][1], can))
                break
        else:
            return False
    return True


n, C = list(map(int, input().split()))
bans = []
for i in range(n):
    bans.append(tuple(map(int, input().split())))
bans.sort(key=lambda x: x[0])
for num in range(1, 1024):
    if func():
        print(num)
        break

def func():
    tvs = []
    for i in range(num):
        tvs.append((-1, -1))
    for i in range(len(bans)):
        now = bans[i][0]
        can = bans[i][2]
        # tvs = [tv for tv in tvs if tv[1] == can] + [tv for tv in tvs if tv[1] != can]
        for j, tv in enumerate(tvs):
            if tv[1] == -1 or (tv[1] == can and tv[0] <= now) or tv[0] <= now - 1:
                del tvs[j]
                tvs.append((bans[i][1], can))
                break
        else:
            return False
    return True


n, C = list(map(int, input().split()))
bans = []
for i in range(n):
    bans.append(tuple(map(int, input().split())))
bans.sort(key=lambda x: x[0])
for num in range(1, 1024):
    if func():
        print(num)
        break

-        tvs = [tv for tv in tvs if tv[1] == can] + [tv for tv in tvs if tv[1] != can] +        # tvs = [tv for tv in tvs if tv[1] == can] + [tv for tv in tvs if tv[1] != can]