Under construction

Still being built. Things may change

Nested Shrubbery Boxes

Time limit1sMemory limit128 MB

Summary
Choose the largest subset of boxes that nest when every box is rotated to fit strictly inside the next box.
Level

Medium5 of 10

Topics
Dynamic programming, Sorting
Solved
No attempts yet

Problem

Every time Roger the Shrubber finishes a job planting a shrubbery, he hauls a cartload of empty planting boxes back home. A planting box here is a wooden box with one open side.

You are given nn planting boxes. Find the largest number of boxes you can pick so that they all nest. Line the picked boxes up from smallest to largest: the smallest has to fit inside the second smallest, the second smallest has to fit inside the third smallest, and this has to hold all the way to the last box.

Box bib_i fits inside box bjb_j if some rotation of bib_i makes each of its three side lengths shorter than the corresponding side of bjb_j. A box can be rotated any way you like.

Input

The input holds several sets of boxes, and the number of sets is not given in advance. The first line of each set has the number of boxes nn (0≤n≤5000 \le n \le 500). The next nn lines each give the length, width and height of one box. All three values are positive integers no greater than 1000, and each of the first two numbers is followed by a space, a lowercase x, and a space, so one line looks like length x width x height.

Input ends when nn is -1.

Output

For each set of boxes, print on its own line the size of the largest subset that nests completely. A set with no boxes has answer 0.

Examples1

  1. Example 1

    Input
    5
    145 x 472 x 812
    827 x 133 x 549
    381 x 371 x 900
    271 x 389 x 128
    718 x 217 x 491
    4
    432 x 123 x 139
    942 x 844 x 783
    481 x 487 x 577
    677 x 581 x 701
    -1
    
    Expected output
    2
    4