#include <bits/stdc++.h>
using std::max;
template <typename T>
inline void read(T &k) {
  T val = 0, flag = 1;
  char ch = getchar();
  while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
  while (ch == '-') flag = -flag, ch = getchar();
  while (ch >= '0' && ch <= '9')
    val = (val << 1) + (val << 3) + ch - '0', ch = getchar();
  k = val * flag;
}
namespace T1 {
const int N = 1e2 + 10, inf = 1e9 + 7;
int n, a, b, o;
int f[N][N][5][5];
inline void upt(int &a, int b) {
  if (a < b) a = b;
}
void main() {
  read(n);
  memset(f, -63, sizeof f);
  for (int i = 1; i <= n; ++i) {
    read(a), read(o), read(b);
    f[i][i][a][b] = f[i][i][b][a] = o;
  }
  int ans = 0;
  for (int i = n; i >= 1; --i)
    for (int j = i; j <= n; ++j)
      for (int x = 1; x <= 4; ++x)
        for (int y = 1; y <= 4; ++y) {
          for (int t = i; t < j; ++t) {
            upt(f[i][j][x][y], max(f[i][t][x][y], f[t + 1][j][x][y]));
            for (int p = 1; p <= 4; ++p)
              upt(f[i][j][x][y], max(f[i][t][p][y] + f[t + 1][j][x][p],
                                     f[i][t][x][p] + f[t + 1][j][p][y]));
          }
          upt(ans, f[i][j][x][y]);
        }
  printf("%d\n", ans);
}
}  // namespace T1
int main() {
  T1::main();
  return 0;
}
