def solve(): n = list(map(int, input().split()))[0] x = 0 y = 0 dp0 = 0 dp1 = 0 for _ in range(n): pdp0, pdp1 = dp0, dp1 arr = list(map(int, input().split())) nx, ny = arr for _ in range(2): ndp00 = pdp0 if y == nx: ndp00 += nx ndp01 = pdp1 if x == nx: ndp01 += nx dp0 = max(ndp00, ndp01) if nx == ny: dp0 += nx x, y = y, x nx, ny = ny, nx dp0, dp1 = dp1, dp0 pdp0, pdp1 = pdp1, pdp0 x, y = nx, ny print(max(dp0, dp1)) if __name__ == '__main__': solve()