import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
//TIP To Run code, press or
// click the icon in the gutter.
public class Main {
static int [][] dp ;
static boolean [] pole = new boolean[1000000000];
public static void main(String[] args) {
//TIP Press with your caret at the highlighted text
// to see how IntelliJ IDEA suggests fixing it.
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
dp = new int[n][5];
for (int i = 0; i < n; i++) {
for (int j = 0; j < 5; j++) {
dp[i][j] = sc.nextInt();
}
}
int max = Integer.MAX_VALUE;
for (int i = 0; i < n; i++) {
max = Math.min(max, findThoughtput(i));
}
System.out.println(max);
//
// Scanner sc = new Scanner(System.in);
// for (int i = 1; i < 1000000000; i++) {
// if (!pole[i])
// {
// pole[i] = false;
// for (int j = i + i; j < 1000000000; j += i) {
// pole[j] = true;
// }
// }
// }
// String string = sc.next();
}
public static int findThoughtput(int i)
{
int [] array = dp[i];
int max = Math.min(array[0], array[1]) + Math.min(array[3], array[4]);
int abrez = Math.max(0, array[1] - array[0]);
int derez = Math.max(0, array[4] - array[3]);
int abvolne = Math.max(0, array[0] - array[1]);
int devolne = Math.max(0, array[3] - array[4]);
max += Math.max(Math.min(Math.min(abrez, devolne), array[2]), Math.min(Math.min(derez, abvolne), array[2]));
return max;
}
}