//
// main.cpp
// Tower of Babylon
//
// Created by Himanshu on 17/09/21.
//
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
struct block {
int x,y,z;
};
bool cmp(const struct block &a,const struct block &b) {
if(a.x != b.x)
return (a.x < b.x);
else if(a.y != b.y)
return (a.y < b.y);
else
return (a.z < b.z);
}
block newBlock(int p,int q,int r) {
block b;
b.x = p;
b.y = q;
b.z = r;
return b;
}
int main() {
int n, ans, p, q, r;
scanf("%d",&n);
// Since each block can be rotated in 6 ways, 1 for each face
int N = 6*n;
block b[N];
int lis[N];
// Input terminates for n = 0, according to problem statement
while(n != 0) {
int j = 0;
ans = 0;
for(int i=0; i<n; i++) {
scanf("%d %d %d",&p,&q,&r);
// Adding all the combinations obtained by rotating a cuboid
b[j++] = newBlock(p,q,r);
b[j++] = newBlock(q,r,p);
b[j++] = newBlock(r,p,q);
b[j++] = newBlock(q,p,r);
b[j++] = newBlock(r,q,p);
b[j++] = newBlock(p,r,q);
}
sort(b,b+N,&cmp);
for(int i=0;i<N;i++) {
lis[i] = b[i].z;
for(j=0;j<i;j++) {
// To check if jth block can be placed above ith block
if((b[j].x < b[i].x && b[j].y < b[i].y) || (b[j].x < b[i].y && b[j].y < b[i].x)) {
//Optimal solution by including b[i] or not
lis[i] = max(lis[i],(lis[j]+b[i].z));
}
}
ans = max(ans,lis[i]);
}
printf("%d\n",ans);
scanf("%d",&n);
}
return 0;
}
Ly8KLy8gIG1haW4uY3BwCi8vICBUb3dlciBvZiBCYWJ5bG9uCi8vCi8vICBDcmVhdGVkIGJ5IEhpbWFuc2h1IG9uIDE3LzA5LzIxLgovLwoKI2luY2x1ZGU8Y3N0ZGlvPgojaW5jbHVkZTxpb3N0cmVhbT4KI2luY2x1ZGU8YWxnb3JpdGhtPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCnN0cnVjdCBibG9jayB7CiBpbnQgeCx5LHo7Cn07Cgpib29sIGNtcChjb25zdCBzdHJ1Y3QgYmxvY2sgJmEsY29uc3Qgc3RydWN0IGJsb2NrICZiKSB7CiAgICBpZihhLnggIT0gYi54KQogICAgICAgIHJldHVybiAoYS54IDwgYi54KTsKICAgIGVsc2UgaWYoYS55ICE9IGIueSkKICAgICAgICByZXR1cm4gKGEueSA8IGIueSk7CiAgICBlbHNlCiAgICAgICAgcmV0dXJuIChhLnogPCBiLnopOwp9CgpibG9jayBuZXdCbG9jayhpbnQgcCxpbnQgcSxpbnQgcikgewogICAgYmxvY2sgYjsKICAgIGIueCA9IHA7CiAgICBiLnkgPSBxOwogICAgYi56ID0gcjsKICAgIHJldHVybiBiOwp9CgppbnQgbWFpbigpIHsKICAgIGludCBuLCBhbnMsIHAsIHEsIHI7CiAgICBzY2FuZigiJWQiLCZuKTsKICAgIAogICAgLy8gU2luY2UgZWFjaCBibG9jayBjYW4gYmUgcm90YXRlZCBpbiA2IHdheXMsIDEgZm9yIGVhY2ggZmFjZQogICAgaW50IE4gPSA2Km47CiAgICBibG9jayBiW05dOwogICAgaW50IGxpc1tOXTsKICAgIAogICAgLy8gSW5wdXQgdGVybWluYXRlcyBmb3IgbiA9IDAsIGFjY29yZGluZyB0byBwcm9ibGVtIHN0YXRlbWVudAogICAgd2hpbGUobiAhPSAwKSB7CiAgICAgICAgaW50IGogPSAwOwogICAgICAgIGFucyA9IDA7CiAgICAgICAgCiAgICAgICAgZm9yKGludCBpPTA7IGk8bjsgaSsrKSB7CiAgICAgICAgICAgIHNjYW5mKCIlZCAlZCAlZCIsJnAsJnEsJnIpOwogICAgICAgICAgICAKICAgICAgICAgICAgLy8gQWRkaW5nIGFsbCB0aGUgY29tYmluYXRpb25zIG9idGFpbmVkIGJ5IHJvdGF0aW5nIGEgY3Vib2lkCiAgICAgICAgICAgIGJbaisrXSA9IG5ld0Jsb2NrKHAscSxyKTsKICAgICAgICAgICAgYltqKytdID0gbmV3QmxvY2socSxyLHApOwogICAgICAgICAgICBiW2orK10gPSBuZXdCbG9jayhyLHAscSk7CiAgICAgICAgICAgIGJbaisrXSA9IG5ld0Jsb2NrKHEscCxyKTsKICAgICAgICAgICAgYltqKytdID0gbmV3QmxvY2socixxLHApOwogICAgICAgICAgICBiW2orK10gPSBuZXdCbG9jayhwLHIscSk7CiAgICAgICAgICAgIAogICAgICAgIH0KICAgICAgICBzb3J0KGIsYitOLCZjbXApOwogICAgICAgIAogICAgICAgIGZvcihpbnQgaT0wO2k8TjtpKyspIHsKICAgICAgICAgICAgbGlzW2ldID0gYltpXS56OwogICAgICAgICAgICBmb3Ioaj0wO2o8aTtqKyspIHsKICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgLy8gVG8gY2hlY2sgaWYganRoIGJsb2NrIGNhbiBiZSBwbGFjZWQgYWJvdmUgaXRoIGJsb2NrCiAgICAgICAgICAgICAgICBpZigoYltqXS54IDwgYltpXS54ICYmIGJbal0ueSA8IGJbaV0ueSkgfHwgKGJbal0ueCA8IGJbaV0ueSAmJiBiW2pdLnkgPCBiW2ldLngpKSB7CiAgICAgICAgICAgICAgICAgICAgLy9PcHRpbWFsIHNvbHV0aW9uIGJ5IGluY2x1ZGluZyBiW2ldIG9yIG5vdAogICAgICAgICAgICAgICAgICAgIGxpc1tpXSA9IG1heChsaXNbaV0sKGxpc1tqXStiW2ldLnopKTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICBhbnMgPSBtYXgoYW5zLGxpc1tpXSk7CiAgICAgICAgfQogICAgICAgIAogICAgICAgIHByaW50ZigiJWRcbiIsYW5zKTsKICAgICAgICBzY2FuZigiJWQiLCZuKTsKIH0KICAgIAogcmV0dXJuIDA7Cn0K