{-# LANGUAGE OverloadedStrings #-}
module Main where

import           Data.List
import           Control.Monad
import           Debug.Trace
import qualified Data.ByteString.Char8         as BC
import           Data.Maybe                     ( fromJust )
import qualified Data.Set                      as S
import qualified Data.IntMap.Strict            as IM
import qualified Data.IntMap.Lazy            as IML
import qualified Data.Map.Strict               as M
import qualified Data.Map.Lazy               as ML
import           Data.Char
import Data.Bits

modN = 1000000007


readTuple2 :: IO (Int, Int)
readTuple2 = do
    [a, b] <- map (fst . fromJust . BC.readInt) . BC.words <$> BC.getLine
    return (a, b)

readTuple3 :: IO (Int, Int, Int)
readTuple3 = do
    [a, b, c] <- map (fst . fromJust . BC.readInt) . BC.words <$> BC.getLine
    return (a, b, c)

readInt :: IO Int
readInt = fst . fromJust . BC.readInt <$> BC.getLine

readInts :: IO [Int]
readInts = map (fst . fromJust . BC.readInt) . BC.words <$> BC.getLine

split :: (a -> Bool) -> [a] -> [[a]]
split f [] = []
split f as =
    let (fo, la) = span f as
        las      = case la of
            [] -> []
            xs -> split f $ tail xs
    in  case fo of
            [] -> las
            xs -> xs : las

pow :: Int -> Int -> Int -> Int
pow base 1 modNum = base `mod` modNum
pow base time modNum
    | odd time = let 
        a = pow base (time `div` 2) modNum `mod` modNum
        in
        a * a * base `mod` modNum
    | otherwise = let
        a = pow base (time `div` 2) modNum `mod` modNum
        in
        a * a `mod` modNum

solve :: Int -> [(Int, Int, Int)] -> Int
solve n acts = let
    actMap = IM.fromList $ zip [1..] acts
    getIJ :: Int -> Int -> Int
    getIJ 0 _ = 0
    getIJ i j = let
        (a, b, c) = actMap IM.! i
        actL = [a, b, c]
        in maximum $ do
            prevActs <- [1,2,3]
            guard $ j /= prevActs
            let prevNum = getIJ (i - 1) prevActs
            let currentNum = actL !! (j - 1)
            pure $ prevNum + currentNum
        
    dp = ML.fromList [((i, j), getIJ i j)| i <- [1..n], j <- [1..3]]
    in maximum $ do
        j <- [1,2,3]
        pure $ dp ML.! (n, j)


main = do
    n <- readInt
    acts <- replicateM n readTuple3
    print $ solve n acts





