#include <bits/stdc++.h>   using namespace std;   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     long long n;     cin >> n;     vector<long long> tpa[6], tpb[4];     long long amt[6];     fill(amt, amt + 6, 0);     for (long long i = 0; i < n; ++i) {       long long a, b, c;       cin >> a >> b >> c;       --a;       --c;       if (c < a) {         swap(a, c);       }       if (a == 0 && c == 0) {         tpb[0].push_back(b);       } else if (a == 0 && c == 1) {         tpa[0].push_back(b);         ++amt[0];       } else if (a == 0 && c == 2) {         tpa[1].push_back(b);         ++amt[1];       } else if (a == 0 && c == 3) {         tpa[2].push_back(b);         ++amt[2];       } else if (a == 1 && c == 1) {         tpb[1].push_back(b);       } else if (a == 1 && c == 2) {         tpa[3].push_back(b);         ++amt[3];       } else if (a == 1 && c == 3) {         tpa[4].push_back(b);         ++amt[4];       } else if (a == 2 && c == 2) {         tpb[2].push_back(b);       } else if (a == 2 && c == 3) {         tpa[5].push_back(b);         ++amt[5];       } else {         tpb[3].push_back(b);       }     }     for (long long i = 0; i < 6; ++i) {       if (i < 4) sort((tpb[i]).begin(), (tpb[i]).end(), greater<long long>());       sort((tpa[i]).begin(), (tpa[i]).end(), greater<long long>());     }     long long ans = 0;     for (long long i = 0; i < 4; ++i) {       long long cr = 0;       for (long long j : tpb[i]) {         cr += j;       }       ans = max(ans, cr);     }     long long val[amt[0] + 1][amt[1] + 1][amt[2] + 1][amt[3] + 1][amt[4] + 1]                  [amt[5] + 1];     memset(val, 0, sizeof val);     for (long long a = 0; a <= amt[0]; ++a) {       for (long long b = 0; b <= amt[1]; ++b) {         for (long long c = 0; c <= amt[2]; ++c) {           for (long long d = 0; d <= amt[3]; ++d) {             for (long long e = 0; e <= amt[4]; ++e) {               for (long long f = 0; f <= amt[5]; ++f) {                 if (!a && !b && !c && !d && !e && !f) continue;                 if (a) {                   val[a][b][c][d][e][f] =                       max(val[a][b][c][d][e][f],                           val[a - 1][b][c][d][e][f] + tpa[0][a - 1]);                 }                 if (b) {                   val[a][b][c][d][e][f] =                       max(val[a][b][c][d][e][f],                           val[a][b - 1][c][d][e][f] + tpa[1][b - 1]);                 }                 if (c) {                   val[a][b][c][d][e][f] =                       max(val[a][b][c][d][e][f],                           val[a][b][c - 1][d][e][f] + tpa[2][c - 1]);                 }                 if (d) {                   val[a][b][c][d][e][f] =                       max(val[a][b][c][d][e][f],                           val[a][b][c][d - 1][e][f] + tpa[3][d - 1]);                 }                 if (e) {                   val[a][b][c][d][e][f] =                       max(val[a][b][c][d][e][f],                           val[a][b][c][d][e - 1][f] + tpa[4][e - 1]);                 }                 if (f) {                   val[a][b][c][d][e][f] =                       max(val[a][b][c][d][e][f],                           val[a][b][c][d][e][f - 1] + tpa[5][f - 1]);                 }               }             }           }         }       }     }     for (long long i = 0; i < 16; ++i) {       for (long long j = 0; j < 6; ++j) {         amt[j] = tpa[j].size();       }       long long cur = 0;       bool adj[4];       fill(adj, adj + 4, false);       for (long long j = 0; j < 4; ++j) {         if (i & (1 << j)) {           for (long long k : tpb[j]) {             cur += k;           }         } else {           adj[j] = 1;           if (j == 0) {             amt[0] = amt[1] = amt[2] = 0;           } else if (j == 1) {             amt[0] = amt[3] = amt[4] = 0;           } else if (j == 2) {             amt[1] = amt[3] = amt[5] = 0;           } else if (j == 3) {             amt[2] = amt[4] = amt[5] = 0;           }         }       }       if (!adj[0] && !amt[0] && !amt[1] && !amt[2]) continue;       if (!adj[1] && !amt[0] && !amt[3] && !amt[4]) continue;       if (!adj[2] && !amt[1] && !amt[3] && !amt[5]) continue;       if (!adj[3] && !amt[2] && !amt[4] && !amt[5]) continue;       bitset<4> dp[amt[0] + 1][amt[1] + 1][amt[2] + 1][amt[3] + 1][amt[4] + 1]                   [amt[5] + 1];       for (long long j = 0; j < 4; ++j) {         dp[amt[0]][amt[1]][amt[2]][amt[3]][amt[4]][amt[5]][j] = 1;       }       for (long long a = amt[0]; a >= 0; --a) {         for (long long b = amt[1]; b >= 0; --b) {           for (long long c = amt[2]; c >= 0; --c) {             for (long long d = amt[3]; d >= 0; --d) {               for (long long e = amt[4]; e >= 0; --e) {                 for (long long f = amt[5]; f >= 0; --f) {                   if (a != amt[0]) {                     if (dp[a + 1][b][c][d][e][f][0]) {                       dp[a][b][c][d][e][f][1] = 1;                     }                     if (dp[a + 1][b][c][d][e][f][1]) {                       dp[a][b][c][d][e][f][0] = 1;                     }                   }                   if (b != amt[1]) {                     if (dp[a][b + 1][c][d][e][f][0]) {                       dp[a][b][c][d][e][f][2] = 1;                     }                     if (dp[a][b + 1][c][d][e][f][2]) {                       dp[a][b][c][d][e][f][0] = 1;                     }                   }                   if (c != amt[2]) {                     if (dp[a][b][c + 1][d][e][f][0]) {                       dp[a][b][c][d][e][f][3] = 1;                     }                     if (dp[a][b][c + 1][d][e][f][3]) {                       dp[a][b][c][d][e][f][0] = 1;                     }                   }                   if (d != amt[3]) {                     if (dp[a][b][c][d + 1][e][f][2]) {                       dp[a][b][c][d][e][f][1] = 1;                     }                     if (dp[a][b][c][d + 1][e][f][1]) {                       dp[a][b][c][d][e][f][2] = 1;                     }                   }                   if (e != amt[4]) {                     if (dp[a][b][c][d][e + 1][f][3]) {                       dp[a][b][c][d][e][f][1] = 1;                     }                     if (dp[a][b][c][d][e + 1][f][1]) {                       dp[a][b][c][d][e][f][3] = 1;                     }                   }                   if (f != amt[5]) {                     if (dp[a][b][c][d][e][f + 1][2]) {                       dp[a][b][c][d][e][f][3] = 1;                     }                     if (dp[a][b][c][d][e][f + 1][3]) {                       dp[a][b][c][d][e][f][2] = 1;                     }                   }                 }               }             }           }         }       }       long long cans = 0;       for (long long a = max(0LL, amt[0]); a >= 0; --a) {         for (long long b = max(0LL, amt[1]); b >= 0; --b) {           for (long long c = max(0LL, amt[2]); c >= 0; --c) {             for (long long d = max(0LL, amt[3]); d >= 0; --d) {               for (long long e = max(0LL, amt[4]); e >= 0; --e) {                 for (long long f = max(0LL, amt[5]); f >= 0; --f) {                   bool usd[4];                   fill(usd, usd + 4, 0);                   if (a < amt[0] || b < amt[1] || c < amt[2]) usd[0] = 1;                   if (a < amt[0] || d < amt[3] || e < amt[4]) usd[1] = 1;                   if (b < amt[1] || d < amt[3] || f < amt[5]) usd[2] = 1;                   if (c < amt[2] || e < amt[4] || f < amt[5]) usd[3] = 1;                   bool ok = 0;                   for (long long j = 0; j < 4; ++j) {                     if (dp[a][b][c][d][e][f][j]) ok = 1;                     if (i & (1 << j)) {                       if (!usd[j]) {                         ok = false;                         break;                       }                     }                   }                   if (!ok) continue;                   cans = max(cans, cur + val[amt[0] - a][amt[1] - b][amt[2] - c]                                             [amt[3] - d][amt[4] - e][amt[5] - f]);                 }               }             }           }         }       }       for (long long j = 0; j < 6; ++j) {         amt[j] = tpa[j].size();       }       ans = max(ans, cans);     }     cout << ans <<   n ;   }