n_of_segments = int(input())

output = 10 ** 9

for _ in range(n_of_segments):
    a,b,c,d,e = [int(i) for i in input().split()]
    if b > a and e > d: nieco = a+d
    if b <= a and e > d: nieco = b + min(min(a-b,c)+d,e)
    if b > a and e <= d: nieco = e + min(min(d-e,c)+a,b)
    if b <= a and e <= d: nieco = b+e
    output = min(output, nieco)
print(output)