lines = int(input())
max_now = -1

for i in range(lines):
    a, b, c, d, e = [int(j) for j in input().split()]
    done = 0
    if a > b:
        done += b
        a -= b
        b = 0
    else:
        done += a
        b -= a
        a = 0
    if d > e:
        done += e
        d -= e
        e = 0
    else:
        done += d
        e -= d
        d = 0
    more = min(a, c, e)
    done += more
    c -= more
    more = min(d, c, b)
    done += more
    if max_now == -1:
        max_now = done
    max_now = min(max_now, done)

print(max_now)
