import java.io.FileNotFoundException;
import java.util.Scanner;

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++) {
            //SEGMENT

            // input
            int a = in.nextInt();
            //output
            int b = in.nextInt();
            //gate
            int c = in.nextInt();
            //input
            int d = in.nextInt();
            //output
            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.min(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);
    }
}