#include <iostream>
#include <vector>

using namespace std;

long long evalClosure(long long  n, long long  a, long long b, long long c, long long d, long long e) {
    long long  x = 0;
    long long topNode = 0;
    long long  downNode = 0;
    long long temp;
    if(n >= (a + d)) {
        //send all through a and d
        topNode = a;
        n -= a;
        a = 0;

        downNode = d;
        n -= d;
        d = 0;

        //send all possible through b (min(topNode, b)) and e (min(downNode, e))
        temp = min(topNode, b);
        //cout << "X += " << temp << "\n";
        x += temp;
        topNode -= temp;
        b -= temp;

        temp = min(downNode, e);
        //cout << "X += " << temp << "\n";
        x += temp;
        downNode -= temp;
        e -= temp;


        if(topNode == 0) {
            topNode += min(downNode, c);
            x += min(topNode, b);
            return x;
        }

        if(downNode == 0) {
            downNode += min(topNode, c);
            x += min(downNode, e);
            return x;
        }
    } else {
        if(d <= e) {
            //send min(n, d) sheep to downNode, they will get through e
            temp = min(n, d);
            n -= temp;
            downNode = temp;
            d -= temp;

            temp = downNode;
            downNode -= temp;
            e -= temp;
            x += temp;

            if(n == 0) {
                return x;
            }

            temp = min(n, a);
            n -= temp;
            a -= temp;
            topNode += temp;

            temp = min(topNode, b);
            topNode -= temp;
            b -= temp;
            x += temp;

            if(topNode == 0) {
                return x;
            }

            temp = min(topNode, c);
            topNode -= temp;
            c -= temp;
            downNode += temp;

            x += min(downNode, e);
        } else if(a <= b) {
            //send min(n, a) sheep to downNode, they will get through b
            temp = min(n, a);
            n -= temp;
            topNode = temp;
            a -= temp;

            temp = topNode;
            topNode -= temp;
            b -= temp;
            x += temp;

            if(n == 0) {
                return x;
            }

            temp = min(n, d);
            n -= temp;
            d -= temp;
            downNode += temp;

            temp = min(downNode, e);
            downNode -= temp;
            e -= temp;
            x += temp;

            if(downNode == 0) {
                return x;
            }

            temp = min(downNode, c);
            downNode -= temp;
            c -= temp;
            topNode += temp;

            x += min(topNode, e);
        } else {
            x = min(n, e + b);
        }
    }

    return x;
}

int main() {
    long long count;
    cin >> count;

    long long curSheep = INT64_MAX;
    long long  a, b, c, d, e;
    for(int i = 0; i < count; i++) {
        cin >> a >> b >> c >> d >> e;
        curSheep = evalClosure(curSheep, a, b, c, d, e);
    }

    cout << curSheep << "\n";
    return 0;
}