This page is still under construction.

Parts of this page are still being built. What you see may change.

Matryoshka Dolls, Again

Time limit1sMemory limit128 MB

Summary
Given N dolls each with three dimensions, nest them so every inner doll is strictly smaller on all three axes; minimize the number of outermost dolls.
Level

Medium7 of 10

Topics
Dynamic programming, Graph, Sorting, Greedy
Solved
No attempts yet

Problem

Adam has received yet another box of Matryoshka dolls from Matryona. This time the dolls come in all shapes and sizes. Each doll ii is described by three numbers: its width wiw_i, length lil_i, and height hih_i.

Doll ii can be placed inside doll jj if and only if wi<wjw_i < w_j, li<ljl_i < l_j, and hi<hjh_i < h_j all hold. In other words, every one of the three dimensions must be strictly smaller, and a doll may not be rotated when it is nested. Moreover, each doll can hold at most one other doll directly inside it.

Nest the dolls inside one another so that the number of outermost dolls is as small as possible, and report that minimum number.

Input

The input consists of several test cases. Each test case starts with a line containing a single integer NN, the number of dolls (1≤N≤5001 \le N \le 500). Each of the next NN lines contains three space-separated integers wiw_i, lil_i, and hih_i (1≤wi,li,hi≤10,0001 \le w_i, l_i, h_i \le 10{,}000), the dimensions of the ii-th doll.

The input ends with a line containing N=0N = 0, which must not be processed.

Output

For each test case, print on its own line the minimum possible number of outermost dolls after nesting the given dolls optimally.

Examples1

  1. Example 1

    Input
    3
    5 4 8
    27 10 10
    100 32 523
    3
    1 2 1
    2 1 1
    1 1 2
    4
    1 1 1
    2 3 2
    3 2 2
    4 4 4
    0
    
    Expected output
    1
    3
    2