Today's morning was exceptionally snowy. Meshanya decided to go outside and noticed a huge snowball rolling down the mountain! Luckily, there are two stones on that mountain.

Initially, snowball is at height $h$ and it has weight $w$. Each second the following sequence of events happens: snowball's weights increases by $i$, where $i$ — is the current height of snowball, then snowball hits the stone (if it's present at the current height), then snowball moves one meter down. If the snowball reaches height zero, it stops.

There are exactly two stones on the mountain. First stone has weight $u_1$ and is located at height $d_1$, the second one — $u_2$ and $d_2$ respectively. When the snowball hits either of two stones, it loses weight equal to the weight of that stone. If after this snowball has negative weight, then its weight becomes zero, but the snowball continues moving as before. [Image] 

Find the weight of the snowball when it stops moving, that is, it reaches height 0.


-----Input-----

First line contains two integers $w$ and $h$ — initial weight and height of the snowball ($0 \le w \le 100$; $1 \le h \le 100$).

Second line contains two integers $u_1$ and $d_1$ — weight and height of the first stone ($0 \le u_1 \le 100$; $1 \le d_1 \le h$).

Third line contains two integers $u_2$ and $d_2$ — weight and heigth of the second stone ($0 \le u_2 \le 100$; $1 \le d_2 \le h$; $d_1 \ne d_2$). Notice that stones always have different heights.


-----Output-----

Output a single integer — final weight of the snowball after it reaches height 0.


-----Examples-----
Input
4 3
1 1
1 2

Output
8
Input
4 3
9 2
0 1

Output
1


-----Note-----

In the first example, initially a snowball of weight 4 is located at a height of 3, there are two stones of weight 1, at a height of 1 and 2, respectively. The following events occur sequentially:   The weight of the snowball increases by 3 (current height), becomes equal to 7.  The snowball moves one meter down, the current height becomes equal to 2.  The weight of the snowball increases by 2 (current height), becomes equal to 9.  The snowball hits the stone, its weight decreases by 1 (the weight of the stone), becomes equal to 8.  The snowball moves one meter down, the current height becomes equal to 1.  The weight of the snowball increases by 1 (current height), becomes equal to 9.  The snowball hits the stone, its weight decreases by 1 (the weight of the stone), becomes equal to 8.  The snowball moves one meter down, the current height becomes equal to 0. 

Thus, at the end the weight of the snowball is equal to 8.

Difficulty: EASY
Tags: Implementation

Starter code:

Solutions:
(m, n) = map(int, input().split())
(x1, y1) = map(int, input().split())
(x2, y2) = map(int, input().split())
v = m
for i in range(n, 0, -1):
	v += i
	if y1 == i:
		v -= x1
	if y2 == i:
		v -= x2
	if v < 0:
		v = 0
print(v)


(n, m) = map(int, input().split())
(t1, t2) = map(int, input().split())
(t3, t4) = map(int, input().split())
while m:
	n += m
	if m == t2:
		n = max(0, n - t1)
	if m == t4:
		n = max(0, n - t3)
	m -= 1
print(n)


(n, h) = map(int, input().rstrip().split())
(w1, s1) = map(int, input().rstrip().split())
(w2, s2) = map(int, input().rstrip().split())
while h > 0:
	n = n + h
	if s1 == h:
		n = max(0, n - w1)
	if s2 == h:
		n = max(0, n - w2)
	h = h - 1
print(n)


from math import *
from collections import *
from itertools import *
from bisect import *
from heapq import *
from operator import *

def io():
	return map(int, input().split())

def op():
	return list(map(int, input().split()))

def kl(con, x=0):
	if x == 0:
		print('Yes') if con else print('No')
	elif x == 1:
		print('yes') if con else print('no')
	elif x == 2:
		print('YES') if con else print('NO')
MOD = 1000000007
MAX = 2147483647
MIN = -2147483648
i = input
(a, b) = io()
(c, d) = io()
(e, f) = io()
ans = a
for i in range(b, 0, -1):
	ans += i
	if i == d:
		ans -= min(c, ans)
	elif i == f:
		ans -= min(ans, e)
print(ans)


import sys
import math
from collections import defaultdict

def getInputFromLine():
	return [int(x) for x in input().split()]
try:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
except:
	pass
(w, h) = getInputFromLine()
(w1, d1) = getInputFromLine()
(w2, d2) = getInputFromLine()
ans = w
for i in range(h, 0, -1):
	ans += i
	if i == d1:
		ans -= w1
	elif i == d2:
		ans -= w2
	if ans <= 0:
		ans = 0
print(ans)


(w, h) = list(map(int, input().split()))
(w1, d1) = list(map(int, input().split()))
(w2, d2) = list(map(int, input().split()))
ans = w
for i in reversed(list(range(h + 1))):
	ans += i
	if i == d1:
		ans = max(0, ans - w1)
	if i == d2:
		ans = max(0, ans - w2)
print(ans)


(w, h) = map(int, input().split())
(u1, d1) = map(int, input().split())
(u2, d2) = map(int, input().split())
while h > 0:
	w += h
	if h == d1:
		w -= u1
		if w < 0:
			w = 0
	elif h == d2:
		w -= u2
		if w < 0:
			w = 0
	h -= 1
print(w)


(w, h) = map(int, input().split())
(w1, h1) = map(int, input().split())
(w2, h2) = map(int, input().split())
for i in range(h, 0, -1):
	w += i
	if i == h1:
		w -= w1
	elif i == h2:
		w -= w2
	if w <= 0:
		w = 0
print(w)


(w, h) = [int(x) for x in input().split()]
(u1, d1) = [int(x) for x in input().split()]
(u2, d2) = [int(x) for x in input().split()]
for i in range(h, 0, -1):
	w += i
	if i == d1:
		w -= u1
	elif i == d2:
		w -= u2
	if w < 0:
		w = 0
print(w)


import sys
import math
(w, h) = map(int, input().split())
(wa, ha) = map(int, input().split())
(wb, hb) = map(int, input().split())
(suma, k) = (w, 0)
for i in range(h, 0, -1):
	suma += i
	if i == ha:
		suma -= wa
	elif i == hb:
		suma -= wb
	if suma < 0:
		suma = 0
print(suma)


(w, h) = map(int, input().split())
(u1, h1) = map(int, input().split())
(u2, h2) = map(int, input().split())
if h2 > h1:
	(h1, h2) = (h2, h1)
	(u1, u2) = (u2, u1)
w += (h + h1) * (h - h1 + 1) // 2
w -= u1
if w < 0:
	w = 0
w += (h1 - 1 + h2) * (h1 - h2) // 2
w -= u2
if w < 0:
	print((h2 - 1) * h2 // 2)
else:
	print(w + (h2 - 1) * h2 // 2)


(x, y) = map(int, input().split())
(w1, h1) = map(int, input().split())
(w2, h2) = map(int, input().split())
for i in range(y, 0, -1):
	x += i
	if i == h1:
		x = x - w1
		if x < 0:
			x = 0
	if i == h2:
		x = x - w2
		if x < 0:
			x = 0
print(x)


(w, h) = map(int, input().split())
(u1, d1) = map(int, input().split())
(u2, d2) = map(int, input().split())
current_w = w
current_h = h
for i in range(h, 0, -1):
	current_w += current_h
	if i == d1:
		current_w -= u1
		if current_w < 0:
			current_w = 0
	if i == d2:
		current_w -= u2
		if current_w < 0:
			current_w = 0
	current_h -= 1
print(current_w)


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, copy, functools
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI():
	return [tuple(map(int, l.split())) for l in sys.stdin]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def main():
	(w, h) = LI()
	(u1, d1) = LI()
	(u2, d2) = LI()
	while h >= 0:
		w += h
		if d1 == h:
			w -= u1
		if d2 == h:
			w -= u2
		if w < 0:
			w = 0
		h -= 1
	return w
print(main())


I = lambda : map(int, input().split())
(w, h) = I()
(u1, d1) = I()
(u2, d2) = I()
for i in range(h, 0, -1):
	w += i
	if i == d1:
		w = max(0, w - u1)
	elif i == d2:
		w = max(0, w - u2)
print(w)


snowBall = list(map(int, input().split()))
firstStone = list(map(int, input().split()))
secondStone = list(map(int, input().split()))
while snowBall[1]:
	snowBall[0] += snowBall[1]
	if snowBall[1] == firstStone[1]:
		snowBall[0] -= firstStone[0]
	if snowBall[1] == secondStone[1]:
		snowBall[0] -= secondStone[0]
	snowBall[0] = max(snowBall[0], 0)
	snowBall[1] -= 1
print(snowBall[0])


def main():
	(w, h) = map(int, input().split())
	(u1, d1) = map(int, input().split())
	(u2, d2) = map(int, input().split())
	if d1 < d2:
		(d1, d2) = (d2, d1)
		(u1, u2) = (u2, u1)
	for i in range(h, d1 - 1, -1):
		w += i
	w = max(w - u1, 0)
	for i in range(d1 - 1, d2 - 1, -1):
		w += i
	w = max(w - u2, 0)
	for i in range(d2 - 1, 0, -1):
		w += i
	print(w)
	return 0
main()


(x1, x2, x3) = (input().split(), input().split(), input().split())
(w, h) = (int(x1[0]), int(x1[1]))
(w1, h1, w2, h2) = (int(x2[0]), int(x2[1]), int(x3[0]), int(x3[1]))
while not h == 0:
	w = w + h
	if h == h1 or h == h2:
		if h == h1:
			w = w - w1
		else:
			w = w - w2
		if w < 0:
			w = 0
	h = h - 1
print(w)


(w, h) = input().split()
w = int(w)
h = int(h)
(u1, d1) = input().split()
(u2, d2) = input().split()
u1 = int(u1)
u2 = int(u2)
d1 = int(d1)
d2 = int(d2)
if d1 > h:
	u1 = 0
if d2 > h:
	u2 = 0
sums = h * (h + 1) // 2
total = w + sums - u1 - u2
d = min(d1, d2)
ans = (d - 1) * d // 2
dmax = max(d1, d2)
if d1 == dmax:
	umax = u2
else:
	umax = u1
third = dmax * (dmax - 1) // 2 - umax
print(max(0, total, ans, third))


(w, h) = list(map(int, input().split()))
(u1, d1) = list(map(int, input().split()))
(u2, d2) = list(map(int, input().split()))
for i in range(h, -1, -1):
	w += i
	if i == d1:
		w -= u1
	if i == d2:
		w -= u2
	w = max(w, 0)
print(w)


(a, b) = map(int, input().split())
(w1, h1) = map(int, input().split())
(w2, h2) = map(int, input().split())
for i in range(b, -1, -1):
	a += i
	if i == h1:
		a = max(a - w1, 0)
	elif i == h2:
		a = max(a - w2, 0)
print(int(a))


def solve(w, h, u1, d1, u2, d2):
	for ch in range(h, -1, -1):
		w += ch
		if d1 == ch:
			w = max(w - u1, 0)
		elif d2 == ch:
			w = max(w - u2, 0)
	return w

def main():
	(w, h) = list(map(int, input().split()))
	(u1, d1) = list(map(int, input().split()))
	(u2, d2) = list(map(int, input().split()))
	print(solve(w, h, u1, d1, u2, d2))
main()


s1 = input().split()
s2 = input().split()
s3 = input().split()
(w, h) = (int(s1[0]), int(s1[1]))
(u1, d1) = (int(s2[0]), int(s2[1]))
(u2, d2) = (int(s3[0]), int(s3[1]))
x = d1
y = d2
e = u1
if y > x:
	d1 = d2
	d2 = x
	u1 = u2
	u2 = e

def func(x):
	return x * (x + 1) // 2
a = func(h) - func(d1 - 1)
w += a
w = max(0, w - u1)
b = func(d1 - 1) - func(d2 - 1)
w += b
w = max(0, w - u2)
c = func(d2 - 1)
w += c
print(w)


X = list(map(int, input().split()))
S1 = list(map(int, input().split()))
S2 = list(map(int, input().split()))
if S2[1] > S1[1]:
	(S1[0], S1[1], S2[0], S2[1]) = (S2[0], S2[1], S1[0], S1[1])
X[0] += sum(range(X[1], S1[1] - 1, -1))
X[0] = 0 if S1[0] > X[0] else X[0] - S1[0]
X[0] += sum(range(S1[1] - 1, S2[1] - 1, -1))
X[0] = 0 if S2[0] > X[0] else X[0] - S2[0]
X[0] += sum(range(S2[1] - 1, -1, -1))
print(X[0])


(w, h) = [int(x) for x in input().split()]
(u1, d1) = [int(x) for x in input().split()]
(u2, d2) = [int(x) for x in input().split()]
h_temp = h
w_temp = w
for x in range(h, -1, -1):
	if x == d1:
		w_temp = w_temp + x
		if w_temp - u1 < 0:
			w_temp = 0
		else:
			w_temp = w_temp - u1
	elif x == d2:
		w_temp = w_temp + x
		if w_temp - u2 < 0:
			w_temp = 0
		else:
			w_temp = w_temp - u2
	else:
		w_temp = w_temp + x
print(w_temp)


__author__ = 'Sudip'
x = list(map(int, input().split()))
y = list(map(int, input().split()))
z = list(map(int, input().split()))
weight = x[0]
for i in range(x[1], -1, -1):
	weight += i
	if y[1] == i:
		weight -= y[0]
		if weight < 0:
			weight = 0
	if z[1] == i:
		weight -= z[0]
		if weight < 0:
			weight = 0
print(weight)


from collections import defaultdict as dd
from collections import deque
import bisect
import heapq

def ri():
	return int(input())

def rl():
	return list(map(int, input().split()))

def solve():
	(w, h) = rl()
	(u1, d1) = rl()
	(u2, d2) = rl()
	while h:
		w += h
		if h == d1:
			w -= u1
		if h == d2:
			w -= u2
		if w < 0:
			w = 0
		h -= 1
	print(w)
mode = 's'
if mode == 'T':
	t = ri()
	for i in range(t):
		solve()
else:
	solve()


(w, h) = map(int, input().split())
(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
while h:
	w += h
	if h == b:
		w = max(0, w - a)
	elif h == d:
		w = max(0, w - c)
	h -= 1
print(w)


import math
import bisect
import itertools
import sys
I = lambda : sys.stdin.readline()
mod = 10 ** 9 + 7

def mindiff(a):
	b = a[:]
	b.sort()
	m = 10000000000
	for i in range(len(b) - 1):
		if b[i + 1] - b[i] < m:
			m = b[i + 1] - b[i]
	return m

def lcm(a, b):
	return a * b // math.gcd(a, b)

def merge(a, b):
	i = 0
	j = 0
	c = 0
	ans = []
	while i < len(a) and j < len(b):
		if a[i] < b[j]:
			ans.append(a[i])
			i += 1
		else:
			ans.append(b[j])
			c += len(a) - i
			j += 1
	ans += a[i:]
	ans += b[j:]
	return (ans, c)

def mergesort(a):
	if len(a) == 1:
		return (a, 0)
	mid = len(a) // 2
	(left, left_inversion) = mergesort(a[:mid])
	(right, right_inversion) = mergesort(a[mid:])
	(m, c) = merge(left, right)
	c += left_inversion + right_inversion
	return (m, c)

def is_prime(num):
	if num == 1:
		return False
	if num == 2:
		return True
	if num == 3:
		return True
	if num % 2 == 0:
		return False
	if num % 3 == 0:
		return False
	t = 5
	a = 2
	while t <= int(math.sqrt(num)):
		if num % t == 0:
			return False
		t += a
		a = 6 - a
	return True

def ceil(a, b):
	if a % b == 0:
		return a // b
	else:
		return a // b + 1

def binsearch(arr, b, low, high):
	if low == high:
		return low
	if arr[math.ceil((low + high) / 2)] < b:
		return binsearch(arr, b, low, math.ceil((low + high) / 2) - 1)
	else:
		return binsearch(arr, b, math.ceil((low + high) / 2), high)

def ncr1(n, r):
	s = 1
	for i in range(min(n - r, r)):
		s *= n - i
		s %= mod
		s *= pow(i + 1, mod - 2, mod)
		s %= mod
	return s

def calc(n, m, r):
	s = 0
	for i in range(0, r + 1, 2):
		s += ncr1(n, i) * ncr1(m, i)
		s %= mod
	return s
(w, h) = map(int, input().split())
(a, b) = map(int, input().split())
d = {}
d[b] = a
(a, b) = map(int, input().split())
d[b] = a
while h > 0:
	w += h
	if h in d:
		w = max(w - d[h], 0)
	h -= 1
print(w)


(w, h) = [int(x) for x in input().split()]
(u1, d1) = [int(x) for x in input().split()]
(u2, d2) = [int(x) for x in input().split()]
while h > 0:
	w += h
	if h == d1:
		w = max(0, w - u1)
	elif h == d2:
		w = max(0, w - u2)
	h -= 1
print(w)


(w1, h1) = map(int, input().split())
(w2, h2) = map(int, input().split())
(w3, h3) = map(int, input().split())
a = [0] * (h1 + 1)
a[h2] = w2
a[h3] = w3
weight = w1
for i in range(h1, -1, -1):
	weight += i
	weight = max(weight - a[i], 0)
print(weight)


def read_nums():
	return [int(x) for x in input().split()]

def main():
	(w, h) = read_nums()
	(w1, h1) = read_nums()
	(w2, h2) = read_nums()
	for i in range(h, -1, -1):
		w += i
		if i == h1:
			w = max(w - w1, 0)
		elif i == h2:
			w = max(w - w2, 0)
	print(w)
main()


(w, h) = map(int, input().split())
(u1, h1) = map(int, input().split())
(u2, h2) = map(int, input().split())
while h != 0:
	w += h
	if h == h1:
		w -= u1
		if w < 0:
			w = 0
	elif h == h2:
		w -= u2
		if w < 0:
			w = 0
	h -= 1
print(0 if w < 0 else w)


(w, h) = map(int, input().split())
(u1, d1) = map(int, input().split())
(u2, d2) = map(int, input().split())
while True:
	if h == 0:
		break
	w += h
	if h == d1:
		w = max(0, w - u1)
	if h == d2:
		w = max(0, w - u2)
	h -= 1
print(w)


import sys
import math
get_string = lambda : sys.stdin.readline().strip()
get_int_list = lambda : list(map(int, sys.stdin.readline().strip().split()))
get_intList_from_str = lambda : list(map(int, list(sys.stdin.readline().strip())))
get_charList_from_str = lambda : list(sys.stdin.readline().strip())
get_char_list = lambda : sys.stdin.readline().strip().split()
get_int = lambda : int(sys.stdin.readline())
pt = lambda x: sys.stdout.write(str(x))
(w_snow, h_snow) = get_int_list()
(w_s1, h_s1) = get_int_list()
(w_s2, h_s2) = get_int_list()
for i in range(h_snow, -1, -1):
	w_snow += i
	if h_s1 == i:
		w_snow -= w_s1
		w_snow = max(0, w_snow)
	if h_s2 == i:
		w_snow -= w_s2
		w_snow = max(0, w_snow)
print(max(0, w_snow))


one = input()
two = input()
three = input()
one = [int(i) for i in one.split()]
two = [int(i) for i in two.split()]
three = [int(i) for i in three.split()]
w = one[0]
h = one[-1]
u1 = two[0]
d1 = two[-1]
u2 = three[0]
d2 = three[-1]
path = list(range(h + 1))
path.reverse()
for heigh in path:
	w += heigh
	if heigh == d1:
		w -= u1
	elif heigh == d2:
		w -= u2
	if w < 0:
		w = 0
print(w)


(w_start, h_start) = map(int, input().split())
(w_stone1, h1) = map(int, input().split())
(w_stone2, h2) = map(int, input().split())
w = w_start
for h_cur in range(h_start, -1, -1):
	w += h_cur
	if h_cur == h1:
		w -= w_stone1
	if h_cur == h2:
		w -= w_stone2
	if w < 0:
		w = 0
print(w)


(w, h) = map(int, input().split())
s1 = list(map(int, input().split()))
s2 = list(map(int, input().split()))
for ht in reversed(range(h + 1)):
	w += ht
	if ht == s1[1]:
		w -= s1[0]
		w = max(w, 0)
	if ht == s2[1]:
		w -= s2[0]
		w = max(w, 0)
print(w)


(w, h) = map(int, input().split())
(u1, h1) = map(int, input().split())
(u2, h2) = map(int, input().split())
take = 0
for i in range(h, -1, -1):
	w += i
	if i == h1:
		w -= u1
	elif i == h2:
		w -= u2
	if w <= 0:
		w = 0
print(w)


(w_ball, h_ball) = map(int, input().split(' '))
(w_x, h_x) = map(int, input().split(' '))
(w_y, h_y) = map(int, input().split(' '))
for i in range(h_ball, 0, -1):
	w_ball = w_ball + i
	if i == h_x:
		w_ball -= w_x
	elif i == h_y:
		w_ball -= w_y
	if w_ball < 0:
		w_ball = 0
print(w_ball)


import math

def modi(a, m):
	return pow(a, m - 2, m)

def __starting_point():
	(w, h) = [int(x) for x in input().split()]
	(u1, d1) = [int(x) for x in input().split()]
	(u2, d2) = [int(x) for x in input().split()]
	for i in range(h):
		w += h - i
		if h - i == d1:
			w -= u1
			w = max(0, w)
		if h - i == d2:
			w -= u2
			w = max(0, w)
	print(w)
__starting_point()


(w, h) = map(int, input().split(' '))
(first_weight, first_height) = map(int, input().split(' '))
(second_weight, second_height) = map(int, input().split(' '))
for i in range(h):
	w += h
	if h == first_height:
		w -= first_weight
	if h == second_height:
		w -= second_weight
	w = max(w, 0)
	h -= 1
if w < 0:
	print(0)
else:
	print(w)


def f(l, w, h):
	for i in l:
		w += (h * (h + 1) - i[1] * (i[1] - 1)) / 2
		w = max(0, w - i[0])
		h = i[1] - 1
	if h != 0:
		w += h * (h + 1) / 2
	return int(w)
(w, h) = map(int, input().split())
(w2, h2) = map(int, input().split())
(w3, h3) = map(int, input().split())
l = []
if h3 > h2:
	l.extend([[w3, h3], [w2, h2]])
else:
	l.extend([[w2, h2], [w3, h3]])
print(f(l, w, h))


(w, h) = map(int, input().split())
(u, d) = map(int, input().split())
(v, e) = map(int, input().split())
while h:
	w += h
	if h == d:
		w = max(0, w - u)
	if h == e:
		w = max(0, w - v)
	h -= 1
print(w)


def rli():
	return list(map(int, input().split()))

def main():
	(w, h) = rli()
	stones = []
	for i in range(2):
		(u, d) = rli()
		stones.append((d, u))
	stones.sort()
	stones.reverse()
	stones.append((float('inf'), 0))
	now = 0
	si = 0
	while h:
		w += h
		if h == stones[si][0]:
			w -= stones[si][1]
			si += 1
		if w < 0:
			w = 0
		now += 1
		h -= 1
	print(w)
main()


(w, h) = map(int, input().strip().split())
(u1, d1) = map(int, input().strip().split())
(u2, d2) = map(int, input().strip().split())
s = w
for i in range(h, 0, -1):
	s += i
	if i == d1:
		s -= u1
	if i == d2:
		s -= u2
	if s < 0:
		s = 0
print(s)


(w, h) = map(int, input().split())
(v1, d1) = map(int, input().split())
(v2, d2) = map(int, input().split())
while h != 0:
	w += h
	if h == d1:
		w -= v1
		if w < 0:
			w = 0
	if h == d2:
		w -= v2
		if w < 0:
			w = 0
	h -= 1
print(w)


in1 = list(map(int, input().split()))
in2 = list(map(int, input().split()))
in3 = list(map(int, input().split()))
w = in1[0]
h = in1[1]
d1 = max(in2[1], in3[1])
d2 = min(in2[1], in3[1])
if in2[1] > in3[1]:
	u1 = in2[0]
	u2 = in3[0]
	d1 = in2[1]
	d2 = in3[1]
else:
	u2 = in2[0]
	u1 = in3[0]
	d1 = in3[1]
	d2 = in2[1]
while h >= d1:
	w = w + h
	h = h - 1
w = w - u1 if w - u1 > 0 else 0
while h >= d2:
	w = w + h
	h = h - 1
w = w - u2 if w - u2 > 0 else 0
while h > 0:
	w = w + h
	h = h - 1
print(w)


(w, h) = map(int, input().split())
(u1, d1) = map(int, input().split())
(u2, d2) = map(int, input().split())
ans = w
i = h
while i > 0:
	ans += i
	if i == d1:
		ans -= u1
	if i == d2:
		ans -= u2
	i -= 1
	if ans < 0:
		ans = 0
print(ans)


import sys
from collections import defaultdict

def solve(io):
	W = io.read_int()
	H = io.read_int()
	U1 = io.read_int()
	D1 = io.read_int()
	U2 = io.read_int()
	D2 = io.read_int()
	drop = defaultdict(lambda : 0)
	drop[D1] += U1
	drop[D2] += U2
	weight = W
	for i in range(H, -1, -1):
		weight = max(0, weight + i - drop[i])
	io.println(weight)

class IO:
	in_stream = None
	out_stream = None
	raw = ''
	buf = []
	pos = 0

	def __init__(self, input_stream, output_stream):
		self.in_stream = input_stream
		self.out_stream = output_stream

	def read_to_buffer(self):
		self.raw = self.in_stream.readline().rstrip('\n')
		self.buf = self.raw.split()
		self.pos = 0

	def read_string(self):
		while self.pos == len(self.buf):
			self.read_to_buffer()
		ans = self.buf[self.pos]
		self.pos += 1
		return ans

	def read_int(self):
		return int(self.read_string())

	def read_float(self):
		return float(self.read_string())

	def read_string_array(self, N, offset=0):
		arr = [None] * offset
		for _ in range(0, N):
			arr.append(self.read_string())
		return arr

	def read_int_array(self, N, offset=0):
		arr = [None] * offset
		for _ in range(0, N):
			arr.append(self.read_int())
		return arr

	def read_float_array(self, N, offset=0):
		arr = [None] * offset
		for _ in range(0, N):
			arr.append(self.read_float())
		return arr

	def read_line(self):
		while self.pos == len(self.buf):
			self.read_to_buffer()
		if self.pos > 0:
			raise ValueError('Cannot call read_line in the middle of a line.')
		self.pos = len(self.buf)
		return self.raw

	def print(self, *args):
		self.out_stream.write(' '.join([str(x) for x in args]))

	def println(self, *args):
		self.print(*args)
		self.print('\n')

	def println_array(self, arr, sep=' '):
		self.println(sep.join((str(x) for x in arr)))

	def flush_output(self):
		self.out_stream.flush()
pythonIO = IO(sys.stdin, sys.stdout)
solve(pythonIO)
pythonIO.flush_output()


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
(e, f) = map(int, input().split())
if d > f:
	r = d
	s = f
	t = c
	u = e
else:
	r = f
	s = d
	t = e
	u = c
x = b
while x >= 1:
	a = a + x
	if x == r:
		a = max(a - t, 0)
	if x == s:
		a = max(a - u, 0)
	x = x - 1
print(a)


(w, k) = map(int, input().split())
(a, c) = map(int, input().split())
(b, d) = map(int, input().split())
for i in range(k, -1, -1):
	w += i
	if c == i:
		w -= a
	if d == i:
		w -= b
	if w < 0:
		w = 0
print(w)


(w, h) = map(int, input().split())
ans = w
(a1, b1) = map(int, input().split())
(a2, b2) = map(int, input().split())
while h > 0:
	ans += h
	if h == b1:
		ans -= a1
	elif h == b2:
		ans -= a2
	if ans < 0:
		ans = 0
	h -= 1
print(ans)


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
(e, f) = map(int, input().split())
if d > f:
	i = b
	while i >= d:
		a += i
		i -= 1
	if a - c > 0:
		a -= c
	else:
		a = 0
	while i >= f:
		a += i
		i -= 1
	if a - e > 0:
		a -= e
	else:
		a = 0
	a = a + f * (f - 1) // 2
	print(a)
else:
	i = b
	while i >= f:
		a += i
		i -= 1
	if a - e > 0:
		a -= e
	else:
		a = 0
	while i >= d:
		a += i
		i -= 1
	if a - c > 0:
		a -= c
	else:
		a = 0
	a = a + d * (d - 1) // 2
	print(a)


def go():
	(w, h) = [int(i) for i in input().split(' ')]
	(w1, h1) = [int(i) for i in input().split(' ')]
	(w2, h2) = [int(i) for i in input().split(' ')]
	for i in range(h, -1, -1):
		w += i
		if i == h1:
			w = max(0, w - w1)
		if i == h2:
			w = max(0, w - w2)
	return w
print(go())


(w, h) = list(map(int, input().split()))
(p, a) = list(map(int, input().split()))
(q, b) = list(map(int, input().split()))
w += h
while h != 0:
	if h == a:
		w -= p
		if w < 0:
			w = 0
		else:
			pass
	elif h == b:
		w -= q
		if w < 0:
			w = 0
	else:
		pass
	h -= 1
	w += h
print(w)


(w1, h1) = map(int, input().split())
(w2, h2) = map(int, input().split())
(w3, h3) = map(int, input().split())
for i in range(h1, 0, -1):
	w1 += i
	if i == h2:
		w1 -= w2
	if i == h3:
		w1 -= w3
	if w1 < 0:
		w1 = 0
print(w1)


s = input().split()
(w, h) = (int(s[0]), int(s[1]))
s = input().split()
(u1, d1) = (int(s[0]), int(s[1]))
s = input().split()
(u2, d2) = (int(s[0]), int(s[1]))
cur_w = w
while h > 0:
	cur_w += h
	if h == d1:
		cur_w -= u1
	elif h == d2:
		cur_w -= u2
	if cur_w < 0:
		cur_w = 0
	h -= 1
print(cur_w)


from sys import stdin
(w, h) = map(int, stdin.readline().split())
(u1, d1) = map(int, stdin.readline().split())
(u2, d2) = map(int, stdin.readline().split())
while h > 0:
	w += h
	if h == d1:
		w -= u1
		if w < 0:
			w = 0
	if h == d2:
		w -= u2
		if w < 0:
			w = 0
	h -= 1
print(w)


(w, h) = map(int, input().split())
(u1, d1) = map(int, input().split())
(u2, d2) = map(int, input().split())
x = w
while h > 0:
	x += h
	if d1 == h:
		x = max(x - u1, 0)
	if d2 == h:
		x = max(x - u2, 0)
	h -= 1
print(x)


(w, h) = [int(i) for i in input().split()]
(m1, h1) = [int(i) for i in input().split()]
(m2, h2) = [int(i) for i in input().split()]
while h != 0:
	w += h
	if h == h1:
		w = max(0, w - m1)
	if h == h2:
		w = max(0, w - m2)
	h -= 1
print(w)


s1 = input()
s1 = s1.split()
w = int(s1[0])
h = int(s1[1])
s2 = input()
s2 = s2.split()
u1 = int(s2[0])
d1 = int(s2[1])
s3 = input()
s3 = s3.split()
u2 = int(s3[0])
d2 = int(s3[1])
while h != 0:
	if h == d1:
		w = w - u1
	elif h == d2:
		w = w - u2
	w += h
	if w < 0:
		w = 0
	h -= 1
print(w)


import sys
import math

def read_int():
	return int(input().strip())

def read_int_list():
	return list(map(int, input().strip().split()))

def read_string():
	return input().strip()

def read_string_list(delim=' '):
	return input().strip().split(delim)
[w, h] = read_int_list()
[u1, h1] = read_int_list()
[u2, h2] = read_int_list()
while h > 0:
	w += h
	if h == h1:
		w -= u1
	elif h == h2:
		w -= u2
	else:
		pass
	if w < 0:
		w = 0
	h -= 1
print(w)


(w, h) = map(int, input().split())
(w1, h1) = map(int, input().split())
(w2, h2) = map(int, input().split())
while h > 0:
	w += h
	if h == h1:
		w = max(0, w - w1)
	if h == h2:
		w = max(w - w2, 0)
	h -= 1
print(w)


def main():
	[weight, height] = [int(_) for _ in input().split()]
	[weight_stone1, height_stone1] = [int(_) for _ in input().split()]
	[weight_stone2, height_stone2] = [int(_) for _ in input().split()]
	while height > 0:
		weight += height
		if height == height_stone1:
			weight -= weight_stone1
		elif height == height_stone2:
			weight -= weight_stone2
		height -= 1
		weight = max(0, weight)
	print(weight)
main()


ball = [int(x) for x in input().split()]
stone1 = [int(x) for x in input().split()]
stone2 = [int(x) for x in input().split()]
(montana, peso) = (ball[1], ball[0])
for x in range(montana):
	i = montana - x
	peso += i
	if i == stone1[1]:
		peso -= stone1[0]
		if peso < 0:
			peso = 0
	if i == stone2[1]:
		peso -= stone2[0]
		if peso < 0:
			peso = 0
print(peso)


l = [tuple(map(int, input().split())) for i in range(3)]
h = l[0][0]
for i in range(l[0][1], 0, -1):
	h += i
	if i == l[1][1]:
		h = max(0, h - l[1][0])
	if i == l[2][1]:
		h = max(0, h - l[2][0])
print(h)


(w, h) = map(int, input().split())
(w1, h1) = map(int, input().split())
(w2, h2) = map(int, input().split())
stones = sorted([(h1, w1), (h2, w2)], reverse=True)
for (sh, sw) in stones:
	w += (h + sh) * (h - sh + 1) // 2
	w = max(0, w - sw)
	h = sh - 1
if h > 0:
	w += (h + 1) * h // 2
print(w)


(w, h) = map(int, input().split())
(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
ans = w
for q in range(h, 0, -1):
	ans += q
	if q == b:
		ans -= a
		ans = max(ans, 0)
	if q == d:
		ans -= c
		ans = max(ans, 0)
print(ans)


(w, h) = map(int, input().split())
(u1, d1) = map(int, input().split())
(u2, d2) = map(int, input().split())
if d1 > d2:
	(d1, d2) = (d2, d1)
	(u1, u2) = (u2, u1)
while h >= d2:
	w += h
	h -= 1
w = w - u2 if w - u2 > 0 else 0
while h >= d1:
	w += h
	h -= 1
w = w - u1 if w - u1 > 0 else 0
print(w + sum(range(h, 0, -1)))


values = []
for i in range(3):
	for value in input().split(' '):
		values.append(int(value))
w = values[0]
h = values[1]
while True:
	w = w + h
	if h == 0:
		break
	if h == values[3]:
		w = w - values[2]
	if h == values[5]:
		w = w - values[4]
	if w < 0:
		w = 0
	h = h - 1
print(w)


def main():
	(ball_weight, ball_height) = map(int, input().split())
	(stone_weights, stone_heights) = ([0] * 2, [0] * 2)
	for i in range(2):
		(stone_weights[i], stone_heights[i]) = map(int, input().split())
	ret = ball_weight
	for curr_height in range(ball_height, -1, -1):
		ret += curr_height
		for i in range(2):
			if curr_height == stone_heights[i]:
				ret = max(0, ret - stone_weights[i])
	print(ret)
main()


import sys
data = sys.stdin.read().splitlines()
(u, h) = map(int, data[0].split())
(u1, d1) = map(int, data[1].split())
(u2, d2) = map(int, data[2].split())
while h != 0:
	u += h
	if h == d1:
		u = max(0, u - u1)
	elif h == d2:
		u = max(0, u - u2)
	h -= 1
print(u)


(n, k) = input().split()
(n, k) = (int(n), int(k))
(u1, d1) = input().split()
(u2, d2) = input().split()
(u1, d1, u2, d2) = (int(u1), int(d1), int(u2), int(d2))
for i in range(k, 0, -1):
	if i == d1:
		n -= u1
	if i == d2:
		n -= u2
	n += i
	if n < 0:
		n = 0
		continue
print(n)


(w, h) = map(int, input().split())
(u1, d1) = map(int, input().split())
(u2, d2) = map(int, input().split())
if d1 > d2:
	c = w + int(h * (h + 1) / 2) - int(d1 * (d1 + 1) / 2) + d1 - u1
	if c < 0:
		c = 0
	c = c + int((d1 - 1) * d1 / 2) - int(d2 * (d2 - 1) / 2) - u2
	if c < 0:
		c = 0
	c = c + int(d2 * (d2 - 1) / 2)
elif d2 > d1:
	c = w + int(h * (h + 1) / 2) - int(d2 * (d2 + 1) / 2) + d2 - u2
	if c < 0:
		c = 0
	c = c + int((d2 - 1) * d2 / 2) - int(d1 * (d1 - 1) / 2) - u1
	if c < 0:
		c = 0
	c = c + int(d1 * (d1 - 1) / 2)
print(c)


def read(type=1):
	if type:
		file = open('input.dat', 'r')
		line1 = list(map(int, file.readline().split()))
		line2 = list(map(int, file.readline().split()))
		line3 = list(map(int, file.readline().split()))
		file.close()
	else:
		line1 = list(map(int, input().strip().split()))
		line2 = list(map(int, input().strip().split()))
		line3 = list(map(int, input().strip().split()))
	return (line1, line2, line3)

def solve():
	h = a[1]
	w = a[0]
	while h > 0:
		w += h
		if h == b[1]:
			w -= b[0]
		if h == c[1]:
			w -= c[0]
		if w < 0:
			w = 0
		h -= 1
	return w
(a, b, c) = read(0)
sol = solve()
print(sol)


def ans(wh, u1d1, u2d2):
	(w, h) = (wh[0], wh[1])
	(u1, d1) = (u1d1[0], u1d1[1])
	(u2, d2) = (u2d2[0], u2d2[1])
	while h >= 0:
		w += h
		if h == d1:
			w -= u1
		if h == d2:
			w -= u2
		h -= 1
		if w < 0:
			w = 0
	return w
wh = list(map(int, input().strip().split()))
u1d1 = list(map(int, input().strip().split()))
u2d2 = list(map(int, input().strip().split()))
print(ans(wh, u1d1, u2d2))


(w, h) = tuple(map(int, input().split()))
(u1, d1) = tuple(map(int, input().split()))
(u2, d2) = tuple(map(int, input().split()))
if d1 < d2:
	(u1, d1, u2, d2) = (u2, d2, u1, d1)
while h != 0:
	if h == d1 or h == d2:
		w += h
		if h == d1:
			w -= u1
		if h == d2:
			w -= u2
		if w < 0:
			w = 0
		h -= 1
	else:
		w += h
		h -= 1
if w < 0:
	print(0)
else:
	print(w)


import math
(w, h) = map(int, input().split(' '))
(a, b) = map(int, input().split(' '))
(c, d) = map(int, input().split(' '))
s = w
if b < d:
	cn = b
	b = d
	d = cn
	cn = c
	c = a
	a = cn
for i in range(h, -1, -1):
	s += i
	if i == b:
		s -= a
		if s < 0:
			s = 0
	if i == d:
		s -= c
		if s < 0:
			s = 0
print(s)


(w, h) = map(int, input().split())
(aWeight, aHeight) = map(int, input().split())
(bWeight, bHeight) = map(int, input().split())
while h > 0:
	w += h
	if h == aHeight:
		w -= aWeight
	elif h == bHeight:
		w -= bWeight
	if w < 0:
		w = 0
	h -= 1
print(w)


(w, h) = map(int, input().split())
(w1, h1) = map(int, input().split())
(w2, h2) = map(int, input().split())
l = []
c = w
l.append([h1, w1])
l.append([h2, w2])
l.sort(reverse=True)
n1 = h - l[0][0] + 1
c += n1 * (l[0][0] + h) // 2
c -= l[0][1]
if c < 0:
	c = 0
n1 = l[0][0] - l[1][0]
c += n1 * (l[1][0] + l[0][0] - 1) // 2
c -= l[1][1]
if c < 0:
	c = 0
n1 = l[1][0] - 1
c += n1 * (l[1][0] - 1 + 1) // 2
print(max(c, 0))


(w, h) = map(int, input().split())
(u, d) = map(int, input().split())
(u2, d2) = map(int, input().split())
for i in range(h, 0, -1):
	w += i
	if i == d:
		if w < u:
			w = 0
		else:
			w = w - u
	if i == d2:
		if w < u2:
			w = 0
		else:
			w = w - u2
print(w)


x = input().split(' ')
w = int(x[0])
h = int(x[1])
y = input().split(' ')
u1 = int(y[0])
d1 = int(y[1])
z = input().split(' ')
u2 = int(z[0])
d2 = int(z[1])

def snowballWeigh(w, h, u1, d1, u2, d2):
	i = h
	while True:
		w = w + i
		if i == d1:
			w = w - u1
			if w < 0:
				w = 0
		elif i == d2:
			w = w - u2
			if w < 0:
				w = 0
		i = i - 1
		if i == 0:
			return w
			break
print(snowballWeigh(w, h, u1, d1, u2, d2))


(snoww, snowh) = [int(x) for x in input().split()]
(stone1w, stone1h) = [int(x) for x in input().split()]
(stone2w, stone2h) = [int(x) for x in input().split()]
for i in range(snowh, 0, -1):
	snoww += i
	if i == stone1h:
		snoww -= stone1w
	elif i == stone2h:
		snoww -= stone2w
	if snoww < 0:
		snoww = 0
print(snoww)


input1 = input().split(' ')
w = int(input1[0])
h = int(input1[1])
stone1 = input().split(' ')
u1 = int(stone1[0])
d1 = int(stone1[1])
stone2 = input().split(' ')
u2 = int(stone2[0])
d2 = int(stone2[1])
while True:
	w = w + h
	if h == 0:
		break
	if h == d1:
		w = w - u1
	if h == d2:
		w = w - u2
	if w < 0:
		w = 0
	h = h - 1
print(w)


(w0, h0) = map(int, input().split())
(w1, h1) = map(int, input().split())
(w2, h2) = map(int, input().split())
if h1 < h2:
	(w1, h1, w2, h2) = (w2, h2, w1, h1)
print(max(max(w0 + (h0 + h1) * (h0 - h1 + 1) // 2 - w1, 0) + (h1 + h2 - 1) * (h1 - h2) // 2 - w2, 0) + h2 * (h2 - 1) // 2)


def calculate(w, h, u1, d1, u2, d2):
	sq = lambda x: int(x * (x + 1) / 2)
	before_1_stone = w + sq(h) - sq(d1 - 1)
	before_2_stone = max(before_1_stone - u1, 0) + sq(d1 - 1) - sq(d2 - 1)
	finish = max(before_2_stone - u2, 0) + sq(d2 - 1)
	return finish
(w, h) = map(int, input().split())
(u1, d1) = map(int, input().split())
(u2, d2) = map(int, input().split())
if d1 < d2:
	(d1, d2) = (d2, d1)
	(u1, u2) = (u2, u1)
print(calculate(w, h, u1, d1, u2, d2))


v1 = list(map(int, input().split()))
v2 = list(map(int, input().split()))
v3 = list(map(int, input().split()))
w = v1[0]
for i in range(v1[1], -1, -1):
	w = w + i
	if i == v2[1]:
		w = w - v2[0]
	if i == v3[1]:
		w = w - v3[0]
	if w < 0:
		w = 0
print(w)


(w, h) = map(int, input().split())
(u1, d1) = map(int, input().split())
(u2, d2) = map(int, input().split())
weight = w
hill = [i for i in range(147)]
hill[d1] -= u1
hill[d2] -= u2
for height in range(h, 0, -1):
	weight = max(0, weight + hill[height])
print(weight)


(ans, h) = map(int, input().split())
(u1, d1) = map(int, input().split())
(u2, d2) = map(int, input().split())
for i in range(h, 0, -1):
	ans += i
	if i == d1:
		ans = max(0, ans - u1)
	elif i == d2:
		ans = max(0, ans - u2)
print(ans)


import sys
import math
from collections import defaultdict, deque
input = sys.stdin.readline

def inar():
	return [int(el) for el in input().split()]

def main():
	(a, b) = inar()
	(c, d) = inar()
	(e, f) = inar()
	for i in range(b, 0, -1):
		a += i
		if i == d:
			a -= c
		if i == f:
			a -= e
		a = max(0, a)
	print(a)
main()


from sys import stdin
from collections import Counter
from math import sqrt, factorial, log10, log, floor, ceil
from itertools import permutations, combinations, combinations_with_replacement
input = stdin.readline

def li():
	return list(map(int, input().split()))

def lis():
	return list(map(str, input().split()))

def mp():
	return map(int, input().split())

def inp():
	return int(input())

def inps():
	return str(input().strip())

def pr(n):
	return stdout.write(str(n) + '\n')
INF = float('inf')

def solve():
	(w, h) = mp()
	(w1, h1) = mp()
	(w2, h2) = mp()
	while h > 0:
		w += h
		if h == h1:
			w -= w1
		if h == h2:
			w -= w2
		if w < 0:
			w = 0
		h -= 1
	print(w)
t = 1
for i in range(t):
	solve()


(w, h) = map(int, input().split(' '))
(u, d) = map(int, input().split(' '))
(u1, d1) = map(int, input().split(' '))
if d1 > d:
	s = h
	while s > d1:
		w += s
		s += -1
	if s == d1:
		w = w - u1
		w += d1
	if w < 0:
		w = 0
	s = d1 - 1
	while s > d:
		w += s
		s += -1
	if s == d:
		w += d
		w = w - u
		s += -1
	if w < 0:
		w = 0
	while s > 0:
		w = w + s
		s += -1
else:
	(e, f) = (u1, d1)
	(u1, d1) = (u, d)
	(u, d) = (e, f)
	s = h
	while s > d1:
		w += s
		s += -1
	if s == d1:
		w = w - u1
		w += d1
	if w < 0:
		w = 0
	s = d1 - 1
	while s > d:
		w += s
		s += -1
	if s == d:
		w = w - u
		w += d
		s += -1
	if w < 0:
		w = 0
	while s > 0:
		w = w + s
		s += -1
print(w)


(sw, sh) = map(int, input().split())
(s1w, s1h) = map(int, input().split())
(s2w, s2h) = map(int, input().split())
z = 0
x = 0
y = 0
w = 0
if s1h > s2h:
	z = s1h
	y = s1w
	x = s2h
	w = s2w
else:
	z = s2h
	y = s2w
	x = s1h
	w = s1w
for j in range(sh, z - 1, -1):
	sw = sw + j
sw = sw - y
if sw < 0:
	sw = 0
for j in range(z - 1, x - 1, -1):
	sw = sw + j
sw = sw - w
if sw < 0:
	sw = 0
for j in range(x - 1, -1, -1):
	sw = sw + j
print(sw)


(w, h) = map(int, input().split())
(v1, h1) = map(int, input().split())
(v2, h2) = map(int, input().split())
for i in range(h, 0, -1):
	w = w + i
	if i == h1:
		w = w - v1
	if i == h2:
		w = w - v2
	if w < 0:
		w = 0
print(w)


(v, h) = list(map(int, input().split()))
(v1, h1) = list(map(int, input().split()))
(v2, h2) = list(map(int, input().split()))
v += h
if max(h1, h2) == h1:
	while h > h1:
		h -= 1
		v += h
	v -= v1
	if v < 0:
		v = 0
	while h > h2:
		h -= 1
		v += h
	v -= v2
	if v < 0:
		v = 0
	while h > 0:
		h -= 1
		v += h
	print(v)
else:
	while h > h2:
		h -= 1
		v += h
	v -= v2
	if v < 0:
		v = 0
	while h > h1:
		h -= 1
		v += h
	v -= v1
	if v < 0:
		v = 0
	while h > 0:
		h -= 1
		v += h
	print(v)


prime = 998244353
t = 1
for test in range(t):
	(w, h) = map(int, input().split())
	(u1, d1) = map(int, input().split())
	(u2, d2) = map(int, input().split())
	for i in range(h, -1, -1):
		w += i
		if i == d1:
			w = max(0, w - u1)
		if i == d2:
			w = max(0, w - u2)
	print(w)


(w, h) = map(int, input().split())
(u1, d1) = map(int, input().split())
(u2, d2) = map(int, input().split())
for i in reversed(range(0, h + 1)):
	w += i
	if i == d1:
		w -= min(w, u1)
	if i == d2:
		w -= min(w, u2)
print(w)


import sys
import math
(w, h) = map(int, sys.stdin.readline().split())
(w1, h1) = map(int, sys.stdin.readline().split())
(w2, h2) = map(int, sys.stdin.readline().split())
for i in range(h, -1, -1):
	w += i
	w = max(w, 0)
	if i == h1:
		w -= w1
	if i == h2:
		w -= w2
	w = max(w, 0)
print(w)


(w, h) = map(int, input().split())
S1 = list(map(int, input().split()))
S2 = list(map(int, input().split()))
if S2[1] > S1[1]:
	k = list(S1)
	S1 = S2
	S2 = k

def c(wb, h, d, ws):
	wb += (h + d) * (h - d + 1) // 2
	if wb <= ws:
		wb = 0
	else:
		wb -= ws
	return wb
w = c(w, h, S1[1], S1[0])
w = c(w, S1[1] - 1, S2[1], S2[0])
if S2[0] != 0:
	w = c(w, S2[1] - 1, 0, 0)
print(w)


(w, h) = map(int, input().split())
(u_1, d_1) = map(int, input().split())
(u_2, d_2) = map(int, input().split())
for i in range(h):
	current_height = h - i
	w += current_height
	if current_height == d_1:
		w -= u_1
		if w < 0:
			w = 0
	if current_height == d_2:
		w -= u_2
		if w < 0:
			w = 0
print(w)


(w, h) = map(int, input().strip().split(' '))
(w1, h1) = map(int, input().strip().split(' '))
(w2, h2) = map(int, input().strip().split(' '))
if h1 > h2:
	s1w = w1
	s1h = h1
	s2w = w2
	s2h = h2
else:
	s1w = w2
	s1h = h2
	s2w = w1
	s2h = h1
for i in range(h, -1, -1):
	w += i
	if i == s1h:
		w -= s1w
		if w < 0:
			w = 0
	if i == s2h:
		w -= s2w
		if w < 0:
			w = 0
print(w)


(w, h) = map(int, input().split())
(a1, b1) = map(int, input().split())
(a2, b2) = map(int, input().split())
i = h
while h >= max(b1, b2):
	w += h
	if h == max(b1, b2):
		if h == b1:
			w -= a1
			if w < 0:
				w = 0
		else:
			w -= a2
			if w < 0:
				w = 0
	h -= 1
while h >= min(b1, b2):
	w += h
	if h == min(b1, b2):
		if h == b1:
			w -= a1
			if w < 0:
				w = 0
		else:
			w -= a2
			if w < 0:
				w = 0
	h -= 1
while h > 0:
	w += h
	h -= 1
print(w)


(w, h) = [int(s) for s in input().split()]
(u1, d1) = [int(s) for s in input().split()]
(u2, d2) = [int(s) for s in input().split()]
F = [0] * (h + 2)
F[d1] = u1
F[d2] = u2
F[-1] = w
for i in range(h, -1, -1):
	F[i] = F[i + 1] - F[i] + i
	if F[i] < 0:
		F[i] = 0
print(F[0])


snowball = [int(x) for x in input().split(' ')]
stone1 = [int(x) for x in input().split(' ')]
stone2 = [int(x) for x in input().split(' ')]
while snowball[1] > 0:
	snowball[0] += snowball[1]
	if snowball[1] == stone1[1]:
		snowball[0] = max(0, snowball[0] - stone1[0])
	elif snowball[1] == stone2[1]:
		snowball[0] = max(0, snowball[0] - stone2[0])
	snowball[1] -= 1
print(snowball[0])


(w, h) = list(map(int, input().strip().split()))
(s1w, s1h) = list(map(int, input().strip().split()))
(s2w, s2h) = list(map(int, input().strip().split()))
for i in reversed(range(h + 1)):
	w += i
	if i == s1h:
		w -= s1w
	if i == s2h:
		w -= s2w
	if w < 0:
		w = 0
print(w)


(w, d) = input().split(' ')
(w, d) = (int(w), int(d))
(u1, d1) = input().split(' ')
(u1, d1) = (int(u1), int(d1))
(u2, d2) = input().split(' ')
(u2, d2) = (int(u2), int(d2))
if d2 > d1:
	td = d1
	d1 = d2
	d2 = td
	tu = u1
	u1 = u2
	u2 = tu
for i in range(d1, d + 1):
	w += i
w -= u1
if w <= 0:
	w = 0
for i in range(d2, d1):
	w += i
w -= u2
if w <= 0:
	w = 0
for i in range(d2):
	w += i
print(w)


(w, h) = map(int, input().split())
(a, b) = map(int, input().split())
(x, y) = map(int, input().split())
while h >= 0:
	w = w + h
	if h == b:
		w = w - a
	if h == y:
		w = w - x
	h -= 1
	if w < 0:
		w = 0
print(w)


(x, y) = map(int, input().split())
(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
if d > b:
	su = 0
	for i in range(d, y + 1):
		su += i
	x = x + su
	x = x - c
	x = max(x, 0)
	su = 0
	for i in range(b, d):
		su += i
	x = x + su
	x = x - a
	x = max(x, 0)
	su = 0
	for i in range(b):
		su = su + i
	x = x + su
	print(x)
else:
	su = 0
	for i in range(b, y + 1):
		su += i
	x = x + su
	x = x - a
	x = max(x, 0)
	su = 0
	for i in range(d, b):
		su += i
	x = x + su
	x = x - c
	x = max(x, 0)
	su = 0
	for i in range(d):
		su = su + i
	x = x + su
	print(x)


(w, h) = map(int, input().split())
(we1, ch1) = map(int, input().split())
(we2, ch2) = map(int, input().split())
h1 = max(ch1, ch2)
h2 = min(ch1, ch2)
if h1 == ch1:
	w1 = we1
	w2 = we2
else:
	w1 = we2
	w2 = we1
w += h * (h + 1) / 2 - h1 * (h1 - 1) / 2
w = max(w - w1, 0)
w += h1 * (h1 - 1) / 2 - h2 * (h2 - 1) / 2
w = max(w - w2, 0)
w += h2 * (h2 - 1) / 2
print(int(w))


def get_weight(start_weight, height, rock1_weight, rock1_height, rock2_weight, rock2_height):
	snow_weight = start_weight
	while height > 0:
		snow_weight += height
		if height == rock1_height:
			snow_weight -= rock1_weight
		if height == rock2_height:
			snow_weight -= rock2_weight
		if snow_weight < 0:
			snow_weight = 0
		height -= 1
	return snow_weight
(w, h) = list(map(int, input().strip().split()))
(u1, d1) = list(map(int, input().strip().split()))
(u2, d2) = list(map(int, input().strip().split()))
print(get_weight(w, h, u1, d1, u2, d2))


import sys
inf = float('inf')
mod = 1000000007

def fast_io():
	sys.stdin = stream(sys.stdin.read())
	sys.stdout = stream()
	register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))

def get_array():
	return list(map(int, sys.stdin.readline().split()))

def get_ints():
	return map(int, sys.stdin.readline().split())

def input():
	return sys.stdin.readline()

def main():
	(w, h) = get_ints()
	(u1, d1) = get_ints()
	(u2, d2) = get_ints()
	for i in range(h, -1, -1):
		w += i
		if d1 == i:
			w -= u1
		if w < 0:
			w = 0
		if d2 == i:
			w -= u2
		if w < 0:
			w = 0
	print(w)
main()


R = lambda : map(int, input().split())
(w, h) = R()
(u1, d1) = R()
(u2, d2) = R()
while h > 0:
	w += h
	if h == d1:
		w -= u1
	if w < 0:
		w = 0
	if h == d2:
		w -= u2
	if w < 0:
		w = 0
	h -= 1
print(w)


(w0, h0) = input().split()
(u1, h1) = input().split()
(u2, h2) = input().split()
(w0, h0, u1, h1, u2, h2) = (int(w0), int(h0), int(u1), int(h1), int(u2), int(h2))
while h0 != 0:
	w0 = w0 + h0
	if h0 == h1 and h1 != h2:
		w0 = w0 - u1
	if h0 == h2 and h1 != h2:
		w0 = w0 - u2
	if w0 < 0:
		w0 = 0
	h0 = h0 - 1
print(w0)


o = input().rstrip().split(' ')
p = input().rstrip().split(' ')
q = input().rstrip().split(' ')
a = int(o[0])
h = int(o[1])
while h != 0:
	a = a + h
	if h == int(p[1]):
		a = a - int(p[0])
		if a < 0:
			a = 0
	elif h == int(q[1]):
		a -= int(q[0])
		if a < 0:
			a = 0
	h -= 1
print(a)


(weight, height) = (int(k) for k in input().split(' '))
(stone1_Weight, stone1_height) = (int(k) for k in input().split(' '))
(stone2_Weight, stone2_height) = (int(k) for k in input().split(' '))
while height > 0:
	weight += height
	if height == stone1_height:
		weight -= stone1_Weight
		if weight < 0:
			weight = 0
		stone1_height = -1
	elif height == stone2_height:
		weight -= stone2_Weight
		if weight < 0:
			weight = 0
		stone2_height = -1
	height -= 1
print(weight)


(w, h) = [int(s) for s in input().split(' ')]
(u1, d1) = [int(s) for s in input().split(' ')]
(u2, d2) = [int(s) for s in input().split(' ')]
if d1 > d2:
	for i in range(h, d1 - 1, -1):
		w += i
	w = max(0, w - u1)
	for i in range(d1 - 1, d2 - 1, -1):
		w += i
	w = max(0, w - u2)
	for i in range(d2 - 1, 0, -1):
		w += i
else:
	for i in range(h, d2 - 1, -1):
		w += i
	w = max(0, w - u2)
	for i in range(d2 - 1, d1 - 1, -1):
		w += i
	w = max(0, w - u1)
	for i in range(d1 - 1, 0, -1):
		w += i
print(w)


import sys
args = input().split()
w = int(args[0])
h = int(args[1])
args = input().split()
w1 = int(args[0])
h1 = int(args[1])
args = input().split()
w2 = int(args[0])
h2 = int(args[1])
for i in range(h, 0, -1):
	w += i
	if i == h1:
		w -= w1
		if w < 0:
			w = 0
	if i == h2:
		w -= w2
		if w < 0:
			w = 0
print(w)


(w, h) = map(int, input().split())
(u1, u2) = map(int, input().split())
(u3, u4) = map(int, input().split())
for i in range(1, h + 1)[::-1]:
	w += i
	if u2 == i:
		w -= u1
	if u4 == i:
		w -= u3
	w = max(w, 0)
print(w)


def A():
	snowball = input().split()
	stone1 = input().split()
	stone2 = input().split()
	(weight, height) = (int(snowball[0]), int(snowball[1]))
	(stone1_w, stone1_h) = (int(stone1[0]), int(stone1[1]))
	(stone2_w, stone2_h) = (int(stone2[0]), int(stone2[1]))
	for h in range(height, 0, -1):
		weight += h
		if h == stone1_h:
			weight = max(0, weight - stone1_w)
		if h == stone2_h:
			weight = max(0, weight - stone2_w)
	print(weight)
A()


com = [int(s) for s in input().split()]
c1 = [int(s) for s in input().split()]
c2 = [int(s) for s in input().split()]
if c1[1] > c2[1]:
	cam1 = c1
	cam2 = c2
else:
	cam1 = c2
	cam2 = c1
n = com[1] - cam1[1] + 1
com[0] = (com[1] + cam1[1]) * n // 2 + com[0] - cam1[0]
if com[0] < 0:
	com[0] = 0
n = cam1[1] - cam2[1]
com[0] = (cam1[1] - 1 + cam2[1]) * n // 2 + com[0] - cam2[0]
if com[0] < 0:
	com[0] = 0
com[0] += (cam2[1] - 1) * cam2[1] // 2
print(com[0])


(w, h) = list(map(int, input().split()))
ans = w
w = {}
(a, b) = list(map(int, input().split()))
w[b] = a
(a, b) = list(map(int, input().split()))
w[b] = a
for i in reversed(range(0, h + 1)):
	ans += i
	if i in w:
		ans -= w[i]
	ans = max(0, ans)
print(ans)


import sys

def main():
	(w, h) = map(int, input().split())
	(u1, d1) = map(int, input().split())
	(u2, d2) = map(int, input().split())
	answer = w
	while h != 0:
		if h == d1:
			answer += h - u1
		elif h == d2:
			answer += h - u2
		else:
			answer += h
		h -= 1
		if answer < 0:
			answer = 0
	print(answer)
main()


import sys

def getAndParseInt(num=1):
	string = sys.stdin.readline().strip()
	if num == 1:
		return int(string)
	else:
		return [int(part) for part in string.split()]

def getAndParseString(num=1, delim=''):
	string = sys.stdin.readline().strip()
	if num == 1:
		return string
	elif delim != '':
		return [part for part in string.split(delim)]
	else:
		return [part for part in string.split()]
(w, h) = getAndParseInt(2)
(u1, d1) = getAndParseInt(2)
(u2, d2) = getAndParseInt(2)
for cur_h in reversed(range(h + 1)):
	w += cur_h
	if cur_h == d1:
		w += -u1
	elif cur_h == d2:
		w += -u2
	w = max(0, w)
print(w)


(weight, height) = map(int, input().split())
(u1, d1) = map(int, input().split())
(u2, d2) = map(int, input().split())
while height:
	if height == d1 or height == d2:
		weight += height
		if height == d1:
			weight -= u1
		else:
			weight -= u2
	else:
		weight += height
	if weight < 0:
		weight = 0
	height -= 1
print(weight)


def f(x):
	return x * (x + 1) // 2
(w, h) = map(int, input().split())
(u1, d1) = map(int, input().split())
(u2, d2) = map(int, input().split())
if d2 > d1:
	(a, b) = (u1, d1)
	(u1, d1) = (u2, d2)
	(u2, d2) = (a, b)
w += f(h) - f(d1 - 1)
h = d1
w -= u1
if w < 0:
	w = 0
h -= 1
w += f(h) - f(d2 - 1)
h = d2
w -= u2
if w < 0:
	w = 0
h -= 1
w += f(h)
print(w)


m = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
we = m[0]
i = m[1]
while i >= 0:
	we += i
	if i == b[1]:
		we -= b[0]
	if i == c[1]:
		we -= c[0]
	if we < 0:
		we = 0
	i -= 1
print(we)


(w, h) = map(int, input().split())
(u1, d1) = map(int, input().split())
(u2, d2) = map(int, input().split())
dict1 = {}
dict1[d1] = u1
dict1[d2] = u2
while h != 0:
	w = w + h
	if h in dict1:
		w = w - dict1[h]
	if w < 0:
		w = 0
	h -= 1
print(w)


def mi():
	return map(int, input().split())
(w, h) = mi()
(u1, d1) = mi()
(u2, d2) = mi()
w += h
if h == d2:
	w -= u2
if h == d1:
	w -= u1
w = max(w, 0)
while h > 0:
	h -= 1
	w += h
	if h == d2:
		w -= u2
	if h == d1:
		w -= u1
	w = max(w, 0)
print(w)


wh = list(map(int, input().split()))
ud1 = list(map(int, input().split()))
ud2 = list(map(int, input().split()))
while wh[1] > 0:
	wh[0] += wh[1]
	if ud1[1] == wh[1]:
		wh[0] = max(0, wh[0] - ud1[0])
	if ud2[1] == wh[1]:
		wh[0] = max(0, wh[0] - ud2[0])
	wh[1] -= 1
print(wh[0])


(w, h) = [int(x) for x in input().split()]
ans = w
ht = [0] * 101
minus = 0
for i in range(2):
	(a, b) = [int(x) for x in input().split()]
	ht[b] = a
i = h
while i >= 0:
	ans += i
	if ht[i]:
		ans -= ht[i]
	ans = max(0, ans)
	i -= 1
print(ans)


(w, h) = map(int, input().strip().split())
(u1, d1) = map(int, input().strip().split())
(u2, d2) = map(int, input().strip().split())
if d1 < d2:
	(d1, d2) = (d2, d1)
	(u1, u2) = (u2, u1)
wa = w
x = d1 * (h - d1 + 1) + (h - d1) * (h - d1 + 1) / 2
wb = wa + x - u1
if wb < 0:
	wa = 0
else:
	wa = wb
x = d2 * (d1 - d2) + (d1 - d2) * (d1 - d2 - 1) / 2
wb = wa + x - u2
if wb < 0:
	wa = 0
else:
	wa = wb
wa += (d2 - 1) * d2 / 2
print(int(wa))


(w, h) = map(int, input().split())
x = w
(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
for i in range(h, 0, -1):
	x += i
	if i == b:
		x = max(0, x - a)
	if i == d:
		x = max(0, x - c)
print(x)


(w, h) = map(int, input().split())
(u, d) = map(int, input().split())
(ul, dl) = map(int, input().split())
for i in range(h, 0, -1):
	w += i
	if i == d:
		w = w - u
	if i == dl:
		w = w - ul
	if w < 0:
		w = 0
print(w, end='')


l = [[int(i) for i in input().split()] for i in range(3)]
x = l[0][0]
h = l[0][1]
for i in range(l[0][1]):
	x += h
	if l[0][1] - i == l[1][1]:
		x = max(0, x - l[1][0])
	if l[0][1] - i == l[2][1]:
		x = max(0, x - l[2][0])
	h -= 1
print(max(0, x))


(w1, h1) = list(map(int, input().split()))
(w2, h2) = list(map(int, input().split()))
(w3, h3) = list(map(int, input().split()))
while h1 >= 0:
	w1 += h1
	if h1 == h2:
		w1 -= w2
	elif h1 == h3:
		w1 -= w3
	if w1 < 0:
		w1 = 0
	h1 -= 1
print(w1)


SNOW = [int(x) for x in input().split()]
S1 = [int(x) for x in input().split()]
S2 = [int(x) for x in input().split()]
for h in range(SNOW[1]):
	i = SNOW[1] - h
	SNOW[0] += i
	if i == S1[1]:
		SNOW[0] -= S1[0]
	if i == S2[1]:
		SNOW[0] -= S2[0]
	if SNOW[0] <= 0:
		SNOW[0] = 0
print(SNOW[0])


(w, h) = map(int, input().split())
(u1, d1) = map(int, input().split())
(u2, d2) = map(int, input().split())
while h > 0:
	w += h
	if h == d1:
		w = max(0, w - u1)
	elif h == d2:
		w = max(0, w - u2)
	else:
		pass
	h -= 1
print(w)


n = [int(x) for x in input().split()]
ans = n[0]
l = []
for i in range(2):
	l.append([int(x) for x in input().split()])
for i in range(n[1]):
	ans = ans + (n[1] - i)
	if n[1] - i == l[0][1]:
		ans = ans - l[0][0]
		if ans < 0:
			ans = 0
	elif n[1] - i == l[1][1]:
		ans = ans - l[1][0]
		if ans < 0:
			ans = 0
print(ans)


(w, h) = map(int, input().split())
(u1, d1) = map(int, input().split())
(u2, d2) = map(int, input().split())
m = w
for x in range(h, -1, -1):
	m = m + x
	if x == d1:
		m = m - u1
		if m < 0:
			m = 0
	if x == d2:
		m = m - u2
		if m < 0:
			m = 0
print(m)


i = lambda : map(int, input().split())
(w, h) = i()
(a, b) = i()
(c, d) = i()
while h:
	w = max(0, w + h - [[0, a][h == b], c][h == d])
	h -= 1
print(w)


(w, h) = map(int, input().split())
(u1, d1) = map(int, input().split())
(u2, d2) = map(int, input().split())
if d1 < d2:
	xx = w + h
	for i in range(h - 1, d2 - 1, -1):
		xx += i
	if d2 <= h:
		xx -= u2
	if xx <= 0:
		xx = 0
	for i in range(d2 - 1, d1 - 1, -1):
		xx += i
	if d1 <= h:
		xx -= u1
	if xx <= 0:
		xx = 0
	for i in range(d1 - 1, -1, -1):
		xx += i
	print(xx)
else:
	yy = w + h
	for i in range(h - 1, d1 - 1, -1):
		yy += i
	if d1 <= h:
		yy -= u1
	if yy <= 0:
		yy = 0
	if d2 <= h:
		yy -= u2
	for i in range(d1 - 1, d2 - 1, -1):
		yy += i
	if yy <= 0:
		yy = 0
	for i in range(d2 - 1, -1, -1):
		yy += i
	print(yy)


import sys
input = sys.stdin.readline
L = lambda : list(map(int, input().split()))
M = lambda : map(int, input().split())
(w, h) = M()
(u1, d1) = M()
(u2, d2) = M()
while h:
	w += h
	if h == d1:
		w -= u1
		if w < 0:
			w = 0
	elif h == d2:
		w -= u2
		if w < 0:
			w = 0
	h -= 1
print(w)


T_ON = 0
DEBUG_ON = 0
MOD = 998244353

def solve():
	(w, h) = read_ints()
	(u1, d1) = read_ints()
	(u2, d2) = read_ints()
	if d1 < d2:
		(u1, d1, u2, d2) = (u2, d2, u1, d1)

	def hit(w, h, u, d):
		w += (h + d) * (h - d + 1) // 2 - u
		h = d - 1
		return (max(0, w), h)
	(w, h) = hit(w, h, u1, d1)
	(w, h) = hit(w, h, u2, d2)
	(w, h) = hit(w, h, 0, 0)
	print(w)

def main():
	T = read_int() if T_ON else 1
	for i in range(T):
		solve()

def debug(*xargs):
	if DEBUG_ON:
		print(*xargs)
from collections import *
import math
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def read_int():
	return int(input())

def read_ints():
	return list(map(int, input().split()))

def print_nums(nums):
	print(' '.join(map(str, nums)))

def YES():
	print('YES')

def Yes():
	print('Yes')

def NO():
	print('NO')

def No():
	print('No')

def First():
	print('First')

def Second():
	print('Second')

def fib(n):
	(a, b) = (0, 1)
	for _ in range(n):
		(a, b) = (b, a + b)
	return a

def fib_ns(n):
	assert n >= 1
	f = [0 for _ in range(n + 1)]
	f[0] = 0
	f[1] = 1
	for i in range(2, n + 1):
		f[i] = f[i - 1] + f[i - 2]
	return f

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def xgcd(a, b):
	(x0, x1, y0, y1) = (0, 1, 1, 0)
	while a != 0:
		((q, a), b) = (divmod(b, a), a)
		(y0, y1) = (y1, y0 - q * y1)
		(x0, x1) = (x1, x0 - q * x1)
	return (b, x0, y0)

def lcm(a, b):
	d = gcd(a, b)
	return a * b // d

def is_even(x):
	return x % 2 == 0

def is_odd(x):
	return x % 2 == 1

def modinv(a, m):
	(g, x, _) = xgcd(a, m)
	if g != 1:
		raise Exception('gcd(a, m) != 1')
	return x % m

def mod_add(x, y):
	x += y
	while x >= MOD:
		x -= MOD
	while x < 0:
		x += MOD
	return x

def mod_mul(x, y):
	return x * y % MOD

def mod_pow(x, y):
	if y == 0:
		return 1
	if y % 2:
		return mod_mul(x, mod_pow(x, y - 1))
	p = mod_pow(x, y // 2)
	return mod_mul(p, p)

def mod_inv(y):
	return mod_pow(y, MOD - 2)

def mod_div(x, y):
	return mod_mul(x, mod_inv(y))

def is_prime(n):
	if n == 1:
		return False
	for i in range(2, int(n ** 0.5) + 1):
		if n % i:
			return False
	return True

def gen_primes(n):
	P = [True for _ in range(n + 1)]
	P[0] = P[1] = False
	for i in range(int(n ** 0.5) + 1):
		if P[i]:
			for j in range(2 * i, n + 1, i):
				P[j] = False
	return P
main()


(a, b) = map(int, input().split())
(u, d) = map(int, input().split())
(U, D) = map(int, input().split())
for i in range(b, 0, -1):
	a += i
	if d == i:
		a -= u
	elif D == i:
		a -= U
	if a < 0:
		a = 0
print(a)


(a, b) = map(int, input().split())
(m, n) = map(int, input().split())
(m1, n1) = map(int, input().split())
while b != 0:
	a += b
	if n1 == b:
		a -= m1
	elif n == b:
		a -= m
	a = max(0, a)
	b -= 1
print(a)


import sys
(h, n) = [int(x) for x in input().split()]
(a, b) = [int(x) for x in input().split()]
(c, d) = [int(x) for x in input().split()]
while n > 0:
	h += n
	if n == b:
		h -= a
	if n == d:
		h -= c
	h = max(h, 0)
	n -= 1
print(h)


(w, h) = map(int, input().split())
(v1, h1) = map(int, input().split())
(v2, h2) = map(int, input().split())
while h != 0:
	w = w + h
	if h == h1:
		w -= v1
	if h == h2:
		w -= v2
	if w < 0:
		w = 0
	h = h - 1
print(w)


import sys
from collections import defaultdict
strInp = lambda : input().strip().split()
intInp = lambda : list(map(int, strInp()))
(w, h) = intInp()
(u1, d1) = intInp()
(u2, d2) = intInp()
for i in range(h, 0, -1):
	w += i
	if d1 == i:
		w -= u1
	if d2 == i:
		w -= u2
	if w < 0:
		w = 0
print(w)


(w, h) = map(int, input().split())
d1 = list(map(int, input().split()))
d2 = list(map(int, input().split()))
if d2[1] > d1[1]:
	(d1, d2) = (d2, d1)
p = h * (h + 1) // 2 - d1[1] * (d1[1] + 1) // 2 + d1[1]
q = d1[1] * (d1[1] + 1) // 2 - d2[1] * (d2[1] + 1) // 2 - d1[1] + d2[1]
r = d2[1] * (d2[1] + 1) // 2 - d2[1]
if w + p - d1[0] < 0:
	w = 0
else:
	w = w + p - d1[0]
if w + q - d2[0] < 0:
	w = 0
else:
	w = w + q - d2[0]
print(w + r)


from operator import itemgetter
(w, h) = map(int, input().split())
(u1, d1) = map(int, input().split())
(u2, d2) = map(int, input().split())
ans = w
for i in range(h, -1, -1):
	ans += i
	if i == d1:
		ans -= u1
	if i == d2:
		ans -= u2
	if ans < 0:
		ans = 0
print(ans)


(w, h) = map(int, input().split())
(u1, d1) = map(int, input().split())
(u2, d2) = map(int, input().split())
for d in range(h, -1, -1):
	if d == 0:
		print(w)
		break
	w += d
	if d1 == d:
		w -= u1
		if w < 0:
			w = 0
	if d2 == d:
		w -= u2
		if w < 0:
			w = 0


(w, h) = [int(i) for i in input().split()]
(u1, d1) = [int(i) for i in input().split()]
(u2, d2) = [int(i) for i in input().split()]
if d1 < d2:
	(d1, d2) = (d2, d1)
	(u1, u2) = (u2, u1)
total = w
if d1 <= h:
	total += h * (h + 1) / 2
	total -= d1 * (d1 - 1) / 2
	h = d1 - 1
	total -= u1
if total < 0:
	total = 0
if d2 <= h:
	total += h * (h + 1) / 2
	total -= d2 * (d2 - 1) / 2
	h = d2 - 1
	total -= u2
if total < 0:
	total = 0
if h > 0:
	total += h * (h + 1) / 2
print(int(total))


def solution(l1, l2, l3):
	(w0, h0) = (l1[0], l1[1])
	(sw1, sh1) = (l2[0], l2[1])
	(sw2, sh2) = (l3[0], l3[1])
	if sh1 > sh2:
		firstw = sw1
		firsth = sh1
		secondw = sw2
		secondh = sh2
	else:
		firstw = sw2
		firsth = sh2
		secondw = sw1
		secondh = sh1
	i = h0
	while i >= 0:
		w0 += i
		if i == firsth:
			w0 = max(0, w0 - firstw)
		if i == secondh:
			w0 = max(0, w0 - secondw)
		i -= 1
	return w0

def answer():
	l1 = [int(x) for x in input().split()]
	l2 = [int(x) for x in input().split()]
	l3 = [int(x) for x in input().split()]
	print(solution(l1, l2, l3))
answer()


(w, h) = map(int, input().split())
stones = tuple((tuple(map(int, input().split())) for _ in [0, 1]))
for i in range(h, 0, -1):
	w += i
	for j in range(2):
		if stones[j][1] == i:
			w = max(0, w - stones[j][0])
print(w)


import math, itertools, fractions, heapq, collections, bisect, sys, queue, copy
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return [int(x) for x in sys.stdin.readline().split()]

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def LS():
	return sys.stdin.readline().split()

def S():
	return input()

def main():
	(w, h) = LI()
	(a, b) = LI()
	(c, d) = LI()
	for i in range(h + 1)[::-1]:
		w += i
		if i == b:
			w -= a
		if i == d:
			w -= c
		if w < 0:
			w = 0
	return w
print(main())


(n, m) = map(int, input().split())
(u1, d1) = map(int, input().split())
(u2, d2) = map(int, input().split())
for i in reversed(range(m + 1)):
	n += i
	if i == d1:
		n -= u1
		if n < 0:
			n = 0
	elif i == d2:
		n -= u2
		if n < 0:
			n = 0
print(n)


(w, h) = map(int, input().split())
(u1, d1) = map(int, input().split())
(u2, d2) = map(int, input().split())
result = w + h
first = True
while h != 0:
	if first:
		result = w + h
		first = False
	else:
		result = result + h
	if h == d1:
		result = result - u1
	elif h == d2:
		result = result - u2
	if result < 0:
		result = 0
	h = h - 1
print(result)


(w, h) = map(int, input().strip().split())
(w1, d1) = map(int, input().strip().split())
(w2, d2) = map(int, input().strip().split())
val = w
if d1 < d2:
	for i in range(h, d2 - 1, -1):
		val += i
	val = max(0, val - w2)
	for i in range(d2 - 1, d1 - 1, -1):
		val += i
	val = max(0, val - w1)
	for i in range(d1 - 1, 0, -1):
		val += i
else:
	for i in range(h, d1 - 1, -1):
		val += i
	val = max(0, val - w1)
	for i in range(d1 - 1, d2 - 1, -1):
		val += i
	val = max(0, val - w2)
	for i in range(d2 - 1, 0, -1):
		val += i
print(val)


(a, b) = input().split()
(c, d) = input().split()
(e, f) = input().split()
a = int(a)
b = int(b)
c = int(c)
d = int(d)
e = int(e)
f = int(f)
while b > 0:
	a += b
	if b == d:
		a -= c
		if a < 0:
			a = 0
	if b == f:
		a -= e
		if a < 0:
			a = 0
	b -= 1
print(a)


M = input().split()
w = int(M[0])
h = int(M[1])
M = input().split()
u1 = int(M[0])
d1 = int(M[1])
M = input().split()
u2 = int(M[0])
d2 = int(M[1])
del M
while h != 0:
	w += h
	if h in [d1, d2]:
		x = [u1, u2][[d1, d2].index(h)]
		if x > w:
			w = 0
		else:
			w -= x
	h -= 1
print(w)


(w, H) = map(int, input().split())
(u1, d1) = map(int, input().split())
(u2, d2) = map(int, input().split())
if d1 < d2:
	tmp = (u1, d1)
	(u1, d1) = (u2, d2)
	(u2, d2) = tmp
for h in range(H, 0, -1):
	w += h
	if h == d1:
		w -= u1
	if h == d2:
		w -= u2
	if w < 0:
		w = 0
print(w)


l = list(map(int, input().split()))
s1 = list(map(int, input().split()))
s2 = list(map(int, input().split()))
(w, h) = (l[0], l[1])
(u1, h1) = (s1[0], s1[1])
(u2, h2) = (s2[0], s2[1])
while h > 0:
	w += h
	if h == h1:
		w -= u1
		if w < 0:
			w = 0
	if h == h2:
		w -= u2
		if w < 0:
			w = 0
	h -= 1
print(w)


(w, h) = map(int, input().split())
(u1, d1) = map(int, input().split())
(u2, d2) = map(int, input().split())
if d2 > d1:
	(k1, k2) = (u1, d1)
	(u1, d1) = (u2, d2)
	(u2, d2) = (k1, k2)
w = w + h
s = 0
for i in range(d1, h):
	s = s + i
w = w + s
h = d1
w = w - u1
if w < 0:
	w = 0
h = d1
s = 0
for i in range(d2, h):
	s = s + i
w = w + s
h = d2
w = w - u2
if w < 0:
	w = 0
s = 0
for i in range(0, d2):
	s = s + i
w = w + s
if w < 0:
	print(0)
else:
	print(w)


R = lambda : map(int, input().split())
(a, b) = R()
(c, d) = R()
(e, f) = R()
s = a
while b:
	s += b
	if b == d:
		s = max(0, s - c)
	if b == f:
		s = max(0, s - e)
	b -= 1
print(s)


(w, h) = map(int, input().split())
(w1, h1) = map(int, input().split())
(w2, h2) = map(int, input().split())
(h3, w3) = min((h2, w2), (h1, w1))
(h1, w1) = max((h2, w2), (h1, w1))
count = w + (h + h1) * (h - h1 + 1) // 2 - w1
count = max(0, count)
count += (h1 - 1 + h3) * (h1 - h3) // 2 - w3
count = max(0, count)
count += (h3 - 1) * h3 // 2
print(max(0, count))


(w, h) = [int(i) for i in input().split()]
(u1, d1) = [int(i) for i in input().split()]
(u2, d2) = [int(i) for i in input().split()]
u = [(d1, u1), (d2, u2)]
u.sort()
u = u[::-1]
for i in range(h, 0, -1):
	w = w + h
	if h == u[0][0]:
		w = w - u[0][1]
	if h == u[1][0]:
		w = w - u[1][1]
	if w < 0:
		w = 0
	h = h - 1
if w < 0:
	w = 0
print(w)


def main():
	(w, h) = [int(x) for x in input().split()]
	(u1, d1) = [int(x) for x in input().split()]
	(u2, d2) = [int(x) for x in input().split()]
	total = w
	d = h
	while d != 0:
		total += d
		if d == d1:
			total -= u1
		elif d == d2:
			total -= u2
		if total < 0:
			total = 0
		d -= 1
	print(total)
main()


from operator import itemgetter as ig

def line():
	return map(int, input().split())
(w, h) = line()
stf = sorted(map(tuple, [line(), line()]), key=ig(1), reverse=True) + [(0, 0)]
for (u, d) in stf:
	w += (d + h) * (-d + h + 1) // 2 - u
	w = max(w, 0)
	h = d - 1
print(w)


r = lambda : map(int, input().split())
((m, h), (u1, h1), (u2, h2)) = (r(), r(), r())
for i in range(h, 0, -1):
	m += i
	if i == h1:
		m -= u1
		if m < 0:
			m = 0
	elif i == h2:
		m -= u2
		if m < 0:
			m = 0
print(m)


(w, h) = [int(x) for x in input().split()]
(u1, d1) = [int(x) for x in input().split()]
(u2, d2) = [int(x) for x in input().split()]
if d1 < d2:
	(u1, u2) = (u2, u1)
	(d1, d2) = (d2, d1)

def sum_(a):
	return a * (a + 1) // 2

def sum_on_range(a, b):
	return sum_(b) - sum_(a) + a
weight = max(w + sum_on_range(d1, h) - u1, 0)
weight = max(weight + sum_on_range(d2, d1 - 1) - u2, 0)
weight += sum_(d2 - 1)
print(weight)


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
(e, f) = map(int, input().split())
for i in range(b, 0, -1):
	a += i
	if d == i:
		a -= c
	elif f == i:
		a -= e
	if a < 0:
		a = 0
print(a)


(n, m) = map(int, input().split())
d = {}
(a, b) = map(int, input().split())
d[b] = a
(a, b) = map(int, input().split())
d[b] = a
cur = n
for h in range(m, -1, -1):
	cur += h
	try:
		cur = max(cur - d[h], 0)
	except:
		pass
print(cur)


(current_weight, s) = map(int, input().split())
(ws1, hs1) = map(int, input().split())
(ws2, hs2) = map(int, input().split())
stones = {}
stones[hs1] = ws1
stones[hs2] = ws2
for current_height in range(s, 0, -1):
	current_weight += current_height
	if current_height in stones:
		current_weight -= stones[current_height]
		if current_weight < 0:
			current_weight = 0
print(current_weight)


(w, h) = [int(s) for s in input().split()]
(w1, h1) = [int(s) for s in input().split()]
(w2, h2) = [int(s) for s in input().split()]
extra = h * (h + 1) / 2
if h2 > h1:
	(h1, h2) = (h2, h1)
	(w1, w2) = (w2, w1)
if h1 > h2:
	for i in range(h, h1 - 1, -1):
		w += i
	w = w - w1
	if w < 0:
		w = 0
	for i in range(h1 - 1, h2 - 1, -1):
		w += i
	w = w - w2
	if w < 0:
		w = 0
	for i in range(h2 - 1, 0, -1):
		w += i
if w < 0:
	print('0')
else:
	print(int(w))


(w, h) = map(int, input().split())
(u1, d1) = map(int, input().split())
(u2, d2) = map(int, input().split())
new = [[u1, d1], [u2, d2]]
new.sort(key=lambda x: x[1])
(u1, d1) = (new[0][0], new[0][1])
(u2, d2) = (new[1][0], new[1][1])
ans = w + h
temp = 0
for i in range(d2, h):
	temp += i
ans += temp - u2
if ans < 0:
	ans = 0
temp = 0
for i in range(d1, d2):
	temp += i
ans += temp - u1
if ans < 0:
	ans = 0
temp = 0
for i in range(0, d1):
	temp += i
if ans < 0:
	ans = 0
ans += temp
print(ans)


import math
import os
import random
import re
import sys
a = list(map(int, input().rstrip().split()))
b = list(map(int, input().rstrip().split()))
c = list(map(int, input().rstrip().split()))
x = []
y = []
x.append(b[1])
x.append(c[1])
y.append(b[0])
y.append(c[0])
i = a[1]
w = a[0]
while i >= 0:
	w = w + i
	if i in x:
		t = x.index(i)
		w = w - y[t]
		if w < 0:
			w = 0
	i = i - 1
print(w)


snowb = list(map(int, input().split()))
r1 = list(map(int, input().split()))
r2 = list(map(int, input().split()))
weight = snowb[0]
i = snowb[1]
while i > 0:
	if i != r1[1] and i != r2[1]:
		weight += i
		i -= 1
	elif r1[1] == i and r2[1] == i:
		weight += i - r1[0] - r2[0]
		i -= 1
		if weight < 0:
			weight = 0
	elif r1[1] == i or r2[1] == i:
		if r1[1] == i:
			weight += i - r1[0]
			i -= 1
		else:
			weight += i - r2[0]
			i -= 1
		if weight < 0:
			weight = 0
print(weight)


(w, h) = [int(x) for x in input().split()]
a = []
(u, d) = [int(x) for x in input().split()]
a.append([d, u])
(u, d) = [int(x) for x in input().split()]
a.append([d, u])
a.sort()
u = a[0][1]
d = a[0][0]
u1 = a[1][1]
d1 = a[1][0]
ans = w
i = h
while i != 0:
	ans += i
	if d1 == i:
		ans -= u1
	elif d == i:
		ans -= u
	ans = max(0, ans)
	i -= 1
print(ans)


sb = input().split()
s1 = input().split()
s2 = input().split()
w = int(sb[0])
h1 = int(s1[1])
h2 = int(s2[1])
w1 = int(s1[0])
w2 = int(s2[0])
for i in range(int(sb[1]), -1, -1):
	if w < 0:
		w = 0
	w = w + i
	if i == h2:
		w = w - w2
	if i == h1:
		w = w - w1
print(w)


(w, h) = map(int, input().split())
st1 = input().split()
st2 = input().split()
for i in range(h, 0, -1):
	w += i
	if i == int(st1[1]):
		w -= int(st1[0])
	elif i == int(st2[1]):
		w -= int(st2[0])
	if w < 0:
		w = 0
print(w)


f = lambda : input().split()
(w, h) = map(int, f())
a = [[0, 1]] * 3
a[0] = [int(i) for i in f()]
a[1] = [int(i) for i in f()]
a.sort(key=lambda x: x[1], reverse=True)
for i in range(3):
	w = max(0, (h + a[i][1]) * (h - a[i][1] + 1) // 2 + w - a[i][0])
	h = a[i][1] - 1
print(w)


(u, h) = list(map(int, input('').strip().split()))
(u1, h1) = list(map(int, input('').strip().split()))
(u2, h2) = list(map(int, input('').strip().split()))
i = h
w = u
while i > 0:
	w = w + i
	if i == h1:
		w = w - u1
		i = i - 1
		if w < 0:
			w = 0
	elif i == h2:
		w = w - u2
		i = i - 1
		if w < 0:
			w = 0
	else:
		i = i - 1
print(w)


(w, h) = map(int, input().split())
(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
if b < d:
	(a, b, c, d) = (c, d, a, b)
w1 = w + (h + b) * (h - b + 1) // 2
w1 -= a
if w1 < 0:
	w1 = 0
w2 = w1 + (b - 1 + d) * (b - d) // 2
w2 -= c
if w2 < 0:
	w2 = 0
w3 = w2 + (d - 1) * d // 2
print(w3)


entrada1 = str(input())
entrada1 = entrada1.split()
w = int(entrada1[0])
h = int(entrada1[1])
entrada2 = str(input())
entrada2 = entrada2.split()
u1 = int(entrada2[0])
d1 = int(entrada2[1])
entrada3 = str(input())
entrada3 = entrada3.split()
u2 = int(entrada3[0])
d2 = int(entrada3[1])

def isRockIn1(n):
	respuesta = False
	if d1 == n:
		respuesta = True
	return respuesta

def isRockIn2(n):
	respuesta = False
	if d2 == n:
		respuesta = True
	return respuesta
for i in range(h + 1)[::-1]:
	w = w + i
	if isRockIn1(i):
		w = max(w - u1, 0)
	if isRockIn2(i):
		w = max(w - u2, 0)
print(w)


import bisect as bi
import collections as cc
I = lambda : list(map(int, input().split()))
(w, h) = I()
a = []
a.append(I())
a.append(I())
a.sort(key=lambda x: x[1])
a = a[::-1]
(x, y) = a
t = x[1]
w += h
while h != t:
	h -= 1
	w += h
w -= x[0]
t = y[1]
w = max(w, 0)
while h != t:
	h -= 1
	w += h
w -= y[0]
w = max(w, 0)
while h != 0:
	h -= 1
	w += h
print(w)


(n, k) = map(int, input().split())
x = []
for i in range(2):
	(a, b) = map(int, input().split())
	x.append([a, b])
for i in range(k, -1, -1):
	n += i
	if i == x[0][1]:
		if x[0][0] > n:
			n = 0
		else:
			n -= x[0][0]
	elif i == x[1][1]:
		if x[1][0] > n:
			n = 0
		else:
			n -= x[1][0]
if n < 0:
	print(0)
else:
	print(n)


read_line = lambda : list(map(int, input().split(' ')))
range_sum = lambda a, b: sum([*range(a, b)])
(ans, h) = read_line()
rocks = [read_line(), read_line()]
rocks = rocks if rocks[0][1] > rocks[1][1] else rocks[::-1]
prev_h = h + 1
for i in range(2):
	ans += range_sum(rocks[i][1], prev_h)
	ans = max(0, ans - rocks[i][0])
	prev_h = rocks[i][1]
ans += range_sum(0, rocks[1][1])
print(ans)


(w, h) = map(int, input().split())
(u1, d1) = map(int, input().split())
(u2, d2) = map(int, input().split())
for hn in range(h, -1, -1):
	w += hn
	if hn == d1:
		w -= u1
		if w < 0:
			w = 0
	elif hn == d2:
		w -= u2
		if w < 0:
			w = 0
print(w)


class Solution(object):

	def solve(self):
		(w, h) = map(int, input().strip().split(' '))
		(u1, d1) = map(int, input().strip().split(' '))
		(u2, d2) = map(int, input().strip().split(' '))
		for _h in range(h, -1, -1):
			if _h == d1:
				w -= u1
			elif _h == d2:
				w -= u2
			w += _h
			if w < 0:
				w = 0
		return w
sol = Solution()
print(sol.solve())


import sys
import math
import heapq
import bisect
import string
import functools
import itertools
import collections
(w, h) = list(map(int, sys.stdin.readline().strip().split()))
(u1, d1) = list(map(int, sys.stdin.readline().strip().split()))
(u2, d2) = list(map(int, sys.stdin.readline().strip().split()))
if d2 > d1:
	(d1, d2) = (d2, d1)
	(u1, u2) = (u2, u1)
cw = w + h * (h + 1) / 2 - d1 * (d1 - 1) / 2
cw = cw - u1
if cw < 0:
	cw = 0
cw = cw + d1 * (d1 - 1) / 2 - d2 * (d2 - 1) / 2
cw = cw - u2
if cw < 0:
	cw = 0
cw = cw + d2 * (d2 - 1) / 2
sys.stdout.write(str(int(cw)) + '\n')


(w, h) = [int(v) for v in input().split()]
(u1, d1) = [int(v) for v in input().split()]
(u2, d2) = [int(v) for v in input().split()]
stones = {d1: u1, d2: u2}
curr = w
for i in reversed(list(range(h + 1))):
	curr = max(0, curr + i - stones.get(i, 0))
print(curr)


(w, h) = list(map(int, input().split(' ')))
ud_list = [list(map(int, input().split(' '))) for i in range(2)]
if ud_list[1][1] > ud_list[0][1]:
	ud_list = [ud_list[1], ud_list[0]]
ud_list.append([0, 0])
w += h
for ud in ud_list:
	(u, d) = ud
	down_h = h - d
	plus_w = (d + h - 1) * (h - d) // 2
	w += plus_w
	w = max(0, w - u)
	h = d
print(w)


l1 = list(map(int, input().split()))
l2 = list(map(int, input().split()))
l3 = list(map(int, input().split()))
w = l1[0]
h = l1[1]
r = w
while h >= 0:
	if h == l2[1]:
		r += h - l2[0]
		if r < 0:
			r = 0
	elif h == l3[1]:
		r += h - l3[0]
		if r < 0:
			r = 0
	else:
		r += h
	h -= 1
print(r)


(w, h) = map(int, input().split())
(w1, d1) = map(int, input().split())
(w2, d2) = map(int, input().split())
while h != 0:
	w += h
	if h == d1:
		w = max(w - w1, 0)
	elif h == d2:
		w = max(w - w2, 0)
	h -= 1
print(w)


import math, sys, itertools
from fractions import *

def mp():
	return list(map(int, input().split()))

def main():
	(w, h) = mp()
	(u1, d1) = mp()
	(u2, d2) = mp()
	for i in range(h, -1, -1):
		w += i
		if i == d1:
			w -= u1
		if i == d2:
			w -= u2
		w = max(w, 0)
	print(w)
deb = 0
if deb:
	file = open('input.txt', 'w')
else:
	input = sys.stdin.readline
main()


(w, h) = list(map(int, input().split(' ')))
(p1w, p1h) = list(map(int, input().split(' ')))
(p2w, p2h) = list(map(int, input().split(' ')))
for i in range(h, -1, -1):
	w += i
	if i == p1h:
		w -= p1w
		if w < 0:
			w = 0
	elif i == p2h:
		w -= p2w
		if w < 0:
			w = 0
print(w)


import sys
import math
import bisect
import itertools

def main():
	(w, h) = map(int, input().split())
	A = []
	A.append(list(map(int, input().split())))
	A.append(list(map(int, input().split())))
	while h:
		w += h
		if h == A[0][1]:
			w -= A[0][0]
		elif h == A[1][1]:
			w -= A[1][0]
		if w < 0:
			w = 0
		h -= 1
	print(w)
main()


(m, h) = map(int, input().split())
(c_1, d_1) = map(int, input().split())
(c_2, d_2) = map(int, input().split())
while h:
	m += h
	if h == d_1:
		m -= c_1
	elif h == d_2:
		m -= c_2
	if m < 0:
		m = 0
	h -= 1
print(m)


(W, H) = map(int, input().split())
(W1, H1) = map(int, input().split())
(W2, H2) = map(int, input().split())
while H != 0:
	W = W + H
	if H == H1:
		W = W - W1
		if W < 0:
			W = 0
	if H == H2:
		W = W - W2
		if W < 0:
			W = 0
	H -= 1
print(W)


(w, h) = map(int, input().split())
(x, y) = map(int, input().split())
(a, b) = map(int, input().split())
for i in range(1, h + 1):
	w += h
	if h == y:
		w -= x
	if h == b:
		w -= a
	if w < 0:
		w = 0
	h = h - 1
print(w)


(m, h) = tuple(map(int, input().split()))
(u1, d1) = tuple(map(int, input().split()))
(u2, d2) = tuple(map(int, input().split()))
if d2 > d1:
	(u1, u2) = (u2, u1)
	(d1, d2) = (d2, d1)
while h != d1:
	m += h
	h -= 1
m += h
if u1 > m:
	m = 0
else:
	m -= u1
if h != 0:
	h -= 1
while h != d2:
	m += h
	h -= 1
m += h
if u2 > m:
	m = 0
else:
	m -= u2
if h != 0:
	h -= 1
while h != 0:
	m += h
	h -= 1
print(m)


s1 = input()
s2 = input()
s3 = input()
(w, h) = (int(s1.split(' ')[0]), int(s1.split(' ')[1]))
(u1, h1) = (int(s2.split(' ')[0]), int(s2.split(' ')[1]))
(u2, h2) = (int(s3.split(' ')[0]), int(s3.split(' ')[1]))
if h1 > h2:
	u_max = u1
	h_max = h1
	u_min = u2
	h_min = h2
else:
	u_max = u2
	h_max = h2
	u_min = u1
	h_min = h1
f1 = int(w + (h + h_max) * (h - h_max + 1) / 2 - u_max)
if f1 < 0:
	f1 = int(0 + (h_max - 1 + h_min) * (h_max - 1 - h_min + 1) / 2 - u_min)
else:
	f1 = int(f1 + (h_max - 1 + h_min) * (h_max - 1 - h_min + 1) / 2 - u_min)
if f1 < 0 and h_min <= 1:
	print(0)
elif f1 < 0 and h_min > 1:
	f = int(0 + (h_min - 1 + 1) * (h_min - 1) / 2)
	print(f)
else:
	f = int(f1 + (h_min - 1 + 1) * (h_min - 1) / 2)
	print(f)


(w, h) = map(int, input().split())
(u1, h1) = map(int, input().split())
(u2, h2) = map(int, input().split())
if h2 > h1:
	(u1, h1, u2, h2) = (u2, h2, u1, h1)
while h > 0:
	w = w + h
	if h == h1:
		w = max(0, w - u1)
	if h == h2:
		w = max(0, w - u2)
	h = h - 1
print(w)


(a, b) = map(int, input().split())
(n, m) = map(int, input().split())
(z, x) = map(int, input().split())
while b > 0:
	a += b
	if b == m:
		if a - n > 0:
			a = a - n
		else:
			a = 0
		b -= 1
	elif b == x:
		if a - z > 0:
			a = a - z
		else:
			a = 0
		b -= 1
	else:
		b -= 1
print(a)


from sys import stdin
input = stdin.readline

def f(w, h, u1, d1, u2, d2):
	while h:
		w += h
		if h == d1:
			w -= u1
			w = max(w, 0)
		if h == d2:
			w -= u2
			w = max(w, 0)
		h -= 1
	return w
(a, b) = map(int, input().strip().split())
(c, d) = map(int, input().strip().split())
(e, l) = map(int, input().strip().split())
print(f(a, b, c, d, e, l))


from collections import deque

def get_res(W, H, W1, H1, W2, H2):
	L = [0] * (H + 1)
	L[H] = W + H
	if H == H1:
		L[H] = max(0, L[H] - W1)
	if H == H2:
		L[H] = max(0, L[H] - W2)
	for i in range(H - 1, -1, -1):
		L[i] = L[i + 1] + i
		if i == H1:
			L[i] = max(0, L[i] - W1)
		if i == H2:
			L[i] = max(0, L[i] - W2)
	return L[0]
(W, H) = [int(s) for s in input().split(' ')]
(W1, H1) = [int(s) for s in input().split(' ')]
(W2, H2) = [int(s) for s in input().split(' ')]
result = get_res(W, H, W1, H1, W2, H2)
print('{}'.format(result))


a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
while a[1] > 0:
	a[0] += a[1]
	if a[1] == b[1]:
		a[0] = max(0, a[0] - b[0])
	if a[1] == c[1]:
		a[0] = max(0, a[0] - c[0])
	a[1] -= 1
print(a[0])


from sys import stdin, stdout
from math import *
from heapq import *
from collections import *

def main():
	(w, h) = [int(x) for x in stdin.readline().split()]
	(u1, d1) = [int(x) for x in stdin.readline().split()]
	(u2, d2) = [int(x) for x in stdin.readline().split()]
	while h >= 0:
		w = w + h
		if h == d1:
			w = max(w - u1, 0)
		if h == d2:
			w = max(w - u2, 0)
		h = h - 1
	stdout.write(str(w))
	return 0
main()


from bisect import bisect_right as br
from bisect import bisect_left as bl
from collections import defaultdict
from itertools import combinations
import functools
import sys
import math
MAX = sys.maxsize
MAXN = 10 ** 6 + 10
MOD = 10 ** 9 + 7

def isprime(n):
	n = abs(int(n))
	if n < 2:
		return False
	if n == 2:
		return True
	if not n & 1:
		return False
	for x in range(3, int(n ** 0.5) + 1, 2):
		if n % x == 0:
			return False
	return True

def mhd(a, b, x, y):
	return abs(a - x) + abs(b - y)

def numIN():
	return map(int, sys.stdin.readline().strip().split())

def charIN():
	return sys.stdin.readline().strip().split()
t = [0] * 1000010

def create(a, n):
	global t
	for i in range(n, 2 * n):
		t[i] = a[i - n]
	for i in range(n - 1, 0, -1):
		t[i] = t[2 * i] + t[2 * i + 1]

def ncr(n, k):
	res = 1
	c = [0] * (k + 1)
	c[0] = 1
	for i in range(1, n + 1):
		for j in range(min(i, k), 0, -1):
			c[j] = (c[j] + c[j - 1]) % MOD
	return c[k]
(w, h) = numIN()
(u1, d1) = numIN()
(u2, d2) = numIN()
for i in range(h, -1, -1):
	w = max(0, w)
	w += i
	if i == d1:
		w -= u1
	if i == d2:
		w -= u2
print(max(0, w))


def larger(rock1, rock2):
	if rock1[1] >= rock2[1]:
		return (rock1, rock2)
	else:
		return (rock2, rock1)

def findweight(weight, height, rock1, rock2):
	(tall, short) = larger(rock1, rock2)
	w = weight
	for h in range(height, tall[1] - 1, -1):
		w += h
	w = max(w - tall[0], 0)
	for h in range(tall[1] - 1, short[1] - 1, -1):
		w += h
	w = max(w - short[0], 0)
	for h in range(short[1] - 1, -1, -1):
		w += h
	return w
begin = input().split()
weight = int(begin[0])
height = int(begin[1])
begin1 = input().split()
rock1 = [int(begin1[0]), int(begin1[1])]
begin2 = input().split()
rock2 = [int(begin2[0]), int(begin2[1])]
output = findweight(weight, height, rock1, rock2)
print(output)


sn = input()
st1 = input()
st2 = input()
snlist = sn.split()
st1list = st1.split()
st2list = st2.split()
snw = int(snlist[0])
snh = int(snlist[1])
st1w = int(st1list[0])
st1h = int(st1list[1])
st2w = int(st2list[0])
st2h = int(st2list[1])
while snh != 0:
	snw = snw + snh
	if snh == st1h:
		snw = snw - st1w
		if snw < 0:
			snw = 0
	if snh == st2h:
		snw = snw - st2w
		if snw < 0:
			snw = 0
	snh = snh - 1
print(snw)


(w, h) = map(int, input().split())
stones = {}
for _ in range(2):
	(u, d) = map(int, input().split())
	stones[d] = u
finish_weight = w
for current_height in range(h, 0, -1):
	finish_weight += current_height
	if current_height in stones:
		finish_weight = max(0, finish_weight - stones[current_height])
print(finish_weight)


l1 = input().split()
l2 = input().split()
l3 = input().split()
w = int(l1[0])
h = int(l1[1])
w1 = int(l2[0])
h1 = int(l2[1])
w2 = int(l3[0])
h2 = int(l3[1])
for i in range(h, 0, -1):
	w += i
	if i == h1:
		w -= w1
		if w < 0:
			w = 0
	elif i == h2:
		w -= w2
		if w < 0:
			w = 0
print(w)


def ii():
	return int(input())

def mi():
	return list(map(int, input().split()))

def li():
	return list(mi())
(w, h) = mi()
(u1, d1) = mi()
(u2, d2) = mi()
c = w
for i in range(h, -1, -1):
	c += i
	if i == d1:
		c -= u1
	elif i == d2:
		c -= u2
	c = max(0, c)
print(c)


(w, h) = map(int, input().split())
(u1, d1) = map(int, input().split())
(u2, d2) = map(int, input().split())
if d2 > d1:
	(d1, d2) = (d2, d1)
	(u1, u2) = (u2, u1)
sm = int(w + (h + d1) / 2 * (h - d1 + 1))
sm -= u1
if sm < 0:
	sm = 0
sm += int((d1 - 1 + d2) / 2 * (d1 - d2))
sm -= u2
if sm < 0:
	sm = 0
sm += int((d2 - 1) / 2 * d2)
print(sm)


(w, h) = list(map(int, input().split()))
(w1, h1) = list(map(int, input().split()))
(w2, h2) = list(map(int, input().split()))
while h != 0:
	w += h
	if h == h1:
		w -= w1
	elif h == h2:
		w -= w2
	if w < 0:
		w = 0
	h -= 1
print(w)


list_int_input = lambda inp: list(map(int, inp.split()))
int_input = lambda inp: int(inp)
string_to_list_input = lambda inp: list(inp)
(w, h) = map(int, input().split())
(u1, d1) = map(int, input().split())
(u2, d2) = map(int, input().split())
if d1 > d2:
	(u1, u2, d1, d2) = (u2, u1, d2, d1)
n = h - d2 + 1
w += int(n * (d2 + h) / 2)
w -= u2
if w < 0:
	w = 0
n = d2 - d1
w += int(n * (d2 - 1 + d1) / 2)
w -= u1
if w < 0:
	w = 0
w += int(d1 * (d1 - 1) / 2)
print(w)


(sw, h) = input().split()
sw = int(sw)
h = int(h)
(u1, h1) = input().split()
u1 = int(u1)
h1 = int(h1)
(u2, h2) = input().split()
u2 = int(u2)
h2 = int(h2)
while h != 0:
	if h2 == h:
		sw -= u2
		sw += h
		if sw < 0:
			sw = 0
		h -= 1
	elif h1 == h:
		sw -= u1
		sw += h
		if sw < 0:
			sw = 0
		h -= 1
	else:
		sw += h
		h -= 1
print(sw)


in2int = lambda : map(int, input().split())
(w, h) = in2int()
(u1, d1) = in2int()
(u2, d2) = in2int()
for i in range(h, -1, -1):
	w += i
	if i == d1:
		w -= u1
	if i == d2:
		w -= u2
	if w < 0:
		w = 0
print(w)


def solve(w: int, h: int, u1: int, d1: int, u2: int, d2: int):
	ans = w
	for i in range(h, 0, -1):
		ans += i
		if i == d1:
			ans -= u1
		if i == d2:
			ans -= u2
		if ans < 0:
			ans = 0
	return ans
import sys
print(solve(*map(int, sys.stdin.read().split())))


(w, h) = map(int, input().split())
(u1, d1) = map(int, input().split())
(u2, d2) = map(int, input().split())
dic = {d1: u1, d2: u2}
w1 = w + (h + max(d1, d2)) * (h - max(d1, d2) + 1) / 2
r1 = max(w1 - dic[max(d1, d2)], 0)
w2 = r1 + (d1 + d2) * (abs(d1 - d2) + 1) / 2 - max(d1, d2)
r2 = max(w2 - dic[min(d1, d2)], 0)
w3 = r2 + min(d1, d2) * (min(d1, d2) + 1) / 2 - min(d1, d2)
print(int(w3))


def solve(ws, hs, w1, h1, w2, h2):
	for height in range(hs, 0, -1):
		ws += height
		if height == h1:
			ws = max(0, ws - w1)
		elif height == h2:
			ws = max(0, ws - w2)
	return ws
(ws, hs) = map(int, input().split())
(w1, h1) = map(int, input().split())
(w2, h2) = map(int, input().split())
print(solve(ws, hs, w1, h1, w2, h2))


def rolling(w, h, u1, d1, u2, d2):
	w_new = w
	while h >= 0:
		w_new += h
		if h == d1:
			w_new -= u1
			if w_new < 0:
				w_new = 0
			h -= 1
		elif h == d2:
			w_new -= u2
			if w_new < 0:
				w_new = 0
			h -= 1
		else:
			h -= 1
	print(w_new)
(w, h) = map(int, input().split())
(u1, d1) = map(int, input().split())
(u2, d2) = map(int, input().split())
rolling(w, h, u1, d1, u2, d2)


def main():
	((w, h), (u1, d1), (u2, d2)) = (map(int, input().split()) for _ in 'h12')
	while h:
		w += h
		if h in (d1, d2):
			w = max(w - (u1 if h == d1 else u2), 0)
		h -= 1
	print(w)
main()


def SUM(x):
	return x * (x + 1) / 2
(w1, d1) = map(int, input().split())
(w2, d2) = map(int, input().split())
(w3, d3) = map(int, input().split())
if d2 > d3:
	w1 = max(0, w1 + SUM(d1) - SUM(d2 - 1) - w2)
	w1 = max(0, w1 + SUM(d2 - 1) - SUM(d3 - 1) - w3)
	w1 += SUM(d3 - 1)
else:
	w1 = max(0, w1 + SUM(d1) - SUM(d3 - 1) - w3)
	w1 = max(0, w1 + SUM(d3 - 1) - SUM(d2 - 1) - w2)
	w1 += SUM(d2 - 1)
print(int(w1))


(w, h) = map(int, input().split())
(u1, d1) = map(int, input().split())
(u2, d2) = map(int, input().split())
for k in range(h):
	i = h - k
	w += i
	if i == d1:
		w -= u1
	if i == d2:
		w -= u2
	if w < 0:
		w = 0
print(w)


(w, h) = map(int, input().split())
(w1, h1) = map(int, input().split())
(w2, h2) = map(int, input().split())
t = w
(f1, f2) = (1, 1)
for i in range(h, -1, -1):
	t += i
	if i == h1 and f1:
		t -= w1
		f1 = 0
	if i == h2 and f2:
		t -= w2
		f2 = 0
	if t < 0:
		t = 0
print(t)


__author__ = 'Sudip'
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
s = a[0]
for i in range(a[1], -1, -1):
	s += i
	if b[1] == i:
		s -= b[0]
		if s < 0:
			s = 0
	if c[1] == i:
		s -= c[0]
		if s < 0:
			s = 0
print(s)


(w, h) = map(int, input().split())
(w1, d1) = map(int, input().split())
(w2, d2) = map(int, input().split())
for i in range(h, -1, -1):
	w += i
	if i == d1:
		w -= w1
	if i == d2:
		w -= w2
	if w < 0:
		w = 0
print(w)


a1 = input().split()
a2 = input().split()
a3 = input().split()
m = abs(int(a1[0]))
h = abs(int(a1[1]))
m1 = abs(int(a2[0]))
h1 = abs(int(a2[1]))
m2 = abs(int(a3[0]))
h2 = abs(int(a3[1]))
for i in range(h):
	m = m + (h - i)
	if h - i == h1:
		m = m - m1
	if h - i == h2:
		m = m - m2
	if m <= 0:
		m = 0
print(int(m))


(snowballw, snowballh) = [int(x) for x in input().split()]
(stone1w, stone1h) = [int(x) for x in input().split()]
(stone2w, stone2h) = [int(x) for x in input().split()]
for i in range(snowballh, 0, -1):
	snowballw += i
	if i == stone1h:
		snowballw -= stone1w
	if i == stone2h:
		snowballw -= stone2w
	if snowballw < 0:
		snowballw = 0
print(snowballw)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def main():
	pass

def binary(n):
	return bin(n).replace('0b', '')

def decimal(s):
	return int(s, 2)

def pow2(n):
	p = 0
	while n > 1:
		n //= 2
		p += 1
	return p

def isPrime(n):
	if n == 1:
		return False
	else:
		root = int(n ** 0.5)
		root += 1
		for i in range(2, root):
			if n % i == 0:
				return False
		return True

def lts(l):
	s = ''.join(map(str, l))
	return s

def stl(s):
	l = list(s)
	return l

def sq(a, target, arr=[]):
	s = sum(arr)
	if s == target:
		return arr
	if s >= target:
		return
	for i in range(len(a)):
		n = a[i]
		remaining = a[i + 1:]
		ans = sq(remaining, target, arr + [n])
		if ans:
			return ans

def SieveOfEratosthenes(n):
	cnt = 0
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	for p in range(2, n + 1):
		if prime[p]:
			cnt += 1
	return cnt

def nCr(n, r):
	f = math.factorial
	return f(n) // f(r) // f(n - r)
mod = int(1000000000.0) + 7

def ssinp():
	return input()

def iinp():
	return int(input())

def nninp():
	return map(int, input().split())

def llinp():
	return list(map(int, input().split()))

def p(xyz):
	print(xyz)

def p2(a, b):
	print(a, b)
import math
(w, h) = nninp()
(u1, d1) = nninp()
(u2, d2) = nninp()
if d1 > d2:
	(u1, d1, u2, d2) = (u2, d2, u1, d1)
for i in range(d2, h + 1):
	w += i
w = max(0, w - u2)
for i in range(d1, d2):
	w += i
w = max(0, w - u1)
for i in range(0, d1):
	w += i
p(w)


MOD = 10 ** 9 + 7
I = lambda : list(map(int, input().split()))
(n, k) = I()
l = I()
b = I()
if b[1] > l[1]:
	(b, l) = (l, b)
ans = n
for i in range(k, -1, -1):
	ans += i
	if i == l[1]:
		ans = max(0, ans - l[0])
	if i == b[1]:
		ans = max(0, ans - b[0])
print(ans)


(snowballweight, snowballheight) = input().split()
snowballweight = int(snowballweight)
snowballheight = int(snowballheight)
(st1weight, st1height) = input().split()
st1weight = int(st1weight)
st1height = int(st1height)
(st2weight, st2height) = input().split()
st2weight = int(st2weight)
st2height = int(st2height)
i = snowballheight
while i > 0:
	snowballweight = snowballweight + i
	if i == st1height:
		snowballweight = snowballweight - st1weight
		if snowballweight < 0:
			snowballweight = 0
	if i == st2height:
		snowballweight = snowballweight - st2weight
		if snowballweight < 0:
			snowballweight = 0
	i -= 1
print(snowballweight)


def res():
	(a, b) = map(int, input().split(' '))
	(c, d) = map(int, input().split(' '))
	(e, f) = map(int, input().split(' '))
	ans = a
	while b > 0:
		ans = ans + b
		if b == d:
			ans = ans - c
			if ans < 0:
				ans = 0
		if b == f:
			ans = ans - e
			if ans < 0:
				ans = 0
		b = b - 1
	print(ans)
res()


def main():
	(w, h) = [int(x) for x in input().split()]
	a = []
	a.append([int(x) for x in input().split()])
	a.append([int(x) for x in input().split()])
	for i in range(h, -1, -1):
		w += i
		if a[0][1] == i:
			w -= a[0][0]
			if w < 0:
				w = 0
		elif a[1][1] == i:
			w -= a[1][0]
			if w < 0:
				w = 0
	print(w)
main()


w_h = list(map(int, input().rstrip().split()))
d1_h1 = list(map(int, input().rstrip().split()))
d2_h2 = list(map(int, input().rstrip().split()))
(w, h, d1, h1, d2, h2) = w_h + d1_h1 + d2_h2
for i in range(h, 0, -1):
	if i == h1:
		w += i
		w -= d1
	elif i == h2:
		w += i
		w -= d2
	else:
		w += i
	if w < 0:
		w = 0
print(w)


sw = input()
s1 = input()
s2 = input()
sw = sw.split(' ')
s1 = s1.split(' ')
s2 = s2.split(' ')
curW = int(sw[0])
curH = int(sw[1])
st1W = int(s1[0])
st1H = int(s1[1])
st2W = int(s2[0])
st2H = int(s2[1])
while curH >= 0:
	curW += curH
	if curH == st1H:
		curW -= st1W
	elif curH == st2H:
		curW -= st2W
	curH -= 1
	if curW < 0:
		curW = 0
print(curW)


def main(s, s1, s2):
	index = s[1]
	w = s[0]
	while index >= 0:
		if w < 0:
			w = 0
		w = w + index
		if index == s1[1]:
			w = w - s1[0]
		elif index == s2[1]:
			w = w - s2[0]
		index = index - 1
	if w < 0:
		return 0
	return w
s = list(map(int, input().rstrip().split()))
s1 = list(map(int, input().rstrip().split()))
s2 = list(map(int, input().rstrip().split()))
res = main(s, s1, s2)
print(res)


(w, h) = [int(x) for x in input().split()]
(u1, d1) = [int(x) for x in input().split()]
(u2, d2) = [int(x) for x in input().split()]
if d2 > d1:
	(u1, u2, d1, d2) = (u2, u1, d2, d1)
s1 = (h + d1) * (h - d1 + 1) // 2
x = max(0, w + s1 - u1)
s2 = (d2 + d1 - 1) * (d1 - d2) // 2
x = max(0, x + s2 - u2)
s3 = d2 * (d2 - 1) // 2
print(x + s3)


(n, k) = [int(x) for x in input().split()]
(w1, h1) = [int(x) for x in input().split()]
(w2, h2) = [int(x) for x in input().split()]
tot = n
for i in range(k, 0, -1):
	if i == h1:
		tot += i
		tot = max(tot - w1, 0)
	elif i == h2:
		tot += i
		tot = max(tot - w2, 0)
	else:
		tot += i
print(tot)


(w, h) = tuple(map(int, input().split()))
(u1, d1) = tuple(map(int, input().split()))
(u2, d2) = tuple(map(int, input().split()))
for _ in range(h):
	if w < 0:
		w = 0
	w += h
	if h == d1:
		w -= u1
	if h == d2:
		w -= u2
	h -= 1
	if h == 0:
		break
if w > 0:
	print(w)
else:
	print(0)


def weightLeft(w, h, u1, h1, u2, h2):
	currentWeight = w
	for i in range(h, -1, -1):
		currentWeight += i
		if i == h1:
			currentWeight = max(currentWeight - u1, 0)
		if i == h2:
			currentWeight = max(currentWeight - u2, 0)
	return currentWeight
(w, h) = map(int, input().split())
(u1, h1) = map(int, input().split())
(u2, h2) = map(int, input().split())
print(weightLeft(w, h, u1, h1, u2, h2))


String = input()
(W, H) = String.split()
(W, H) = (int(W), int(H))
String = input()
(U1, D1) = String.split()
(U1, D1) = (int(U1), int(D1))
String = input()
(U2, D2) = String.split()
(U2, D2) = (int(U2), int(D2))
h = H
while True:
	if h == 0:
		print(W)
		break
	elif h != D1 and h != D2:
		W = W + h
		h = h - 1
	elif h == D1:
		W = W + h
		W = W - U1
		if W < 0:
			W = 0
		h = h - 1
	else:
		W = W + h
		W = W - U2
		if W < 0:
			W = 0
		h = h - 1


(w, h) = map(int, input().split())
(u, a) = map(int, input().split())
(v, b) = map(int, input().split())
while h:
	w = w + h
	if h == a:
		w = w - u
		if w < 0:
			w = 0
	if h == b:
		w = w - v
		if w < 0:
			w = 0
	h = h - 1
print(w)


(w, h) = map(int, input().split())
(s1, h1) = map(int, input().split())
(s2, h2) = map(int, input().split())
while h != 0:
	if h == h1:
		w += h
		h -= 1
		w -= s1
		if w < 0:
			w = 0
	elif h == h2:
		w += h
		h -= 1
		w -= s2
		if w < 0:
			w = 0
	else:
		w += h
		h -= 1
if w > 0:
	print(w)
else:
	print('0')


c = [int(i) for i in str(input()).split(' ')]
blocks1 = [int(i) for i in str(input()).split(' ')]
blocks2 = [int(i) for i in str(input()).split(' ')]
maxl = max(blocks1[1], blocks2[1])
h = c[1]
m = c[0]
for i in range(c[1]):
	if blocks1[1] == h:
		m += h
		m -= blocks1[0]
		if m <= 0:
			m = 0
	elif blocks2[1] == h:
		m += h
		m -= blocks2[0]
		if m <= 0:
			m = 0
	else:
		m += h
	h -= 1
print(m)


from collections import Counter
import math
(u, d) = map(int, input().split())
(u1, d1) = map(int, input().split())
(u2, d2) = map(int, input().split())
i = d
while i > 0:
	if i == d1:
		u = u + i - u1
	elif i == d2:
		u = u + i - u2
	else:
		u = u + i
	if u < 0:
		u = 0
	i -= 1
print(u)


(w, h) = map(int, input().split())
u = [0] * 2
d = [0] * 2
for i in range(2):
	(u[i], d[i]) = map(int, input().split())
while h != 0:
	w += h
	if h == d[0]:
		w = max(0, w - u[0])
	elif h == d[1]:
		w = max(0, w - u[1])
	h -= 1
print(w)


from collections import deque
import pdb

def rI():
	return [int(i) for i in input().split()]
(w, h) = rI()
(u1, d1) = rI()
(u2, d2) = rI()
out = w
for i in range(h, -1, -1):
	out += i
	if i == d1:
		out = max(out - u1, 0)
	if i == d2:
		out = max(out - u2, 0)
print(out)


(w, h) = map(int, input().split())
(u, d) = map(int, input().split())
(u1, d1) = map(int, input().split())
for i in range(h, -1, -1):
	if i == d:
		w = max(0, w - u + i)
	elif d1 == i:
		w = max(0, w - u1 + i)
	else:
		w += i
print(w)


(ow, oh) = map(int, input().split())
(u1, h1) = map(int, input().split())
(u2, h2) = map(int, input().split())
while oh != 0:
	ow += oh
	if oh == h1:
		ow -= u1
	elif oh == h2:
		ow -= u2
	if ow < 0:
		ow = 0
	oh -= 1
if ow <= 0:
	print(0)
else:
	print(ow)


str = input()
num = str.split()
w = int(num[0])
h = int(num[1])
u = []
d = []
for i in range(0, 2):
	str = input()
	num = str.split()
	u.append(int(num[0]))
	d.append(int(num[1]))
for i in range(0, h):
	w += h
	for j in range(0, 2):
		if h == d[j]:
			w -= u[j]
			if w < 0:
				w = 0
	h -= 1
print(w)


conv = lambda x: list(map(int, x.split()))
(wh, u1, u2) = (conv(input()), conv(input()), conv(input()))
while wh[1] > 0:
	wh[0] += wh[1]
	if u1[1] == wh[1]:
		wh[0] -= u1[0]
	if u2[1] == wh[1]:
		wh[0] -= u2[0]
	if wh[0] < 0:
		wh[0] = 0
	wh[1] -= 1
print(wh[0])


(w, h) = map(int, input().split())
(u1, d1) = map(int, input().split())
(u2, d2) = map(int, input().split())
if d2 > d1:
	(u1, d1, u2, d2) = (u2, d2, u1, d1)
(u, d) = (u1, d1)
while h > 0:
	w += h
	if h == d:
		w = max(0, w - u)
		(u, d) = (u2, d2)
	h -= 1
print(w)


t = [tuple(map(int, input().split())) for _ in range(3)]
w = t[0][0]
for i in range(t[0][1], 0, -1):
	w += i
	if i == t[1][1]:
		w = max(0, w - t[1][0])
	if i == t[2][1]:
		w = max(0, w - t[2][0])
print(w)


def ht(a, b):
	a = a - 1
	return (b * (b + 1) - a * (a + 1)) / 2
s1 = input()
(w, h) = s1.split()
w = int(w)
h = int(h)
s2 = input()
(w1, h1) = s2.split()
w1 = int(w1)
h1 = int(h1)
s3 = input()
(w2, h2) = s3.split()
w2 = int(w2)
h2 = int(h2)
if h1 > h2:
	temp = h2
	h2 = h1
	h1 = temp
	temp = w2
	w2 = w1
	w1 = temp
check = w + ht(h2, h) - w2
if check > 0:
	w = check
else:
	w = 0
check = w + ht(h1, h2 - 1) - w1
if check > 0:
	w = check
else:
	w = 0
w += ht(1, h1 - 1)
print(int(w))


snow = [int(i) for i in input().split()]
s1 = [int(i) for i in input().split()]
s2 = [int(i) for i in input().split()]
while snow[1] > 0:
	snow[0] += snow[1]
	if snow[1] == s1[1]:
		snow[0] -= s1[0]
	if snow[1] == s2[1]:
		snow[0] -= s2[0]
	if snow[0] < 0:
		snow[0] = 0
	snow[1] -= 1
print(snow[0])


def kom(w, h, u1, d1, u2, d2):
	for i in range(h, -1, -1):
		if w < 0:
			w = 0
		w += i
		if i == d1:
			w -= u1
		elif i == d2:
			w -= u2
	return w
(W, H) = [int(i) for i in input().split()]
(U1, D1) = [int(i) for i in input().split()]
(U2, D2) = [int(i) for i in input().split()]
print(kom(W, H, U1, D1, U2, D2))


(w, h) = map(int, input().split())
a1 = list(map(int, input().split()))
a2 = list(map(int, input().split()))
if a1[1] < a2[1]:
	(a1, a2) = (a2[:], a1[:])

def mySum(a, b):
	return (a + b) * (b - a + 1) // 2
w = max(0, w + mySum(a1[1], h) - a1[0])
w = max(0, w + mySum(a2[1], a1[1] - 1) - a2[0])
print(w + mySum(1, a2[1] - 1))


arr = []
for i in range(3):
	temp = input().split()
	arr.append(temp)
w = int(arr[0][0])
h = int(arr[0][1])
w1 = int(arr[1][0])
h1 = int(arr[1][1])
w2 = int(arr[2][0])
h2 = int(arr[2][1])
for i in range(h, -1, -1):
	w = w + i
	if h1 == i:
		w = w - w1
		if w < 0:
			w = 0
	elif h2 == i:
		w = w - w2
		if w < 0:
			w = 0
print(w)


import math

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return list(mi())
(w, h) = tuple(mi())
(u1, d1) = tuple(mi())
(u2, d2) = tuple(mi())
if d2 > d1:
	w = w + h * (h + 1) // 2 - (d2 - 1) * d2 // 2
	w = max(0, w - u2)
	w = w + (d2 - 1) * d2 // 2 - (d1 - 1) * d1 // 2
	w = max(0, w - u1)
	w = w + (d1 - 1) * d1 // 2
else:
	w = w + h * (h + 1) // 2 - (d1 - 1) * d1 // 2
	w = max(0, w - u1)
	w = w + (d1 - 1) * d1 // 2 - (d2 - 1) * d2 // 2
	w = max(0, w - u2)
	w = w + (d2 - 1) * d2 // 2
print(w)


ball = list(map(int, input().split()))
rock1 = list(map(int, input().split()))
rock2 = list(map(int, input().split()))
while ball[1] > 0:
	ball[0] += ball[1]
	if ball[1] == rock1[1]:
		ball[0] -= rock1[0]
	if ball[1] == rock2[1]:
		ball[0] -= rock2[0]
	if ball[0] < 0:
		ball[0] = 0
	ball[1] -= 1
print(ball[0])


(curr_w, curr_h) = map(int, input().split())
(u1, h1) = map(int, input().split())
(u2, h2) = map(int, input().split())
for i in range(curr_h, -1, -1):
	curr_w += i
	if i == h1:
		curr_w -= u1
	if i == h2:
		curr_w -= u2
	if curr_w < 0:
		curr_w = 0
if curr_w < 0:
	print(0)
else:
	print(curr_w)


snowball = input().split()
rock1 = input().split()
rock2 = input().split()
w = int(snowball[0])
h = int(snowball[1])
u1 = int(rock1[0])
d1 = int(rock1[1])
u2 = int(rock2[0])
d2 = int(rock2[1])
if d2 > d1:
	(d1, d2) = (d2, d1)
	(u1, u2) = (u2, u1)
while h > d1:
	w = w + h
	h = h - 1
if h == d1:
	w = w + h
	w = w - u1
	h = h - 1
	if w < 0:
		w = 0
while h > d2:
	w = w + h
	h = h - 1
if h == d2:
	w = w + h
	w = w - u2
	h = h - 1
	if w < 0:
		w = 0
while h > 0:
	w = w + h
	h = h - 1
print(w)


(w, h) = [int(i) for i in input().split()]
(u1, d1) = [int(i) for i in input().split()]
(u2, d2) = [int(i) for i in input().split()]
while h > -1:
	w += h
	if h == d1:
		w -= u1
		if w < 0:
			w = 0
	if h == d2:
		w -= u2
		if w < 0:
			w = 0
	h -= 1
print(w)


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
(x, y) = map(int, input().split())
for i in range(b, 0, -1):
	a += i
	if d == i:
		a -= c
	elif y == i:
		a -= x
	if a < 0:
		a = 0
print(a)


(w, h) = map(int, input().split())
(u1, d1) = map(int, input().split())
(u2, d2) = map(int, input().split())
if d1 < d2:
	tmp = d1
	d1 = d2
	d2 = tmp
	tmp = u1
	u1 = u2
	u2 = tmp
sum1 = (d1 + h) * (h - d1 + 1) // 2
sum2 = (d1 + d2) * (d1 - d2 + 1) // 2 - d1
sum3 = d2 * (d2 + 1) // 2 - d2
w += sum1
w -= u1
if w < 0:
	w = 0
w += sum2
w -= u2
if w < 0:
	w = 0
w += sum3
print(w)


R = lambda : [int(x) for x in input().split()]
(w, h) = R()
(wa, ha) = R()
(wb, hb) = R()
while h:
	w += h
	if ha == h:
		w = max(0, w - wa)
	if hb == h:
		w = max(0, w - wb)
	h -= 1
print(w)


(w, h) = [int(s) for s in input().split()]
sum_ = w
(u, d) = [int(s) for s in input().split()]
(u_1, d_1) = [int(s) for s in input().split()]
while h != 0:
	sum_ += h
	if h == d:
		if sum_ - u < 0:
			sum_ = 0
		else:
			sum_ -= u
	if h == d_1:
		if sum_ - u_1 < 0:
			sum_ = 0
		else:
			sum_ -= u_1
	h -= 1
print(int(sum_))


import bisect
import os
from collections import Counter
import bisect
from collections import defaultdict
import math
import random
import heapq as hq
from math import sqrt
import sys
from functools import reduce, cmp_to_key
from collections import deque
import threading
from itertools import combinations
from io import BytesIO, IOBase
from itertools import accumulate

def list_input():
	return list(map(int, input().split()))

def num_input():
	return map(int, input().split())

def string_list():
	return list(input())
(w, h) = num_input()
(w1, h1) = num_input()
(w2, h2) = num_input()
i = h
while i >= 0:
	w += i
	if h1 == i:
		w -= w1
		if w < 0:
			w = 0
	if h2 == i:
		w -= w2
		if w < 0:
			w = 0
	i -= 1
print(w)


ball = [int(n) for n in input().split(' ')]
stones = {'stone1': [int(n) for n in input().split(' ')], 'stone2': [int(n) for n in input().split(' ')]}
weight = ball[0]
for num in range(ball[1], 0, -1):
	weight += num
	if stones['stone1'][1] == num:
		weight -= stones['stone1'][0]
	if stones['stone2'][1] == num:
		weight -= stones['stone2'][0]
	if weight < 0:
		weight = 0
print(weight)


(current_w, current_h) = map(int, input().strip().split())
stone_list = [[int(i) for i in input().strip().split()]]
stone_list.append([int(i) for i in input().strip().split()])
stone_list.sort(key=lambda x: x[1], reverse=True)
for i in range(current_h, -1, -1):
	current_w += i
	if stone_list != [] and i == stone_list[0][1]:
		current_w -= stone_list[0][0]
		del stone_list[0]
	if current_w < 0:
		current_w = 0
print(current_w)


(w, h) = list(map(int, input().split()))
(u1, d1) = list(map(int, input().split()))
(u2, d2) = list(map(int, input().split()))
for i in range(h, 0, -1):
	w += i
	if i == d1:
		w -= u1
		if w < 0:
			w = 0
	elif i == d2:
		w -= u2
		if w < 0:
			w = 0
	else:
		continue
print(w)


(w, h) = map(int, input().split())
(w1, h1) = map(int, input().split())
(w2, h2) = map(int, input().split())
x = [[h1, w1], [h2, w2]]
x.sort()
ans = w
i = h
while i > 0:
	ans += i
	if i == x[1][0]:
		ans -= x[1][1]
		if ans < 0:
			ans = 0
	if i == x[0][0]:
		ans -= x[0][1]
		if ans < 0:
			ans = 0
	i -= 1
print(ans)


(w, h) = map(int, input().split())
p = (w1, h1) = map(int, input().split())
q = (w2, h2) = map(int, input().split())
if h2 < h1:
	temp1 = h2
	h2 = h1
	h1 = temp1
	temp = w2
	w2 = w1
	w1 = temp
j = h * (h + 1) // 2 - h2 * (h2 - 1) // 2 + w
if j - w2 < 0:
	j = 0
else:
	j = j - w2
k = h2 * (h2 - 1) // 2 - h1 * (h1 - 1) // 2 + j
if k - w1 < 0:
	k = 0
else:
	k = k - w1
d = h1 * (h1 - 1) // 2 + k
print(d)


w1 = list(map(int, input().split()))
w2 = list(map(int, input().split()))
w3 = list(map(int, input().split()))
w = int(w1[0])
h = int(w1[1])
u1 = int(w2[0])
d1 = int(w2[1])
u2 = int(w3[0])
d2 = int(w3[1])
NW = w
j = h
while j >= 0:
	if NW < 0:
		NW = 0
	if j == d1:
		NW -= u1
	if j == d2:
		NW -= u2
	NW += j
	j -= 1
print(NW)


import math
import sys
import bisect
input = sys.stdin.readline
t = 1
for _ in range(t):
	(w, h) = map(int, input().split())
	(u1, d1) = map(int, input().split())
	(u2, d2) = map(int, input().split())
	for i in range(h, -1, -1):
		w += i
		if d1 == i:
			w = max(0, w - u1)
		if d2 == i:
			w = max(0, w - u2)
	print(w)


(w, h) = map(int, input().split())
(w1, h1) = map(int, input().split())
(w2, h2) = map(int, input().split())
while h != 0:
	w += h
	if h == h1:
		w -= w1
	if h == h2:
		w -= w2
	if w < 0:
		w = 0
	h -= 1
if w < 0 and h == 0:
	print(0)
else:
	print(w)


(w, h) = map(int, input().split())
(u1, d1) = map(int, input().split())
(u2, d2) = map(int, input().split())
if d1 > d2:
	w = w + h
	l = 0
	for i in range(h):
		if i > d1 - 1:
			l = l + i
	w = w + l - u1
	if w < 0:
		w = 0
	m = 0
	for i in range(d1):
		if i > d2 - 1:
			m = m + i
	w = w + m - u2
	if w < 0:
		w = 0
	t = 0
	for i in range(d2):
		t = t + i
	print(w + t)
elif d2 > d1:
	w = w + h
	l = 0
	for i in range(h):
		if i > d2 - 1:
			l = l + i
	w = w + l - u2
	if w < 0:
		w = 0
	m = 0
	for i in range(d2):
		if i > d1 - 1:
			m = m + i
	w = w + m - u1
	if w < 0:
		w = 0
	t = 0
	for i in range(d1):
		t = t + i
	print(w + t)


(weight, h0) = map(int, input().split())
(w1, h1) = map(int, input().split())
(w2, h2) = map(int, input().split())
while h0 >= 0:
	weight = weight + h0
	if h0 == h1:
		weight = weight - w1
	if h0 == h2:
		weight = weight - w2
	if weight < 0:
		weight = 0
	h0 = h0 - 1
print(weight)


(w, h) = map(int, input().split())
(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
for i in range(h, 0, -1):
	w += i
	if b == i:
		if w > a:
			w -= a
		else:
			w = 0
	elif d == i:
		if w > c:
			w -= c
		else:
			w = 0
print(w)


l1 = input().split()
l2 = input().split()
l3 = input().split()
w = int(l1[0])
h = int(l1[1])
u1 = int(l2[0])
d1 = int(l2[1])
u2 = int(l3[0])
d2 = int(l3[1])
height = h
for i in range(height, 0, -1):
	w += i
	if d1 == i:
		w -= u1
		if w < 0:
			w = 0
	if d2 == i:
		w -= u2
		if w < 0:
			w = 0
print(w)


(u, h) = map(float, input().split())
(u1, h1) = map(float, input().split())
(u2, h2) = map(float, input().split())
if h1 > h2:
	(u1, u2) = (u2, u1)
	(h1, h2) = (h2, h1)
ans = int(u + (h - h2 + 1) * (h2 + h) // 2 - u2)
if ans < 0:
	ans = 0
ans += int((h2 - h1) * (h2 - 1 + h1) // 2 - u1)
if ans < 0:
	ans = 0
ans += int(h1 * (h1 - 1) // 2)
if ans < 0:
	print(0)
else:
	print(ans)


(w, h) = map(int, input().split())
x1 = tuple(map(int, input().split()))
x2 = tuple(map(int, input().split()))
u = [x1[0], x2[0]]
d = [x1[1], x2[1]]
while h >= 0:
	w += h
	if h in d:
		w -= u[d.index(h)]
	if w < 0:
		w = 0
	h -= 1
print(w)


(w, h) = (int(t) for t in input().split())
(u1, d1) = (int(t) for t in input().split())
(u2, d2) = (int(t) for t in input().split())
while h > 0:
	w += h
	if h == d1:
		w = 0 if u1 > w else w - u1
	elif h == d2:
		w = 0 if u2 > w else w - u2
	h -= 1
print(w)


(w, h) = map(int, input().split())
(s1, h1) = map(int, input().split())
(s2, h2) = map(int, input().split())
for x in range(h, -1, -1):
	w += x
	if x == h1:
		w -= s1
		if w < 0:
			w = 0
	elif x == h2:
		w -= s2
		if w < 0:
			w = 0
print(w)


import sys
(w, h) = map(int, sys.stdin.readline().split())
(u1, d1) = map(int, sys.stdin.readline().split())
(u2, d2) = map(int, sys.stdin.readline().split())
if d2 > d1:
	(d1, d2) = (d2, d1)
	(u1, u2) = (u2, u1)
flag = True
while h > 0:
	w += h
	if flag:
		if h == d1:
			w -= u1
			flag = False
	elif h == d2:
		w -= u2
	h -= 1
	w = max(0, w)
print(w)


wh = [int(x) for x in input().split()]
(w, h) = (wh[0], wh[1])
u1d1 = [int(x) for x in input().split()]
(u1, d1) = (u1d1[0], u1d1[1])
u2d2 = [int(x) for x in input().split()]
(u2, d2) = (u2d2[0], u2d2[1])
for h1 in range(h, 0, -1):
	w += h1
	if h1 == d1:
		w -= u1
	if h1 == d2:
		w -= u2
	if w < 0:
		w = 0
print(w)


(w, h) = list(map(int, input().split()))
(w1, d) = list(map(int, input().split()))
(w2, d1) = list(map(int, input().split()))
for i in range(h, 0, -1):
	w += i
	if i == d:
		w = max(w - w1, 0)
	if i == d1:
		w = max(w - w2, 0)
print(w)


(w1, h1) = map(int, input().split())
(u1, d1) = map(int, input().split())
(u2, d2) = map(int, input().split())
while h1 > 0:
	w1 = w1 + h1
	if h1 == d1:
		w1 = w1 - u1
		if w1 < 0:
			w1 = 0
	elif h1 == d2:
		w1 = w1 - u2
		if w1 < 0:
			w1 = 0
	h1 = h1 - 1
print(w1)


(w, h) = [int(c) for c in input().split()]
(w1, h1) = [int(x) for x in input().split()]
(w2, h2) = [int(x) for x in input().split()]
while h:
	w += h
	if h == h1:
		w -= w1
	if h == h2:
		w -= w2
	w = max(0, w)
	h -= 1
print(w)


(w, h) = map(int, input().split())
(u1, d1) = map(int, input().split())
(u2, d2) = map(int, input().split())
a = []
b = []
if d1 > d2:
	a.append(d1)
	a.append(d2)
	b.append(u1)
	b.append(u2)
else:
	a.append(d2)
	b.append(u2)
	a.append(d1)
	b.append(u1)
s1 = h - a[0]
for i in range(s1 + 1):
	w = w + (h - i)
	if w < 0:
		w = 0
w = w - b[0]
if w < 0:
	w = 0
for i in range(a[0] - a[1]):
	w = w + (a[0] - i - 1)
	if w < 0:
		w = 0
w = w - b[1]
if w < 0:
	w = 0
for i in range(a[1]):
	w = w + (a[1] - i - 1)
	if w < 0:
		w = 0
print(w)


(w, h) = input().split()
(w1, h1) = input().split()
(w2, h2) = input().split()
w = int(w)
h = int(h)
w1 = int(w1)
w2 = int(w2)
h1 = int(h1)
h2 = int(h2)
hs = []
for i in range(h, -1, -1):
	hs.append(i)
tmp = w
hst = [h1, h2]
wst = [w1, w2]
for i in hs:
	tmp += i
	if i in hst:
		tmp -= wst[hst.index(i)]
		if tmp < 0:
			tmp = 0
print(tmp)


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
(e, f) = map(int, input().split())
x = [0] * b
y = []
for i in range(1, b + 1):
	y.append(i)
x[d - 1] = c
x[f - 1] = e
s = a
while 1:
	h = y[len(y) - 1]
	s = s + h
	s = s - x[len(x) - 1]
	if s < 0:
		s = 0
	del x[len(x) - 1]
	del y[len(y) - 1]
	if len(x) == 0:
		break
print(s)


(w, h) = map(int, input().split())
(u1, d1) = map(int, input().split())
(u2, d2) = map(int, input().split())
if d1 > d2:
	w += h * (h + 1) // 2 - d1 * (d1 - 1) // 2
	w -= u1
	w = max(w, 0)
	h = d1
	w += h * (h - 1) // 2 - d2 * (d2 - 1) // 2
	w -= u2
	w = max(w, 0)
	h = d2
	w += h * (h - 1) // 2
else:
	w += h * (h + 1) // 2 - d2 * (d2 - 1) // 2
	w -= u2
	w = max(w, 0)
	h = d2
	w += h * (h - 1) // 2 - d1 * (d1 - 1) // 2
	w -= u1
	w = max(w, 0)
	h = d1
	w += h * (h - 1) // 2
print(w)


from math import *
from itertools import *
from collections import *
from functools import reduce

def input_ints():
	return map(int, input().split())

def input_floats():
	return map(float, input().split())

def input_strings():
	return input().split()
(w, h) = input_ints()
(u1, d1) = input_ints()
(u2, d2) = input_ints()
while h > 0:
	w += h
	if h == d1:
		w -= u1
	if h == d2:
		w -= u2
	w = max(w, 0)
	h -= 1
print(w)


x = input('').split()
y = input('').split()
z = input('').split()
w = eval(x[0])
for i in range(eval(x[1]), 0, -1):
	if w < 0:
		w = 0
	if i == eval(y[1]):
		w = w - eval(y[0])
		w = w + eval(y[1])
	elif i == eval(z[1]):
		w = w - eval(z[0])
		w = w + eval(z[1])
	else:
		w = w + i
if w <= 0:
	print(0)
else:
	print(w)


import string

def main_function():
	(w, h) = [int(i) for i in input().split(' ')]
	(u1, d1) = [int(i) for i in input().split(' ')]
	(u2, d2) = [int(i) for i in input().split(' ')]
	current_weight = w
	for i in range(h):
		j = h - i
		current_weight += j
		if j == d1:
			current_weight -= u1
			if current_weight < 0:
				current_weight = 0
		elif j == d2:
			current_weight -= u2
			if current_weight < 0:
				current_weight = 0
	return current_weight
print(main_function())


(w, h) = [int(s) for s in input().split()]
(u1, d1) = [int(s) for s in input().split()]
(u2, d2) = [int(s) for s in input().split()]
if d1 > h or d1 == 0:
	u1 = 0
	d1 = 0
if d2 > h or d2 == 0:
	u2 = 0
	d2 = 0
if d1 < d2:
	(d1, d2) = (d2, d1)
	(u1, u2) = (u2, u1)
w = max(0, w + (d1 + h) * (h - d1 + 1) // 2 - u1)
w = max(0, w + (d2 + d1 - 1) * (d1 - d2) // 2 - u2)
w += (d2 - 1) * d2 // 2
print(w)


(w, h) = map(int, input().split())
(w1, h1) = map(int, input().split())
(w2, h2) = map(int, input().split())
if h1 > h2:
	ht = h1
	h1 = h2
	h2 = ht
	wt = w1
	w1 = w2
	w2 = wt
a = h
for i in range(h - h2 + 1):
	w += a
	a -= 1
if w > w2:
	w -= w2
else:
	w = 0
a = h2 - 1
for i in range(h2 - h1):
	w += a
	a -= 1
if w > w1:
	w -= w1
else:
	w = 0
a = h1 - 1
for i in range(h1):
	w += a
	a -= 1
print(w)


import sys
from math import ceil
read = sys.stdin.buffer.readline
mi = lambda : map(int, read().split())
li = lambda : list(mi())
cin = lambda : int(read())
for _ in range(1):
	(w, h) = mi()
	(u1, d1) = mi()
	(u2, d2) = mi()
	for i in range(h, 0, -1):
		w += i
		if i == d1:
			w -= u1
			w = max(0, w)
		if i == d2:
			w -= u2
			w = max(0, w)
	print(w)


(iw, ih) = map(int, input().split())
(sw, sh) = map(int, input().split())
(rw, rh) = map(int, input().split())
for i in range(ih):
	if ih != 0:
		iw = iw + ih
		if ih == sh:
			iw = iw - sw
		if ih == rh:
			iw = iw - rw
		if iw <= 0:
			iw = 0
	ih = ih - 1
print(iw)
quit()


kom = input().split()
k1 = input().split()
k2 = input().split()
w = int(kom[0])
h = int(kom[1])
w1 = int(k1[0])
h1 = int(k1[1])
w2 = int(k2[0])
h2 = int(k2[1])
i = h
while True:
	if i != h1 and i != h2:
		w += i
		i -= 1
	elif i == h1:
		w += i
		w = w - w1
		i -= 1
	elif i == h2:
		w += i
		w = w - w2
		i -= 1
	if w <= 0:
		w = 0
	if i == 0:
		print(w)
		break


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
(a1, b1) = map(int, input().split())
for i in range(-b, 0):
	a -= i
	if i == -d:
		if a > c:
			a -= c
		else:
			a = 0
	if i == -b1:
		if a > a1:
			a -= a1
		else:
			a = 0
print(a)


(w, h) = map(int, input().split())
(u1, d1) = map(int, input().split())
(u2, d2) = map(int, input().split())
i = h
while True:
	w += h
	if h == 0:
		break
	if h == d1:
		if w > u1:
			w -= u1
		else:
			w = 0
		h -= 1
	elif h == d2:
		if w > u2:
			w -= u2
		else:
			w = 0
		h -= 1
	else:
		h -= 1
if w <= 0:
	print(0)
else:
	print(w)


def calc(sb, s1, s2):
	(w, h) = (sb[0], sb[1])
	(u1, d1) = (s1[0], s1[1])
	(u2, d2) = (s2[0], s2[1])
	while h >= 0:
		if w < 0:
			w = 0
		w += h
		if h == d1:
			w -= u1
		if h == d2:
			w -= u2
		h -= 1
	if w <= 0:
		return 0
	return w
sb = list(map(int, input().split()))
s1 = list(map(int, input().split()))
s2 = list(map(int, input().split()))
print(calc(sb, s1, s2))


(h, w) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
for i in range(w, 0, -1):
	h += i
	if a[1] == i:
		if a[0] >= h:
			h = 0
		else:
			h -= a[0]
	if b[1] == i:
		if b[0] >= h:
			h = 0
		else:
			h -= b[0]
print(h)


x = input()
w = int(x.split()[0])
h = int(x.split()[1])
mass = w
height = h
x = input()
m1 = int(x.split()[0])
h1 = int(x.split()[1])
x = input()
m2 = int(x.split()[0])
h2 = int(x.split()[1])
while height > 0:
	mass += height
	if height == h1:
		mass -= m1
		if mass < 0:
			mass = 0
	elif height == h2:
		mass -= m2
		if mass < 0:
			mass = 0
	height -= 1
print(mass)


l = input()
arr = l.split(' ')
w = int(arr[0])
h = int(arr[1])
l = input()
arr = l.split(' ')
u1 = int(arr[0])
d1 = int(arr[1])
l = input()
arr = l.split(' ')
u2 = int(arr[0])
d2 = int(arr[1])
i = h
while i > 0:
	w = w + i
	if i == d1:
		w = w - u1
		if w <= 0:
			w = 0
	if i == d2:
		w = w - u2
		if w <= 0:
			w = 0
	i = i - 1
print(w)


(w, h) = map(int, input().split())
(w1, h1) = map(int, input().split())
(w2, h2) = map(int, input().split())
if h2 > h1:
	print(max(0, max(0, w + (h2 + h) * (h - h2 + 1) // 2 - w2) + (h2 - 1 + h1) * (h2 - h1) // 2 - w1) + (h1 - 1) * h1 // 2)
else:
	print(max(0, max(0, w + (h1 + h) * (h - h1 + 1) // 2 - w1) + (h2 - 1 + h1) * (h1 - h2) // 2 - w2) + (h2 - 1) * h2 // 2)


temp = list(map(int, input().split(' ')))
w = temp[0]
h = temp[1]
temp = list(map(int, input().split(' ')))
u1 = temp[0]
d1 = temp[1]
temp = list(map(int, input().split(' ')))
u2 = temp[0]
d2 = temp[1]
while h >= 0:
	w = w + h
	if h == d2:
		w = w - u2
		if w < 0:
			w = 0
	if h == d1:
		w = w - u1
		if w < 0:
			w = 0
	h -= 1
print(w)


from math import *
from copy import *
from string import *
from sys import stdin
from itertools import *
from collections import Counter
(W, H) = map(int, input().split())
(u1, d1) = map(int, input().split())
(u2, d2) = map(int, input().split())
ans = 0
h = deepcopy(H)
for i in range(H, 0, -1):
	W += i
	if i == d1:
		W -= u1
	if i == d2:
		W -= u2
	if W < 0:
		W = 0
if W <= 0:
	print(0)
else:
	print(W)


(weight, height) = map(int, input().split())
total_summation = height * (height + 1) // 2
(first_stone, first_height) = map(int, input().split())
(second_stone, second_height) = map(int, input().split())
if second_height < first_height:
	(second_height, first_height) = (first_height, second_height)
	(second_stone, first_stone) = (first_stone, second_stone)
first_height = first_height - 1
second_height = second_height - 1
d = first_height * (first_height + 1) // 2
f = second_height * (second_height + 1) // 2
weight = weight + (total_summation - f) - second_stone
if weight < 0:
	weight = 0
weight = weight + (f - d) - first_stone
if weight < 0:
	weight = 0
weight = weight + d
print(weight)


gi = lambda : list(map(int, input().split()))
(w, h) = gi()
(u1, d1) = gi()
(u2, d2) = gi()
while h:
	w += h
	if h == d1:
		w -= u1
	if h == d2:
		w -= u2
	w = max(w, 0)
	h -= 1
print(w)


(n, t) = input().split()
t = int(t)
n = int(n)
(i1, i2) = input().split()
(j1, j2) = input().split()
i1 = int(i1)
i2 = int(i2)
j1 = int(j1)
j2 = int(j2)
if i2 > j2:
	(i2, j2) = (j2, i2)
	(i1, j1) = (j1, i1)
j2 -= 1
n += t * (t + 1) // 2 - j2 * (j2 + 1) // 2
n -= j1
if n < 0:
	n = 0
i2 -= 1
n += j2 * (j2 + 1) // 2 - i2 * (i2 + 1) // 2
n -= i1
if n < 0:
	n = 0
n += i2 * (i2 + 1) // 2
print(n)


(snowWeight, snowHeight) = input().split(' ')
(stoneW1, stoneH1) = input().split(' ')
(stoneW2, stoneH2) = input().split(' ')
snowWeight = int(snowWeight)
snowHeight = int(snowHeight)
stoneW1 = int(stoneW1)
stoneH1 = int(stoneH1)
stoneW2 = int(stoneW2)
stoneH2 = int(stoneH2)
for i in range(snowHeight, -1, -1):
	snowWeight += i
	if i == stoneH1:
		snowWeight -= stoneW1
	elif i == stoneH2:
		snowWeight -= stoneW2
	if snowWeight < 0:
		snowWeight = 0
print(snowWeight)


def zero(a):
	if a < 0:
		return 0
	else:
		return a
(w, h) = map(int, input().split())
(w1, h1) = map(int, input().split())
(w2, h2) = map(int, input().split())
for i in range(h):
	w += h
	if h == h1:
		w = zero(w - w1)
	if h == h2:
		w = zero(w - w2)
	h -= 1
print(w)


import sys
input = sys.stdin.readline

def main():
	(w, h) = map(int, input().split())
	(u1, d1) = map(int, input().split())
	(u2, d2) = map(int, input().split())
	if d1 < d2:
		(u1, u2) = (u2, u1)
		(d1, d2) = (d2, d1)
	while h > 0:
		w += h
		if h == d1:
			w = max(0, w - u1)
		elif h == d2:
			w = max(0, w - u2)
		h -= 1
	print(w)
main()


l = list(map(int, input().split()))
w = l[0]
h = l[1]
p = list(map(int, input().split()))
k = list(map(int, input().split()))
for i in range(h, -1, -1):
	w += i
	if i == p[1]:
		if p[0] > w:
			w = 0
		else:
			w -= p[0]
	elif i == k[1]:
		if k[0] > w:
			w = 0
		else:
			w -= k[0]
print(w)


from sys import stdin
(w, h) = map(int, stdin.readline().split())
(u1, d1) = map(int, stdin.readline().split())
(u2, d2) = map(int, stdin.readline().split())
for i in range(h, 0, -1):
	w += i
	if i == d1:
		w = max(0, w - u1)
	if i == d2:
		w = max(0, w - u2)
print(w)


import sys
(w, h) = map(int, input().split())
(u1, d1) = map(int, input().split())
(u2, d2) = map(int, input().split())
while h > 0:
	w += h
	if d1 == h:
		w -= u1
	if d2 == h:
		w -= u2
	if w < 0:
		w = 0
	h -= 1
print(w)


import sys
import math, bisect
sys.setrecursionlimit(10 ** 6)
from itertools import groupby, accumulate
from heapq import heapify, heappop, heappush
from collections import deque, Counter, defaultdict

def I():
	return int(sys.stdin.readline())

def neo():
	return map(int, sys.stdin.readline().split())

def Neo():
	return list(map(int, sys.stdin.readline().split()))
(w, h) = neo()
(u1, d1) = neo()
(u2, d2) = neo()
while h > 0:
	w += h
	if d1 == h:
		w = max(0, w - u1)
	elif d2 == h:
		w = max(0, w - u2)
	h -= 1
print(w)


(snowball_weight, snowball_height) = [int(x) for x in input().split()]
(stone1_weight, stone1_height) = [int(x) for x in input().split()]
(stone2_weight, stone2_height) = [int(x) for x in input().split()]
while snowball_height > 0:
	snowball_weight += snowball_height
	if snowball_height == stone1_height:
		snowball_weight = max(snowball_weight - stone1_weight, 0)
	if snowball_height == stone2_height:
		snowball_weight = max(snowball_weight - stone2_weight, 0)
	snowball_height -= 1
print(snowball_weight)


(a1, b1) = map(int, input().split())
(a2, b2) = map(int, input().split())
(a3, b3) = map(int, input().split())
if b2 < b3:
	(a2, a3) = (a3, a2)
	(b2, b3) = (b3, b2)
while b1 >= b2:
	a1 += b1
	b1 -= 1
a1 -= a2
if a1 < 0:
	a1 = 0
while b1 >= b3:
	a1 += b1
	b1 -= 1
a1 -= a3
if a1 < 0:
	a1 = 0
while b1 >= 0:
	a1 += b1
	b1 -= 1
print(a1)


(sw, sh) = map(int, input().split())
(s1w, s1h) = map(int, input().split())
(s2w, s2h) = map(int, input().split())
while sh != 0:
	if sh == s1h:
		sw += sh
		sh -= 1
		sw -= s1w
		if sw < 0:
			sw = 0
	elif sh == s2h:
		sw += sh
		sh -= 1
		sw -= s2w
		if sw < 0:
			sw = 0
	else:
		sw += sh
		sh -= 1
print(sw)


(w, h) = [int(i) for i in input().split()]
(w1, d1) = [int(i) for i in input().split()]
(u2, d2) = [int(i) for i in input().split()]
t = w
for i in range(h, -1, -1):
	t += i
	if i == d1:
		t = max(0, t - w1)
	if i == d2:
		t = max(0, t - u2)
print(t)


def main():
	(w, h) = list(map(int, input().split()))
	(w1, h1) = list(map(int, input().split()))
	(w2, h2) = list(map(int, input().split()))
	for i in range(h, 0, -1):
		w += i
		if h1 == i:
			w -= w1
		if h2 == i:
			w -= w2
		if w < 0:
			w = 0
	print(w)
main()


(w, h) = list(map(int, input().split()))
(u1, d1) = list(map(int, input().split()))
(u2, d2) = list(map(int, input().split()))
ans = w
while h > 0:
	ans += h
	if h == d1:
		ans = max(0, ans - u1)
	elif h == d2:
		ans = max(0, ans - u2)
	h -= 1
print(ans)


(n, k) = input().split()
n = int(n)
k = int(k)
(n1, k1) = input().split()
n1 = int(n1)
k1 = int(k1)
(n2, k2) = input().split()
n2 = int(n2)
k2 = int(k2)
h = k
for i in range(h, 0, -1):
	n = n + i
	if i == k1:
		n = max(0, n - n1)
	elif i == k2:
		n = max(0, n - n2)
print(n)


entrada1 = input()
(w, h) = (int(entrada1.split()[0]), int(entrada1.split()[1]))
entrada2 = input()
(u1, d1) = (int(entrada2.split()[0]), int(entrada2.split()[1]))
entrada3 = input()
(u2, d2) = (int(entrada3.split()[0]), int(entrada3.split()[1]))
for i in range(h + 1)[::-1]:
	w = w + h
	if h == d1:
		w = max(w - u1, 0)
	if h == d2:
		w = max(w - u2, 0)
	h = h - 1
print(w)