N = int(input())
summa = -1
for i in range(N):
    a, b, c, d, e = map(int, input().split())
    bb = max(b - a, 0)
    ee = max(e - d, 0)


    prev = summa
    if a == b:
        summa = a + min(d, e)
    elif d == e:
        summa = d + min(a, b)
    elif ee == 0 and b > a:
        summa = a + e + min(d, c, bb)
    elif bb == 0 and e > d:
        summa = d + b + min(a, c, ee)
    elif bb == 0 and ee == 0:
        summa = b + e
    else:
        exit('a')
    if prev != -1:
        summa = min(summa, prev)
print(summa)
