#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const double oula = 0.57721566490153286060651209;
using namespace std;
int n;
int dp[105][105][5][5];
int main() {
  std::ios::sync_with_stdio(false);
  cin >> n;
  memset(dp, -0x3f3f3f3f, sizeof(dp));
  int a, b, c;
  for (int i = 1; i <= n; i++) {
    cin >> a >> b >> c;
    dp[i][i][a][c] = dp[i][i][c][a] = b;
  }
  int ans = 0;
  for (int i = n; i; i--) {
    for (int j = i; j <= n; j++) {
      for (int q = 1; q <= 4; q++) {
        for (int w = 1; w <= 4; w++) {
          for (int e = i; e <= j + 1; e++) {
            dp[i][j][q][w] =
                max(dp[i][j][q][w], max(dp[i][e][q][w], dp[e + 1][j][q][w]));
            for (int r = 1; r <= 4; r++) {
              dp[i][j][q][w] =
                  max(dp[i][j][q][w], max(dp[i][e][r][w] + dp[e + 1][j][q][r],
                                          dp[i][e][q][r] + dp[e + 1][j][r][w]));
            }
          }
          ans = max(ans, dp[i][j][q][w]);
        }
      }
    }
  }
  cout << ans << endl;
}
