#include <bits/stdc++.h>
using namespace std;
int n, f[2][32][32], now, s[200];
int main() {
  cin >> n;
  for (int p = 0; p < 16; p++)
    for (int k = 0; k < 16; k++) f[now][p][k] = f[now ^ 1][p][k] = -0x3f3f3f3f;
  f[now][1][0] = 0;
  f[now][2][0] = 0;
  f[now][4][0] = 0;
  f[now][8][0] = 0;
  int c1, c2, v;
  for (int i = 0; i < n; i++) {
    now ^= 1;
    cin >> c1 >> v >> c2;
    if (c1 != c2) {
      for (int p = 1; p < 16; p++) {
        int x = (1 << (4 - c1));
        int y = (1 << (4 - c2));
        if ((p & (x + y)) != x + y) continue;
        for (int k = 0; k <= p; k++) {
          for (int A = 1; A < p; A++) {
            if (((A >> (4 - c1)) & 1) == 1) {
              int B = p ^ A;
              if ((A & B) > 0) continue;
              if (((B >> (4 - c2)) & 1) == 0) continue;
              for (int a = 0; a <= A; a++) {
                if ((a & A) != a) continue;
                for (int b = 0; b <= B; b++) {
                  if ((b & B) != b) continue;
                  if (((a + b) ^ (x + y)) == (k & p))
                    f[now][p][p & k] =
                        max(f[now][p][p & k],
                            f[now ^ 1][A][a] + f[now ^ 1][B][b] + v);
                }
              }
            }
          }
          f[now][p][k] = max(f[now][p][k], f[now ^ 1][p][k ^ (x + y)] + v);
        }
      }
    }
    if (c1 == c2) s[c1] += v;
    for (int p = 0; p < 16; p++) {
      for (int k = 0; k <= p; k++) {
        f[now][p][k] = max(f[now][p][k], f[now ^ 1][p][k]);
        f[now ^ 1][p][k] = -0x3f3f3f3f;
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < 16; i++) {
    for (int p = 0; p < 16; p++) {
      int x = p & i;
      int cnt = 0;
      while (x > 0) {
        if ((x & 1) == 1) cnt++;
        x >>= 1;
      }
      int sum = 0;
      for (int j = 1; j <= 4; j++)
        if (((p >> (4 - j)) & 1) == 1) sum += s[j];
      if (cnt % 2 == 0 && cnt != 4) ans = max(ans, f[now][p][p & i] + sum);
    }
  }
  cout << ans;
  return 0;
}
