#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int sum = 0, ff = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') ff = -1;
    ch = getchar();
  }
  while (isdigit(ch)) sum = sum * 10 + (ch ^ 48), ch = getchar();
  return sum * ff;
}
const int mod = 1e9 + 7;
const int mo = 998244353;
const int N = 105;
const int M = 5;
int n, m, f[N][N][M][M], ans;
int main() {
  memset(f, -0x3f, sizeof(f));
  n = read();
  for (int i = (1); i <= (n); i++) {
    int x, y, z;
    x = read(), y = read(), z = read();
    f[i][i][x][z] = f[i][i][z][x] = y;
  }
  for (int i = (n); i >= (1); i--)
    for (int j = (i); j <= (n); j++)
      for (int c1 = (1); c1 <= (4); c1++)
        for (int c2 = (1); c2 <= (4); c2++) {
          for (int k = (i); k <= (j - 1); k++) {
            f[i][j][c1][c2] =
                max(f[i][j][c1][c2], max(f[i][k][c1][c2], f[k + 1][j][c1][c2]));
            f[i][j][c1][c2] =
                max(f[i][j][c1][c2], max(f[i][k][c2][c1], f[k + 1][j][c2][c1]));
            for (int c3 = (1); c3 <= (4); c3++)
              f[i][j][c1][c2] =
                  max(f[i][j][c1][c2],
                      max(f[i][k][c1][c3], f[i][k][c3][c1]) +
                          max(f[k + 1][j][c3][c2], f[k + 1][j][c2][c3]));
          }
          ans = max(ans, f[i][j][c1][c2]);
        }
  printf("%d\n", ans);
  return 0;
}
