n = int(input()) inf = 1 << 60 dp = [-inf] * (3 * n) dp[0] = dp[n] = dp[2 * n] = 0 for t in range(n): A = list(map(int, input().split())) ndp = [-inf] * (3 * n) for i1 in range(3): for i2 in range(3): if i1 == i2: continue for j in range(n): nj = j + A[i2] nv = dp[i1 * n + j] if nj >= n: nv += (n - t) * (nj - n + 1) nj = n - 1 nv += nj nj = max(nj - 1, 0) ndp[i2 * n + nj] = max(ndp[i2 * n + nj], nv) dp = ndp print(max(dp))