import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.HashMap;
import java.util.Vector;

public class Main {
    public static void main(String[] args) throws FileNotFoundException {
        Scanner in = new Scanner(System.in);

        int n = in.nextInt();

        int max = Integer.MAX_VALUE;

        for (int i = 0; i < n; i++) {
            int a = in.nextInt();
            int b = in.nextInt();
            int c = in.nextInt();
            int d = in.nextInt();
            int e = in.nextInt();

            int smthng = 0;

            if (a-b < 0 && d-e > 0) {
                smthng = Math.min(Math.min(d-e, c),Math.abs(a-b));
            } else if (a-b > 0 && d-e < 0) {
                smthng = Math.max(Math.min(a-b, c),Math.abs(d-e));
            }


            max = Math.min(max, Math.min(a,b) + Math.min(d,e) + smthng);
        }

        System.out.println(max);
    }

    private static Vector<coords> readLine(String line, int lineIndex) {
        Vector<coords> result = new Vector<>();
        var chars = line.toCharArray();
        if (chars[0] == '#')
            result.add(new coords(0, lineIndex));
        if (chars[4] == '#')
            result.add(new coords(4, lineIndex));
        if (chars[8] == '#')
            result.add(new coords(8, lineIndex));
        return result;
    }
}

class Pismeno {
    static HashMap<Character, Pismeno> map;

    private coords flag1;
    private coords flag2;

    public Pismeno(coords flag1, coords flag2) {
        this.flag1 = flag1;
        this.flag2 = flag2;
    }
}

class coords{
    int x;
    int y;

    public coords(int x, int y) {
        this.x = x;
        this.y = y;
    }
}