class gate:
    def __init__(self, a, b, c, d, e):
        self.a = a
        self.b = b
        self.c = c
        self.d = d
        self.e = e

    def pass_through(self, n):
        if (n == -1):
            n = self.a + self.b + self.d + self.e

        pass_top = min(self.a, self.b)
        top = min(n, pass_top)
        self.a -= top
        self.b -= top
        n -= top

        pass_bot = min(self.d, self.e)
        bot = min(n, pass_bot)
        self.d -= bot
        self.e -= bot
        n -= bot

        top_wait = min(self.a, n)
        n -= top_wait
        bot_wait = min(self.d, n)

        wait = top_wait + bot_wait
        rest = self.b + self.e

        return top + bot + min(min(wait, self.c), rest)

count = int(input())
gates = []
for i in range(count):
    data = input().split(" ")
    g = gate(
            int(data[0]),
            int(data[1]),
            int(data[2]),
            int(data[3]),
            int(data[4]),
            )
    gates.append(g)

res = 50
for g in gates:
    res = g.pass_through(res)

print(res)
