N = int(input())

def go_through(s, A, B, C, D, E):
    sheep = s
    through_sheep = 0

    a = min(sheep, A, B)
    A -= a
    B -= a
    sheep -= a
    through_sheep += a

    b = min(sheep, D, E)
    D -= b
    E -= b
    sheep -= b
    through_sheep += b

    through_sheep += min(max(A, B), max(D, E), C, sheep)

    return through_sheep

sheep = 10 ** 9
for i in range(N):
    inp = input()
    inp = inp.split(' ')
    sheep = go_through(sheep, int(inp[0]), int(inp[1]), int(inp[2]), int(inp[3]), int(inp[4]))

print(sheep)
