#include <bits/stdc++.h>   using namespace std;   const int N = (int)1e6 + 3;   int ToInt[4][4];   int FromIntI[6];   int FromIntJ[6];   vector<int> costs[4][4];   vector<int> g[4];   int take[6];   vector<int> cost, from, to;   struct dsu {     vector<int> p;     dsu(int n) : p(n, -1) {}     void Clear() { p.assign(p.size(), -1); }     int Get(int x) {       if (p[x] == -1) return x;       return p[x] = Get(p[x]);     }     void Union(int a, int b) {       a = Get(a);       b = Get(b);       if (a == b) return;       p[a] = b;     }   } d(4);   vector<int> degs;   string oneTake(4, 0);   string taken(4, 0);   int ans;   void brute(int a, int b) {     if (a == 3 && b == 4) {       taken.assign(4, 0);       for (int(i) = (0); (i) < (6); (i)++) {         if (take[i] > 0) {           d.Union(FromIntI[i], FromIntJ[i]);           taken[FromIntI[i]] = 1;           taken[FromIntJ[i]] = 1;         }       }       bool connected = true;       int repr = -1;       for (int(i) = (0); (i) < (4); (i)++) {         if (taken[i]) {           if (repr == -1) {             repr = d.Get(i);           } else {             if (repr != d.Get(i)) {               connected = false;               break;             }           }         }       }       if (!connected) return;       degs.assign(4, 0);       for (int(i) = (0); (i) < (6); (i)++) {         degs[FromIntI[i]] += take[i];         degs[FromIntJ[i]] += take[i];       }       bool euler = true;       int odds = 0;       for (int(i) = (0); (i) < (4); (i)++) {         if (degs[i] % 2 == 1) odds++;       }       if (odds != 0 && odds != 2) euler = false;       if (!euler) return;       int sum = 0;       for (int(i) = (0); (i) < (6); (i)++) {         if (take[i] > 0) {           sum += costs[FromIntI[i]][FromIntJ[i]][take[i] - 1];         }       }       oneTake.assign(4, 0);       for (int(i) = (0); (i) < (4); (i)++) {         for (int(j) = (i + 1); (j) < (4); (j)++) {           if (take[ToInt[i][j]]) {             oneTake[i] = 1;             oneTake[j] = 1;           }         }       }       int oneSum = 0;       for (int(i) = (0); (i) < (4); (i)++) {         if (oneTake[i] && !costs[i][i].empty()) oneSum += costs[i][i].back();       }       ans = max(ans, sum + oneSum);     } else if (b == 4) {       brute(a + 1, a + 2);     } else {       for (int(i) = (0); (i) < (costs[a][b].size() + 1); (i)++) {         take[ToInt[a][b]] = i;         brute(a, b + 1);       }     }   }   int main(int argc, char* argv[]) {     ios::sync_with_stdio(0);     int n;     cin >> n;     for (int(i) = (0); (i) < (n); (i)++) {       int a, b, c;       cin >> a >> c >> b;       a--;       b--;       if (a > b) swap(a, b);       cost.push_back(c);       from.push_back(a);       to.push_back(b);       g[a].push_back((int((cost).size())) - 1);       g[b].push_back((int((cost).size())) - 1);       costs[a][b].push_back(c);     }     int enumCnt = 0;     for (int(i) = (0); (i) < (4); (i)++) {       for (int(j) = (i + 1); (j) < (4); (j)++) {         ToInt[i][j] = enumCnt++;         FromIntI[ToInt[i][j]] = i;         FromIntJ[ToInt[i][j]] = j;       }     }     for (int(i) = (0); (i) < (4); (i)++) {       for (int(j) = (i); (j) < (4); (j)++) {         sort(costs[i][j].begin(), costs[i][j].end());         reverse(costs[i][j].begin(), costs[i][j].end());       }     }     for (int(i) = (0); (i) < (4); (i)++) {       for (int(j) = (i); (j) < (4); (j)++) {         for (int(k) = (1); (k) < ((int((costs[i][j]).size()))); (k)++) {           costs[i][j][k] += costs[i][j][k - 1];         }       }     }     brute(0, 1);     for (int(i) = (0); (i) < (4); (i)++) {       if (!costs[i][i].empty()) {         ans = max(ans, costs[i][i].back());       }     }     cout << ans << endl;     return 0;   }