// @author godmar import java.util.*; import static java.lang.Math.*; public class godmar { public static void main(String ...args) { var sc = new Scanner(System.in); int r = sc.nextInt(); int g = sc.nextInt(); int b = sc.nextInt(); int cr = sc.nextInt(); int cg = sc.nextInt(); int cb = sc.nextInt(); int crg = sc.nextInt(); int cgb = sc.nextInt(); int need_r = max(r - cr, 0); int need_b = max(b - cb, 0); int need_g = max(g - cg, 0); if (need_r > crg || need_b > cgb) { System.out.println(-1); return; } crg -= need_r; cgb -= need_b; if (need_g > cgb + crg) { System.out.println(-1); return; } System.out.println(need_r + need_g + need_b); } }