#include <bits/stdc++.h>
using namespace std;
const int MX = 100 + 5;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
int n;
int ans, dp[MX][MX][5][5];
void upd(int &x, int 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++) dp[i][j][x][y] = -1e9;
      }
    }
  }
  for (int i = 1, x, y, z; i <= n; i++) {
    scanf("%d%d%d", &x, &z, &y);
    dp[i][i][x][y] = dp[i][i][y][x] = z;
    ans = max(ans, z);
  }
  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; k++) {
            upd(dp[i][j][x][y], max(dp[i][k][x][y], dp[k + 1][j][x][y]));
            for (int t = 1; t <= 4; t++) {
              upd(dp[i][j][x][y], dp[i][k][x][t] + dp[k + 1][j][t][y]);
              upd(dp[i][j][x][y], dp[k + 1][j][x][t] + dp[i][k][t][y]);
            }
            upd(ans, dp[i][j][x][y]);
          }
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
