Problem: Matryoshka

Matryoshka is a famous Russian folk craft doll. Matryoshka can be divided into upper and lower parts, and when opened, there is another smaller doll inside. The nesting structure is such that when the small doll that appears is opened, a smaller doll is contained.

<image>


You found an unusually shaped matryoshka doll on your trip and bought N dolls. The shape of the i-th doll is a rectangular parallelepiped of xi × yi × zi.

After watching Matryoshka for a while, you are about to put away Matryoshka. Before that, I want to reduce the space required by storing some dolls in another. When storing a doll, one other doll can be stored only in the doll that has not yet stored any doll. However, only the dolls that are stored directly are counted, and the doll that contains the doll can be stored in another doll.

The stored doll becomes invisible from the outside. However, the following conditions must be met.

* The doll may rotate, but each side of the rectangular parallelepiped is parallel to any side of the other rectangular parallelepiped.
* After rotation, the length of the doll on the side to be stored is shorter for each of the lengths of the corresponding sides.
* At most one doll can be stored directly in one doll



Since the volume of the closet is limited, we want to minimize the sum of the volumes of the dolls that can be seen from the outside. Your job is to create a program that finds the minimum sum of the volumes of the dolls that are visible from the outside, which can be achieved by repeating the operation of storing the dolls any number of times.

Input

The input consists of multiple datasets. The maximum number of data sets does not exceed 50. Each dataset is represented in the following format.

> N
> x1 y1 z1
>::
>::
> xN yN zN
>

Each dataset consists of N + 1 rows, and the first row of the dataset is given the integer N, which represents the number of dolls. In the i-th line of the following N lines, three integers xi, yi, and zi representing the size of the i-th doll are given, separated by a half-width space. These integers satisfy 1 ≤ N, xi, yi, zi ≤ 100.

> The end of the input is represented by a single zero line.

> ### Output

For each data set, output the minimum value of the sum of the volumes of the dolls that can be seen from the outside in one line.

> ### Sample Input


2
one two Three
4 2 3
3
2 5 2
3 3 4
5 5 5
Five
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
Five
1 1 1
2 1 1
3 1 1
4 1 1
5 1 1
Ten
3 1 4
1 5 9
2 6 5
3 5 8
9 7 9
3 2 3
8 4 6
2 6 4
3 3 8
3 2 7
0


Output for Sample Input


twenty four
145
125
15
864





Example

Input

2
1 2 3
4 2 3
3
2 5 2
3 3 4
5 5 5
5
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
5
1 1 1
2 1 1
3 1 1
4 1 1
5 1 1
10
3 1 4
1 5 9
2 6 5
3 5 8
9 7 9
3 2 3
8 4 6
2 6 4
3 3 8
3 2 7
0


Output

24
145
125
15
864

Solution (in CPP):

Solution:
#include <iostream>
#include <algorithm>
#include <array>
#include <vector>
#include <tuple>
#include <limits>
#include <queue>

template <class T>
using MinHeap = std::priority_queue<T, std::vector<T>, std::greater<T>>;

template <class Cap, class Cost>
struct MinCostFlow {
    struct Edge {
        int src, dst;
        Cap cap;
        Cost cost;
        Edge(int src, int dst, Cap cap, Cost cost)
            : src(src), dst(dst), cap(cap), cost(cost){};
    };

    using Edges = std::vector<Edge>;
    using Graph = std::vector<std::vector<int>>;

    Edges edges;
    Graph graph;
    std::vector<Cost> dist, pot;
    std::vector<int> rev;

    const Cost INF = std::numeric_limits<Cost>::max() / 2;

    explicit MinCostFlow(int n) : graph(n), dist(n), pot(n), rev(n) {}

    void span(int u, int v, Cap cap, Cost cost) {
        graph[u].push_back(edges.size());
        edges.emplace_back(u, v, cap, cost);

        graph[v].push_back(edges.size());
        edges.emplace_back(v, u, 0, -cost);
    }

    void dijkstra(int s) {
        std::fill(dist.begin(), dist.end(), INF);
        dist[s] = 0;
        MinHeap<std::pair<Cost, int>> heap;
        heap.emplace(0, s);

        while (!heap.empty()) {
            int u;
            Cost d;
            std::tie(d, u) = heap.top();
            heap.pop();
            if (d > dist[u]) continue;

            for (auto eidx : graph[u]) {
                const auto& edge = edges[eidx];
                int v = edge.dst;

                if (edge.cap > 0 &&
                    dist[u] < INF &&
                    dist[v] > dist[u] + edge.cost + pot[u] - pot[v]) {
                    dist[v] = dist[u] + edge.cost + pot[u] - pot[v];
                    rev[v] = eidx;
                    heap.emplace(dist[v], v);
                }
            }
        }
    }

    Cost exec(int s, int g, Cap flow) {
        Cost ret = 0;
        std::fill(pot.begin(), pot.end(), 0);

        while (flow > 0) {
            dijkstra(s);
            if (dist[g] == INF) break;

            for (int v = 0; v < (int)graph.size(); ++v) {
                pot[v] = std::min(pot[v] + dist[v], INF);
            }

            Cap f = flow;
            int v = g;
            while (v != s) {
                const auto& edge = edges[rev[v]];
                f = std::min(f, edge.cap);
                v = edge.src;
            }

            flow -= f;
            ret += f * pot[g];

            v = g;
            while (v != s) {
                auto& edge = edges[rev[v]];
                auto& redge = edges[rev[v] ^ 1];
                edge.cap -= f;
                redge.cap += f;
                v = edge.src;
            }
        }
        return (flow > 0 ? -1 : ret);
    }
};

using Box = std::array<int, 3>;

bool contain(Box a, Box b) {
    for (int q = 0; q < 6; ++q) {
        bool judge = true;
        for (int i = 0; i < 3; ++i) {
            if (a[i] <= b[i]) judge = false;
        }
        if (judge) return true;

        std::next_permutation(a.begin(), a.end());
    }
    return false;
}

bool solve() {
    int n;
    std::cin >> n;
    if (n == 0) return false;

    std::vector<Box> bs(n);
    for (auto& b : bs) {
        for (auto& x : b) std::cin >> x;
    }

    const int S = n * 2 + 0, G = n * 2 + 1;
    MinCostFlow<int, int> mcf(n * 2 + 2);

    for (int v = 0; v < n; ++v) {
        const auto& b = bs[v];
        mcf.span(S, v, 1, 0);
        mcf.span(S, v + n, 1, b[0] * b[1] * b[2]);
        mcf.span(v + n, G, 1, 0);
    }

    for (int u = 0; u < n; ++u) {
        for (int v = 0; v < n; ++v) {
            if (contain(bs[u], bs[v])) {
                mcf.span(u, v + n, 1, 0);
            }
        }
    }

    std::cout << mcf.exec(S, G, n) << std::endl;
    return true;
}

int main() {
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);
    std::ios::sync_with_stdio(false);

    while (solve()) {}

    return 0;
}