#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void read(T &x) {
  T f = 1;
  x = 0;
  char s = getchar();
  while (s < '0' || s > '9') {
    if (s == '-') f = -1;
    s = getchar();
  }
  while (s >= '0' && s <= '9') {
    x = x * 10 + s - '0';
    s = getchar();
  }
  x *= f;
}
int f[101][101][4][4], C1[101], v[101], C2[101], ans, n;
inline void solve() {
  read(n);
  for (int i = (1); i <= (n); i++)
    read(C1[i]), read(v[i]), read(C2[i]), C1[i]--, C2[i]--;
  for (int i = (1); i <= (n); i++)
    f[i][i][C1[i]][C2[i]] = f[i][i][C2[i]][C1[i]] = v[i];
  for (int len = (2); len <= (n); len++)
    for (int l = (1); l <= (n - len + 1); l++) {
      int r = l + len - 1;
      for (int k = (l); k <= (r - 1); k++)
        for (int c1 = (0); c1 <= (3); c1++)
          for (int c2 = (0); c2 <= (3); c2++)
            f[l][r][c1][c2] =
                max(f[l][r][c1][c2], max(f[l][k][c1][c2], f[k + 1][r][c1][c2]));
      for (int k = (l); k <= (r - 1); k++)
        for (int c1 = (0); c1 <= (3); c1++)
          for (int c2 = (0); c2 <= (3); c2++)
            for (int c3 = (0); c3 <= (3); c3++) {
              if (f[l][k][c1][c3] && f[k + 1][r][c3][c2])
                f[l][r][c1][c2] =
                    max(f[l][r][c1][c2], f[l][k][c1][c3] + f[k + 1][r][c3][c2]);
              if (f[l][k][c3][c2] && f[k + 1][r][c1][c3])
                f[l][r][c1][c2] =
                    max(f[l][r][c1][c2], f[k + 1][r][c1][c3] + f[l][k][c3][c2]);
            }
    }
  for (int i = (0); i <= (3); i++)
    for (int j = (0); j <= (3); j++) ans = max(ans, f[1][n][i][j]);
  printf("%d\n", ans);
}
int main() { solve(); }
