#include <bits/stdc++.h>
using namespace std;
int n;
long long ans, f[105][105][5][5];
void upd(long long &x, long long y) { x = max(x, y); }
int main() {
  scanf("%d", &n);
  for (int i = (1); i <= (n); i++)
    for (int j = (1); j <= (n); j++)
      for (int x = (1); x <= (4); x++)
        for (int y = (1); y <= (4); y++) f[i][j][x][y] = -1e9;
  for (int i = (1); i <= (n); i++) {
    int x, y, v;
    scanf("%d%d%d", &x, &v, &y);
    f[i][i][x][y] = f[i][i][y][x] = v;
    ans = max(ans, (long long)v);
  }
  for (int i = (n - 1); i >= (1); i--)
    for (int j = (i + 1); j <= (n); j++)
      for (int x = (1); x <= (4); x++)
        for (int y = (1); y <= (4); y++) {
          for (int k = (i); k <= (j - 1); k++) {
            upd(f[i][j][x][y], max(f[i][k][x][y], f[k + 1][j][x][y]));
            for (int t = (1); t <= (4); t++) {
              upd(f[i][j][x][y], f[i][k][x][t] + f[k + 1][j][t][y]);
              upd(f[i][j][x][y], f[k + 1][j][x][t] + f[i][k][t][y]);
            }
            upd(ans, f[i][j][x][y]);
          }
        }
  cout << ans;
  return 0;
}
