Vasya works as a watchman in the gallery. Unfortunately, one of the most expensive paintings was stolen while he was on duty. He doesn't want to be fired, so he has to quickly restore the painting. He remembers some facts about it.  The painting is a square 3 × 3, each cell contains a single integer from 1 to n, and different cells may contain either different or equal integers.  The sum of integers in each of four squares 2 × 2 is equal to the sum of integers in the top left square 2 × 2.  Four elements a, b, c and d are known and are located as shown on the picture below. $\left. \begin{array}{|c|c|c|} \hline ? & {a} & {?} \\ \hline b & {?} & {c} \\ \hline ? & {d} & {?} \\ \hline \end{array} \right.$

Help Vasya find out the number of distinct squares the satisfy all the conditions above. Note, that this number may be equal to 0, meaning Vasya remembers something wrong.

Two squares are considered to be different, if there exists a cell that contains two different integers in different squares.


-----Input-----

The first line of the input contains five integers n, a, b, c and d (1 ≤ n ≤ 100 000, 1 ≤ a, b, c, d ≤ n) — maximum possible value of an integer in the cell and four integers that Vasya remembers.


-----Output-----

Print one integer — the number of distinct valid squares.


-----Examples-----
Input
2 1 1 1 2

Output
2

Input
3 3 1 2 3

Output
6



-----Note-----

Below are all the possible paintings for the first sample. $\left. \begin{array}{|l|l|l|} \hline 2 & {1} & {2} \\ \hline 1 & {1} & {1} \\ \hline 1 & {2} & {1} \\ \hline \end{array} \right.$ $\left. \begin{array}{|l|l|l|} \hline 2 & {1} & {2} \\ \hline 1 & {2} & {1} \\ \hline 1 & {2} & {1} \\ \hline \end{array} \right.$

In the second sample, only paintings displayed below satisfy all the rules. $\left. \begin{array}{|c|c|c|} \hline 2 & {3} & {1} \\ \hline 1 & {1} & {2} \\ \hline 2 & {3} & {1} \\ \hline \end{array} \right.$ $\left. \begin{array}{|c|c|c|} \hline 2 & {3} & {1} \\ \hline 1 & {2} & {2} \\ \hline 2 & {3} & {1} \\ \hline \end{array} \right.$ $\left. \begin{array}{|l|l|l|} \hline 2 & {3} & {1} \\ \hline 1 & {3} & {2} \\ \hline 2 & {3} & {1} \\ \hline \end{array} \right.$ $\left. \begin{array}{|c|c|c|} \hline 3 & {3} & {2} \\ \hline 1 & {1} & {2} \\ \hline 3 & {3} & {2} \\ \hline \end{array} \right.$ $\left. \begin{array}{|c|c|c|} \hline 3 & {3} & {2} \\ \hline 1 & {2} & {2} \\ \hline 3 & {3} & {2} \\ \hline \end{array} \right.$ $\left. \begin{array}{|c|c|c|} \hline 3 & {3} & {2} \\ \hline 1 & {3} & {2} \\ \hline 3 & {3} & {2} \\ \hline \end{array} \right.$

Difficulty: MEDIUM
Tags: Complete search

Mathematics

Constructive algorithms
Skill types: Complete search

Starter code:

Solutions:
inpdata = input().split()
n = int(inpdata[0])
a = int(inpdata[1])
b = int(inpdata[2])
c = int(inpdata[3])
d = int(inpdata[4])
ans = 0
for x in range(1, n + 1):
	y = x + b - c
	z = x + a - d
	w = a + y - d
	if 1 <= y and y <= n and (1 <= z) and (z <= n) and (1 <= w) and (w <= n):
		ans += 1
ans = ans * n
print(ans)


(n, a, b, c, d) = map(int, input().split())
res = 0
for i in range(1, n + 1):
	sum = i + a + b
	x = sum - a - c
	y = sum - c - d
	z = sum - b - d
	if max(x, y, z) <= n and min(x, y, z) >= 1:
		res += n
print(res)


(n, a, b, c, d) = map(int, input().split())
p1 = a + b
p2 = a + c
p3 = b + d
p4 = c + d
minimum = min(p1, p2, p3, p4)
maximum = max(p1, p2, p3, p4)
if maximum - minimum >= n:
	print(0)
else:
	total = (n - (maximum - minimum)) * n
	print(total)


(n, a, b, c, d) = map(int, input().split())
ans = 0
for x in range(1, n + 1):
	y = x + b - c
	w = y + a - d
	z = x + a - d
	ans += 1 <= y <= n and 1 <= z <= n and (1 <= w <= n)
ans *= n
print(ans)


(n, a, b, c, d) = map(int, input().split())
ans = 0
for x1 in range(1, n + 1):
	x2 = x1 + b - c
	x4 = a + x1 - d
	x5 = b + x4 - c
	if 1 <= x2 <= n and 1 <= x4 <= n and (1 <= x5 <= n):
		ans += 1
print(ans * n)


(n, a, b, c, d) = map(int, input().split())
count = 0
for x in range(1, n + 1):
	x2 = x + b - c
	x4 = x + a - d
	x5 = x + a + b - c - d
	if n >= x2 >= 1 and n >= x4 >= 1 and (n >= x5 >= 1):
		count += 1
print(count * n)


(n, a, b, c, d) = map(int, input().split(' '))
if any(((i > n) | (i < 1) for i in [a, b, c, d])):
	print(0)
elif not (a + b - (c + d) >= 1 - n) & (a + b - (c + d) <= n - 1) & (a + c - (b + d) >= 1 - n) & (a + c - (b + d) <= n - 1):
	print(0)
else:
	min_x1 = max(1 + (c - b), 1 + (d - a), 1 + (c + d) - (a + b), 1)
	max_x1 = min(n + (c - b), n + (d - a), n + (c + d) - (a + b), n)
	range_x1 = max_x1 - min_x1 + 1
	range_x3 = n
	answer = range_x3 * range_x1
	print(answer)


import math, sys, bisect, heapq
from collections import defaultdict, Counter, deque
from itertools import groupby, accumulate
from functools import lru_cache
int1 = lambda x: int(x) - 1
input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
aj = lambda : list(map(int, input().split()))

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def Y(c):
	print(['NO', 'YES'][c])

def y(c):
	print(['no', 'yes'][c])

def Yy(c):
	print(['No', 'Yes'][c])
(n, a, b, c, d) = aj()
ans = 0
for p in range(1, n + 1):
	q = p + b - c
	r = p + a + b - c - d
	s = p + a - d
	if 1 <= q <= n and 1 <= r <= n and (1 <= s <= n):
		ans += 1
print(ans * n)


inp = input().split()
(n, a, b, c, d) = (int(inp[0]), int(inp[1]), int(inp[2]), int(inp[3]), int(inp[4]))

def equal(e, a, b, c, d):
	return e + b - c <= n and e + a - d <= n and (e + a + b - c - d <= n) and (e + b - c > 0) and (e + a - d > 0) and (e + a + b - c - d > 0)
count = 0
for e in range(1, n + 1):
	if equal(e, a, b, c, d):
		count += n
print(count)


(n, a, b, c, d) = tuple(map(int, input().split()))
ans = 0
sum = 0

def ch(c):
	return n >= c and c > 0
for i in range(1, n + 1):
	sum = i + a + b
	x2 = sum - a - c
	x3 = sum - c - d
	x4 = sum - b - d
	if ch(i) and ch(x2) and ch(x3) and ch(x4):
		ans += 1
print(ans * n)


inpt = list(map(int, input().split(' ')))
n = inpt[0]
a = inpt[1]
b = inpt[2]
c = inpt[3]
d = inpt[4]
count = 0
for i in range(n):
	w = i + 1
	x = b - c + w
	z = a - d + w
	y = a - d + x
	if 1 <= x <= n and 1 <= z <= n and (1 <= y <= n):
		count += 1
print(count * n)


(n, a, b, c, d) = map(int, input().split())
counter = 0
for y in range(1, n + 1):
	k = y + b - c
	if k > n or k < 1:
		continue
	w = y + a - d
	if w > n or w < 1:
		continue
	f = y + a + b - c - d
	if f > n or f < 1:
		continue
	counter += 1
print(counter * n)


def main():
	(n, a, b, c, d) = [int(x) for x in input().split()]
	cnt = 0
	for x in range(1, n + 1):
		z1 = x + b - c
		z2 = x + a + b - d - c
		z3 = x + a - d
		if all((1 <= xx <= n for xx in (z1, z2, z3))):
			cnt += n
	print(cnt)

def __starting_point():
	main()
__starting_point()


(n, a, b, c, d) = map(int, input().split())
ans = 0
for i in range(1, n + 1):
	s = a + b + i
	j = s - a - c
	k = s - b - d
	l = s - c - d
	if 1 <= j <= n and 1 <= k <= n and (1 <= l <= n):
		ans += n
print(ans)


(n, a, b, c, d) = map(int, input().split())
ans = 0
l = [a + b, a + c, b + d, d + c]
sum = min(l) + n - (max(l) + 1) + 1
ans = sum * n
if ans < 0:
	ans = 0
print(ans)


import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from heapq import *
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
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')
ALPHA = 'abcdefghijklmnopqrstuvwxyz'
MOD = 1000000007

def value():
	return tuple(map(int, input().split()))

def array():
	return [int(i) for i in input().split()]

def Int():
	return int(input())

def Str():
	return input()

def arrayS():
	return [i for i in input().split()]
(n, a, b, c, d) = value()
ans = 0
minSum = a + b + 2
maxSum = a + b + 2 * n
for mid in range(1, n + 1):
	box1 = (a + b + mid + 1, a + b + mid + n)
	box2 = (a + c + mid + 1, a + c + mid + n)
	box3 = (c + d + mid + 1, c + d + mid + n)
	box4 = (d + b + mid + 1, d + b + mid + n)
	minSum = max(box1[0], box2[0], box3[0], box4[0])
	maxSum = min(box1[1], box2[1], box3[1], box4[1])
	ans += max(maxSum - minSum + 1, 0)
print(ans)


(n, a, b, c, d) = map(int, input().split())
m = [0, b - c, a - d, a + b - c - d]
count = max([0, n - max(m)]) - min([n, -min(m)])
if count <= 0:
	print(0)
else:
	print(n * count)


(n, a, b, c, d) = map(int, input().split())
min_sum = max(a + b, a + c, b + d, c + d) + 1
max_sum = min(a + b, a + c, b + d, c + d) + n
print(max((max_sum - min_sum + 1) * n, 0))


(n, a, b, c, d) = list(map(int, input().split(' ')))
print(max(0, n * (n - max([a + b, a + c, c + d, b + d]) + min([a + b, a + c, c + d, b + d]))))


[n, a, b, c, d] = list(map(int, input().split()))
res = 0
for p in range(1, n + 1):
	q = p + b - c
	if q <= 0 or q > n:
		continue
	s = p + a - d
	if s <= 0 or s > n:
		continue
	t = a + q - d
	if t <= 0 or t > n:
		continue
	res = res + n
print(res)


(n, a, b, c, d) = (int(_) for _ in input().split())
print(max(n - abs(a - d) - abs(b - c), 0) * n)


(n, a, b, c, d) = list(map(int, input().split()))
arr = [0] * 4
arr[0] = a + b
arr[1] = a + c
arr[2] = c + d
arr[3] = b + d
arr.sort()
if n + arr[0] - arr[3] >= 0:
	print(n * (n + arr[0] - arr[3]))
else:
	print(0)


def f(n, r, p, q):
	if r < 1 or r > n:
		return 0
	if p < 1 or p > n:
		return 0
	if q < 1 or q > n:
		return 0
	return 1
(n, a, b, c, d) = [int(i) for i in input().split()]
box = 0
for i in range(1, n + 1):
	r = i + b - c
	p = r + a - d
	q = p + c - b
	box += f(n, r, p, q)
box *= n
print(box)


(n, a, b, c, d) = map(int, input().split())
q = 1
w = -(b + d - a - b) + q
e = -(c + d - a - b) + q
r = -(c + a - a - b) + q
x = n - max(q, w, e, r) + 1
y = 1 - min(q, w, e, r)
print(max(0, (x - y) * n))


from collections import *
(n, a, b, c, d) = list(map(int, input().split()))
print((n - min(n, max(abs(a + b - c - d), abs(b - c), abs(a - d), abs(a + c - b - d)))) * n)


from re import *
from sys import stderr

def readint():
	return int(input())

def readfloat():
	return float(input())

def readarray(N, foo=input):
	return [foo() for i in range(N)]

def readlinearray(foo=int):
	return map(foo, input().split())

def NOD(a, b):
	while b:
		(a, b) = (b, a % b)
	return a

def gen_primes(max):
	primes = [1] * (max + 1)
	for i in range(2, max + 1):
		if primes[i]:
			for j in range(i + i, max + 1, i):
				primes[j] = 0
	primes[0] = 0
	return [x for x in range(max + 1) if primes[x]]

def is_prime(N):
	i = 3
	if not N % 2:
		return 0
	while i * i < N:
		if not N % i:
			return 0
		i += 3
	return 1
(n, a, b, c, d) = readlinearray()
maxx = min(n, n - b + c, n - a + d, n - a + c - b + d)
minx = max(1, 1 - b + c, 1 - a + d, 1 - a + c - b + d)
if maxx < minx:
	print(0)
else:
	print((maxx - minx + 1) * n)


array = input().split()
number = int(array[0])
nArray = []
for i in array:
	nArray.append(int(i))
nArray.pop(0)
count = 0
count = max(0, number - abs(nArray[3] - nArray[0]) - abs(nArray[2] - nArray[1]))
print(count * number)


(n, a, b, c, d) = map(int, input().split())
res = 0
for i in range(1, n + 1):
	tl = i
	tr = tl - c + b
	br = tr - d + a
	bl = br - b + c
	if min(tl, tr, br, bl) >= 1 and max(tl, tr, br, bl) <= n:
		res += n
print(res)


(n, a, b, c, d) = input().split()
n = int(n)
a = int(a)
b = int(b)
c = int(c)
d = int(d)
diff2 = b - c
diff4 = a - d
diff5 = a + b - c - d
(lower, _, upper) = sorted((diff2, diff4, diff5))
lower_bound = 1 - min(0, lower)
upper_bound = n - max(0, upper)
if lower_bound > upper_bound:
	print(0)
else:
	print((upper_bound - lower_bound + 1) * n)


Input = list(map(int, input().split()))
(n, a, b, c, d) = (Input[0], Input[1], Input[2], Input[3], Input[4])
(Amin, Amax) = (max(1 - b + c, 1 - a - b + c + d, 1 - a + d, 1), min(n - b + c, n - a - b + c + d, n - a + d, n))
if Amin > Amax:
	print(0)
	exit()
print((Amax - Amin + 1) * n)


import sys, os, io
import math, bisect, operator
(inf, mod) = (float('inf'), 10 ** 9 + 7)
from itertools import groupby, accumulate
from heapq import heapify, heappop, heappush
from collections import deque, Counter, defaultdict
input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
Neo = lambda : list(map(int, input().split()))
(n, a, b, c, d) = Neo()
Ans = 0
for i in range(1, n + 1):
	p = i + a + b
	q = p - a - c
	r = p - b - d
	s = p - c - d
	if 1 <= q <= n and 1 <= r <= n and (1 <= s <= n):
		Ans += 1
print(n * Ans)


z = input().split()
n = int(z[0])
a = int(z[1])
b = int(z[2])
c = int(z[3])
d = int(z[4])
s_11 = 1
total = 0
while s_11 <= n:
	s_31 = s_11 + b - c
	s_13 = s_11 + a - d
	s_33 = s_11 + a + b - c - d
	if 1 <= s_31 <= n and 1 <= s_13 <= n and (1 <= s_33 <= n):
		total += 1
	s_11 += 1
x = n * total
print(x)


(n, a, b, c, d) = map(int, input().split())
m = [0, b - c, a - d, a + b - c - d]
count = 0
for j in range(1, n + 1):
	x1 = j
	x2 = m[1] + x1
	x3 = m[2] + x1
	x4 = m[3] + x1
	if any([n < k or k <= 0 for k in [x1, x2, x3, x4]]):
		pass
	else:
		count += 1
print(n * count)


read = lambda : map(int, input().split())
(n, a, b, c, d) = read()
cnt = 0
for i in range(1, n + 1):
	x = i + (b - c)
	y = i + (a + b - c - d)
	z = i + (a - d)
	p = sorted((x, y, z))
	if 1 <= p[0] and p[2] <= n:
		cnt += 1
ans = cnt * n
print(ans)


(n, a, b, c, d) = [int(x) for x in input().split()]
rezwan = 0
for x in range(1, n + 1):
	if 1 <= a + b + x - c - d <= n and 1 <= b + x - c <= n and (1 <= a + x - d <= n):
		rezwan += 1
print(rezwan * n)


def picture(n, a, b, c, d):
	result = 0
	for j in range(1, n + 1):
		y = j + b - c
		z = j + a - d
		w = a + y - d
		if 1 <= y <= n and 1 <= z <= n and (1 <= w <= n):
			result += 1
	result *= n
	return result
(N, A, B, C, D) = [int(i) for i in input().split()]
print(picture(N, A, B, C, D))


(n, a, b, c, d) = map(int, input().split())
k = 0
for u in range(1, n + 1):
	v = u + b - c
	if v >= 1 and v <= n:
		z = a + v - d
		if z >= 1 and z <= n:
			y = c + z - b
			if y >= 1 and y <= n:
				u = d + y - a
				if y >= 1 and y <= n:
					k += 1
print(k * n)


def valid(x, n):
	if x > 0 and x <= n:
		return True
	return False
inf = list(map(int, input().split()))
n = inf[0]
a = inf[1]
b = inf[2]
c = inf[3]
d = inf[4]
ans = 0
for i in range(1, n + 1):
	p = i
	q = p + a - d
	s = p + b - c
	r = p + b + a - d - c
	if valid(q, n) and valid(s, n) and valid(r, n):
		ans += 1
print(ans * n)


import sys
input = lambda : sys.stdin.readline()
MOD = 1000000007
ii = lambda : int(input())
si = lambda : input()
dgl = lambda : list(map(int, input()))
f = lambda : list(map(int, input().split()))
il = lambda : list(map(int, input().split()))
ls = lambda : list(input())
(n, a, b, c, d) = f()
mx = max(a + b, b + d, c + d, a + c)
mn = min(a + b, b + d, c + d, a + c)
print(max(0, n * n - (mx - mn) * n))


import sys
myInput = sys.stdin.readline().strip().split()
n = int(myInput[0])
a = int(myInput[1])
b = int(myInput[2])
c = int(myInput[3])
d = int(myInput[4])

def faster():
	v = 0
	w = 0
	x = 0
	y = 0
	z = 0
	numSolution = 0
	for i in range(1, n + 1):
		v = i
		w = v + a + b - a - c
		y = v + a + b - b - d
		z = v + a + b - c - d
		if w >= 1 and w <= n and (y >= 1) and (y <= n) and (z >= 1) and (z <= n):
			numSolution += 1
	return numSolution * n

def accurate():
	v = 0
	w = 0
	x = 0
	y = 0
	z = 0
	numSolution = 0
	for i in range(1, n + 1):
		for j in range(1, n + 1):
			v = i
			x = j
			w = v + x + a + b - a - x - c
			y = v + x + a + b - b - x - d
			z = v + x + a + b - x - c - d
			if w >= 1 and w <= n and (y >= 1) and (y <= n) and (z >= 1) and (z <= n):
				numSolution += 1
	return numSolution
print(str(faster()))


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')
from collections import Counter
import heapq
from collections import Counter
(n, a, b, c, d) = map(int, input().split())
ans = 0
for i in range(1, n + 1):
	y = i + a - d
	z = i + b - c
	w = z + a - d
	if y > 0 and y <= n and (z > 0) and (z <= n) and (w > 0) and (w <= n):
		ans += 1
print(n * ans)


(n, a, b, c, d) = input().split()
n = int(n)
a = int(a)
b = int(b)
c = int(c)
d = int(d)
ans = [1, b - c + 1, a - d + 1, a + b - c - d + 1]
ans.sort()
length = ans[3] - ans[0] + 1
if length > n:
	print(0)
else:
	print(n * (n - length + 1))


(n, a, b, c, d) = map(int, input().split())
r = range(1, n + 1)
print(n * sum((i + b - c in r and i + a - d in r and (i + a + b - c - d in r) for i in r)))


(n, a, b, c, d) = [eval(x) for x in input().split()]
if a < d:
	ab = c + (d - a)
else:
	ab = c
aa = ab + a + b
ac = aa - (a + c)
bd = aa - (b + d)
cd = aa - (c + d)
mini = min(ab, ac, bd, cd)
maxi = max(ab, ac, bd, cd)
diff = maxi - mini
t = n - (1 + diff) + 1
if t <= 0:
	print(0)
else:
	print(t * n)


[n, a, b, c, d] = [int(x) for x in input().split()]
a1 = a2 = a4 = a5 = 0
if a + b > c + d:
	a1 = 1
	a5 = a + b + 1 - c - d
else:
	a5 = 1
	a1 = c + d + 1 - a - b
if a + c > b + d:
	a2 = 1
	a4 = a + c + 1 - b - d
else:
	a4 = 1
	a2 = b + d + 1 - a - c
if max(a1, a2, a4, a5) > n:
	print(0)
else:
	print(n * (n - max(a1, a2, a4, a5) + 1))


(n, a, b, c, d) = map(int, input().split())
min_a11 = max(1, 1 + c - b)
max_a11 = min(n, n + c - b)
min_a11 = max(min_a11, 1 + d - a)
max_a11 = min(max_a11, n + d - a)
min_a11 = max(min_a11, 1 + c + d - a - b)
max_a11 = min(max_a11, n + c + d - a - b)
print(n * max(max_a11 - min_a11 + 1, 0))


pt = [[0 for x in range(3)] for y in range(3)]
inp = input().split()
n = int(inp[0])
a = int(inp[1])
b = int(inp[2])
c = int(inp[3])
d = int(inp[4])
d1 = c - b
d2 = d - a
count = 0
for i in range(1, n + 1):
	ltup = i
	ltdn = ltup - d2
	rtup = ltup - d1
	rtdn = rtup - d2
	if ltdn > 0 and rtup > 0 and (rtdn > 0) and (ltdn <= n) and (rtup <= n) and (rtdn <= n):
		count = count + 1
num = count * n
print(num)


__author__ = 'zihaozhu'
from sys import stdin
(n, a, b, c, d) = map(int, stdin.readline().split())
answer = 0
for x in range(1, n + 1):
	y = x + b - c
	z = x + a - d
	w = z + b - c
	if 1 <= y and y <= n and (1 <= z) and (z <= n) and (1 <= w) and (w <= n):
		answer += 1
answer *= n
print(answer)


l = list(map(int, input().split()))
n = l[0]
a = l[1]
b = l[2]
c = l[3]
d = l[4]
k = 0
for i in range(n):
	x = i + 1
	c2 = x + b - c
	c3 = x + a - d
	c4 = x + a + b - c - d
	if c2 > 0 and c3 > 0 and (c4 > 0):
		if c2 < n + 1 and c3 < n + 1 and (c4 < n + 1):
			k += 1
print(n * k)


def main():
	(n, a, b, c, d) = map(int, input().split())
	l = sorted((a + b, a + c, b + d, c + d))
	print(n * max(n + l[0] - l[3], 0))
main()


(n, a, b, c, d) = map(int, input().split())
answer = 0
for x in range(1, n + 1):
	flag = 0
	if x + b - c <= 0 or x + b - c > n:
		flag = 1
	if x + a - d <= 0 or x + a - d > n:
		flag = 1
	if x + a + b - c - d <= 0 or x + a + b - c - d > n:
		flag = 1
	if flag == 0:
		answer += 1
print(answer * n)


(n, a, b, c, d) = [int(x) for x in input().split()]
l = [a + b, a + c, b + d, c + d]
print(n * max(0, n - max(l) + min(l)))


(n, a, b, c, d) = map(int, input().split())
arr = [a + b, a + c, b + d, c + d]
count = n * (n - max(arr) + min(arr))
print(max(0, count))


(n, a, b, c, d) = list(map(int, input().split()))
ans = 0
for i in range(1, n + 1):
	s = i + a + b
	x2 = s - a - c
	x3 = s - c - d
	x4 = s - b - d
	if max(x2, x3, x4) <= n and min(x2, x3, x4) >= 1:
		ans += n
print(ans)


import math
(n, a, b, c, d) = map(int, input().split())
(x1, y1, z1) = (b - c, a + b - c - d, a - d)
(x2, y2, z2) = (x1 + n, y1 + n, z1 + n)
z = min(x2, y2, z2, n) - max(x1, y1, z1, 0)
print(n * max(0, z))


s = input().split()
n = int(s[0])
a = int(s[1])
b = int(s[2])
c = int(s[3])
d = int(s[4])
posi = 0
for i in range(n):
	x = i + 1
	y = x - c + b
	if (y < 1) + (y > n):
		continue
	w = y - d + a
	if (w < 1) + (w > n):
		continue
	z = w - b + c
	if (z < 1) + (z > n):
		continue
	if x == z - a + d:
		posi += 1
print(posi * n)


(n, a, b, c, d) = map(int, input().split())
lo = min(a + b, a + c, c + d, b + d)
hi = max(a + b, a + c, c + d, b + d)
ans = 0
for x in range(1, n + 1):
	ans += max(0, n - (hi - lo))
print(ans)


import sys
(n, a, b, c, d) = map(int, sys.stdin.readline().split())
c1 = 0
for x11 in range(n):
	x13 = x11 + 1 - c + b
	x31 = x11 + 1 - d + a
	x33 = x11 + 1 - c - d + a + b
	if x13 >= 1 and x13 <= n and (x31 >= 1) and (x31 <= n) and (x33 >= 1) and (x33 <= n):
		c1 = c1 + 1
print(c1 * n)


line = input().split()
N = int(line[0])
A = int(line[1])
B = int(line[2])
C = int(line[3])
D = int(line[4])
AB = A + B
AC = A + C
BD = B + D
CD = C + D
s = 0
for i in range(1, N + 1):
	m = AB + i
	m = min(m, AC + i)
	m = min(m, BD + i)
	m = min(m, CD + i)
	M = AB + i
	M = max(M, AC + i)
	M = max(M, BD + i)
	M = max(M, CD + i)
	s += max(0, m + N - M)
print(s)


(n, a, b, c, d) = input().split()
(n, a, b, c, d) = (int(n), int(a), int(b), int(c), int(d))
condl = max(0, d - a, b + d - a - c, b - c)
condh = min(n, n + d - a, n + b + d - a - c, n + b - c)
print(max(0, (condh - condl) * n))


a = list(map(int, input().split()))
N = a[0]
b = a[2]
c = a[3]
d = a[4]
a = a[1]
M = max(a + b, a + c, b + d, c + d)
m = min(a + b, a + c, b + d, c + d)
x = (m + N - M) * N
if x <= 0:
	print(0)
else:
	print(x)


(n, a, b, c, d) = map(int, input().split())

def getResult(n, a, b, c, d):
	count = 0
	for i in range(n):
		if i + 1 + b - c <= n and i + 1 + b - c > 0:
			if i + 1 + a + b - d - c <= n and i + 1 + a + b - d - c > 0:
				if i + 1 + a - d <= n and i + 1 + a - d > 0:
					count += 1
	print(count * n)
getResult(n, a, b, c, d)


(n, a, b, c, d) = map(int, input().split())
ans = 0
for i in range(1, n + 1):
	p1 = i
	sm = a + b + i
	p2 = sm - a - c
	p3 = sm - d - c
	p4 = sm - b - d
	if p1 >= 1 and p1 <= n and (p2 >= 1) and (p2 <= n) and (p3 >= 1) and (p3 <= n) and (p4 >= 1) and (p4 <= n):
		ans = ans + 1
print(ans * n)


(n, a, b, c, d) = map(int, input().split())
k = min(n, n - c + b, n - d + a, n - c + b - d + a)
l = max(0, b - c, a - d, a - d + b - c)
print(max(0, (k - l) * n))


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


(n, a, b, c, d) = list(map(int, input().split()))
r = 0
for e in range(1, n + 1):
	h = e + (a - d)
	i = h + (b - c)
	f = i + (d - a)
	if e == f + (c - b) and h <= n and (i <= n) and (f <= n) and (h > 0) and (i > 0) and (f > 0):
		r += 1
print(r * n)


(n, a, b, c, d) = list(map(int, input().split()))
lst = [a + b, a + c, c + d, b + d]
if n - (max(lst) - min(lst)) <= 0:
	print(0)
else:
	print((n - (max(lst) - min(lst))) * n)


(n, a, b, c, d) = map(int, input().split())
num = 0
i = 0
while n > i:
	i += 1
	if i + b - c < 1 or i + b - c > n:
		continue
	if i + a - d < 1 or i + a - d > n:
		continue
	if i + a - d + b - c < 1 or i + a - d + b - c > n:
		continue
	num += 1
print(num * n)


(n, a, b, c, d) = map(int, input().split())
res = 0
for i in range(1, n + 1):
	r = 4 * n
	l = 1
	r = min(r, n + (i + a + b))
	r = min(r, n + (i + a + c))
	r = min(r, n + (i + c + d))
	r = min(r, n + (i + d + b))
	l = max(l, a + b + i + 1)
	l = max(l, a + c + i + 1)
	l = max(l, c + d + i + 1)
	l = max(l, b + d + i + 1)
	if l <= r:
		res += r - l + 1
print(res)


(n, a, b, c, d) = map(int, input().split())
x = max([1 - a + d, 1 - b + c, 1, 1 - a - b + d + c])
y = min([n - a + d, n - b + c, n, n - a - b + d + c])
if x > y:
	print(0)
	exit(0)
else:
	print(n * (y - x + 1))


class CodeforcesTask675BSolution:

	def __init__(self):
		self.result = ''
		self.n_a_b_c_d = []

	def read_input(self):
		self.n_a_b_c_d = [int(x) for x in input().split(' ')]

	def process_task(self):
		result = 0
		for x in range(1, self.n_a_b_c_d[0] + 1):
			y = self.n_a_b_c_d[2] - self.n_a_b_c_d[3] + x
			w = self.n_a_b_c_d[1] - self.n_a_b_c_d[4] + x
			q = self.n_a_b_c_d[2] - self.n_a_b_c_d[3] + w
			if 1 <= y <= self.n_a_b_c_d[0] and 1 <= w <= self.n_a_b_c_d[0] and (1 <= q <= self.n_a_b_c_d[0]):
				result += 1
		self.result = str(result * self.n_a_b_c_d[0])

	def get_result(self):
		return self.result
Solution = CodeforcesTask675BSolution()
Solution.read_input()
Solution.process_task()
print(Solution.get_result())


def solve():
	(n, a, b, c, d) = map(int, input().split())
	ans = 0
	for i in range(1, n + 1):
		t = i + a + b
		if t - a - c > 0 and t - a - c <= n and (t - c - d > 0) and (t - c - d <= n) and (t - b - d > 0) and (t - b - d <= n):
			ans += 1
	return ans * n
print(solve())


def main(n, a, b, c, d):
	s = [a + b, b + d, d + c, c + a]
	s.sort()
	(a, b, c, d) = s
	ans = n * max(0, n + a - d)
	return ans
(n, a, b, c, d) = list(map(int, input().split()))
print(main(n, a, b, c, d))


(n, a, b, c, d) = (int(s) for s in input().strip().split())

def isok(i):
	j = i + b - c
	l = i + a - d
	m = l + b - c
	return all((v > 0 and v <= n for v in (i, j, l, m)))
print(n * sum((1 for i in range(1, n + 1) if isok(i))))


(n, a, b, c, d) = map(int, input().split())
m1 = a + b
m2 = a + c
m3 = c + d
m4 = b + d
mn = min(m1, m2, m3, m4)
mx = max(m1, m2, m4, m3)
print(max(0, n * (n - (mx - mn))))


def is_ok(n, a, b, c, d, e):
	return a + e - d in range(1, n + 1) and b + e - c in range(1, n + 1) and (a + b + e - c - d in range(1, n + 1))
(n, a, b, c, d) = input().split()
n = int(n)
a = int(a)
b = int(b)
c = int(c)
d = int(d)
num = 0
for i in range(1, n + 1):
	if is_ok(n, a, b, c, d, i) == True:
		num = num + 1
print(num * n)


(n, a, b, c, d) = list(map(int, input().split()))
t1 = b - c
t2 = a - d
print(n * max(1 + min(n, n + t1, n + t2, n + t1 + t2) - max(1, 1 + t1, 1 + t2, 1 + t1 + t2), 0))


(n, a, b, c, d) = map(int, input().split())
q = {}
for x1 in range(1, n + 1):
	x4 = a + x1 - d
	if x4 < 1 or x4 > n:
		continue
	x2 = x1 + b - c
	if x2 < 1 or x2 > n:
		continue
	x5 = x2 + a - d
	if x5 < 1 or x5 > n:
		continue
	q[x1, x2, x4, x5] = 1
print(len(q) * n)


(n, a, b, c, d) = map(int, input().split())
ans = 0
for e in range(1, n + 1):
	min1 = a + b + e + 1
	max1 = a + b + e + n
	min2 = b + e + d + 1
	max2 = b + d + e + n
	min3 = d + c + e + 1
	max3 = d + c + e + n
	min4 = a + c + e + 1
	max4 = a + c + e + n
	l = max(min1, min2, min3, min4)
	r = min(max1, max2, max3, max4)
	if l <= r:
		ans += r - l + 1
print(ans)


l = input()
l = l.split(' ')
for i in range(len(l)):
	l[i] = int(l[i])
n = l[0]
a = l[1]
b = l[2]
c = l[3]
d = l[4]
ctr = 0
for e in range(1, n + 1):
	f = e + b - c
	h = a + e - d
	i = a + f - d
	cond = c + d + i == a + b + e
	cond = cond and (f >= 1 and f <= n)
	cond = cond and (h >= 1 and h <= n)
	cond = cond and (i >= 1 and i <= n)
	if cond:
		ctr += n
print(ctr)


l = input().split(' ')
n = int(l[0])
a = int(l[1])
b = int(l[2])
c = int(l[3])
d = int(l[4])
x = 1
y = n
m = min(x + b - c, x + a - d, a + b - c - d + x)
g = max(y + b - c, y + a - d, a + b - c - d + y)
if m < 1:
	x += 1 - m
if g > n:
	y += n - g
if x > y:
	print(0)
else:
	print(n * (y - x + 1))


(n, a, b, c, d) = list(map(int, input().split()))
l = max(1 - b + c, 1 - a + d, 1 - a - b + c + d)
h = min(n - b + c, n - a + d, n - a - b + c + d)
if l < 0:
	l = 1
if h > n:
	h = n
if h < l:
	print(0)
else:
	print(n * (h - l + 1))


def main(n, a, b, c, d):
	w = a + b
	x = a + c
	y = b + d
	z = c + d
	g = 0
	count = 0
	for e in range(1, n + 1):
		cur = w + e
		f = cur - x
		h = cur - y
		i = cur - z
		if f < 1 or h < 1 or i < 1 or (f > n) or (h > n) or (i > n):
			continue
		count += 1
	return count * n
print(main(*list(map(int, input().split(' ')))))


def solve(n, a, b, c, d):
	ans = 0
	sums = [a + b, b + d, d + c, c + a]
	minsum = min(sums)
	maxsum = max(sums)
	tmp = minsum + n
	if tmp <= maxsum:
		return ans
	else:
		ans += n * (tmp - maxsum)
	return ans
(n, a, b, c, d) = [int(x) for x in input().split()]
print(solve(n, a, b, c, d))


import math
(n, a, b, c, d) = input().split()
n = int(n)
a = int(a)
b = int(b)
c = int(c)
d = int(d)
sum = 0
g = 0
for x in range(1, n + 1):
	y = x + b - c
	z = x + a - d
	w = z + b - c
	if 1 <= y <= n:
		if 1 <= z <= n:
			if 1 <= w <= n:
				sum += 1
g = n * sum
print(g)


def paintings_possibility():
	(n, a, b, c, d) = (int(x) for x in input().split())
	minX1 = max(1, 1 - b + c, 1 - a + d, 1 - a - b + c + d)
	maxX1 = min(n, n - b + c, n - a + d, n - a - b + c + d)
	if maxX1 >= minX1:
		return n * (maxX1 - minX1 + 1)
	return 0
print(paintings_possibility())


import os
import sys
from io import BytesIO, IOBase
mod = 10 ** 9 + 7

def main():
	(n, a, b, c, d) = map(int, input().split())
	ans = 0
	mn = max([a + b, a + c, b + d, c + d]) + 2
	mx = min([a + b, a + c, b + d, c + d]) + n + 1
	if mx >= mn:
		print((mx - mn + 1) * n)
	else:
		print(0)

def nouse0():
	a = 420
	b = 420
	print(f'i am nitish{(a + b) // 2}')

def nouse1():
	a = 420
	b = 420
	print(f'i am nitish{(a + b) // 2}')

def nouse2():
	a = 420
	b = 420
	print(f'i am nitish{(a + b) // 2}')
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 nouse3():
	a = 420
	b = 420
	print(f'i am nitish{(a + b) // 2}')

def nouse4():
	a = 420
	b = 420
	print(f'i am nitish{(a + b) // 2}')

def nouse5():
	a = 420
	b = 420
	print(f'i am nitish{(a + b) // 2}')
main()


(n, a, b, c, d) = map(int, input().split())
sol = 0
for i in range(1, n + 1):
	sol += max(min(a + b, a + c, b + d, c + d) + i + n - (max(a + b, a + c, b + d, c + d) + i), 0)
print(sol)


def __starting_point():
	(n, a, b, c, d) = list(map(int, input().split()))
	x = max([1 - a + d, 1 - b + c, 1, 1 - a - b + d + c])
	y = min([n - a + d, n - b + c, n, n - a - b + d + c])
	if x > y:
		print(0)
		return
	else:
		print(n * (y - x + 1))
__starting_point()


(n, a, b, c, d) = [int(s) for s in input().split()]

def solve(p):
	s = []
	s.append(p[0][0] + p[0][1] + p[1][0] + p[1][1])
	s.append(p[0][1] + p[0][2] + p[1][1] + p[1][2])
	s.append(p[1][0] + p[1][1] + p[2][0] + p[2][1])
	s.append(p[1][1] + p[1][2] + p[2][1] + p[2][2])
	return max(0, n - (max(s) - min(s)))
p = [[0, a, 0], [b, 0, c], [0, d, 0]]
res = 0
for m in range(1, n + 1):
	p[1][1] = m
	res += solve(p)
print(res)


def less(x, y):
	if x - y <= 0:
		return x
	else:
		return y

def great(x, y):
	if x - y <= 0:
		return y
	else:
		return x
(n, a, b, c, d) = [int(x) for x in input().split()]
out = 0
if a == d and c == b:
	out = n ** 2
elif a == d:
	out = n * (n - great(c, b) + 1)
elif b == c:
	out = n * (n - great(a, d) + 1)
elif a > 0 and b > 0 and (c > 0) and (d > 0):
	out = n * (n - (great(great(a + b, a + c), great(b + d, d + c)) - less(less(a + b, a + c), less(b + d, d + c))))
if out <= 0:
	print(0)
else:
	print(out)


import sys
import math
from collections import defaultdict
import itertools
MAXNUM = math.inf
MINNUM = -1 * math.inf

def getInt():
	return int(sys.stdin.readline().rstrip())

def getInts():
	return map(int, sys.stdin.readline().rstrip().split(' '))

def getString():
	return sys.stdin.readline().rstrip()

def printOutput(ans):
	sys.stdout.write(str(ans) + '\n')

def solve(n, a, b, c, d):
	maxCorner = max(a + b, a + c, c + d, b + d)
	minCorner = min(a + b, a + c, c + d, b + d)
	diff = maxCorner - minCorner
	if n * (n - diff) >= 0:
		return n * (n - diff)
	return 0

def readinput():
	(n, a, b, c, d) = getInts()
	printOutput(solve(n, a, b, c, d))
readinput()


(n, a, b, c, d) = map(int, input().split())
ans = 0
for centro in range(1, n + 1):
	q1 = a + b + centro
	q2 = a + c + centro
	q3 = b + d + centro
	q4 = c + d + centro
	v = [q1, q2, q3, q4]
	v.sort()
	q_min = v[0]
	q_max = v[-1]
	if q_min + n < q_max + 1:
		continue
	elif q_min + n == q_max + 1:
		ans += 1
	else:
		ans += q_min + n - (q_max + 1) + 1
print(ans)


import math as m
(n, a, b, c, d) = map(int, input().split())
ans = 0
for i in range(n):
	x = i + 1
	t = x + b - c
	curr = 1
	if t >= 1 and t <= n:
		curr = 1
	else:
		curr = 0
	t = x + a - d
	if t >= 1 and t <= n and (curr != 0):
		curr = 1
	else:
		curr *= 0
	t = x + a + b - d - c
	if t >= 1 and t <= n and (curr != 0):
		curr = 1
	else:
		curr *= 0
	ans += curr * n
print(ans)


(n, a, b, c, d) = map(int, input().split())
ans = n * (n - abs(a - d) - abs(b - c))
print(max(0, ans))


def main():
	(n, a, b, c, d) = map(int, input().split())
	sums = [a + b, a + c, b + d, c + d]
	diff = max(sums) - min(sums)
	ans = n * (n - diff)
	if ans < 0:
		ans = 0
	print(ans)
main()


line = input().split()
n = int(line[0])
a = int(line[1])
b = int(line[2])
c = int(line[3])
d = int(line[4])
res = 0
for i in range(1, n + 1):
	SUM = a + b + i
	if SUM - a - c > 0 and SUM - a - c <= n and (SUM - d - c > 0) and (SUM - d - c <= n) and (SUM - b - d > 0) and (SUM - b - d <= n):
		res = res + n
print(res)


import sys
arr = list(map(int, input().strip().split()))
n = arr[0]
top = arr[1]
left = arr[2]
right = arr[3]
bottom = arr[4]
top_left = top + left
top_right = top + right
bottom_left = bottom + left
bottom_right = bottom + right
max_greater = 0
max_lesser = 0
nums = [top_right, bottom_left, bottom_right]
for e in nums:
	diff = e - top_left
	max_greater = max(max_greater, diff)
	max_lesser = min(max_lesser, diff)
max_diff = max_greater - max_lesser
if max_diff > n - 1:
	ans = 0
else:
	ans = (n - max_diff) * n
print(ans)


def main():
	(n, a, b, c, d) = tuple([int(i) for i in input().split()])
	ans = 0
	for i in range(1, n + 1):
		t1 = i
		t2 = t1 + a + b - a - c
		t3 = t1 + a + b - b - d
		t4 = t1 + a + b - c - d
		if t2 >= 1 and t2 <= n and (t3 >= 1) and (t3 <= n) and (t4 >= 1) and (t4 <= n):
			ans += n
	print(ans)
main()


(n, a, b, c, d) = map(int, input().split())
ans = 0
for x in range(1, n + 1):
	g = [a + b + x, a + c + x, b + d + x, c + d + x]
	mi = min(g)
	mx = max(g)
	dif = mx - mi
	ans += max(n - dif, 0)
print(ans)


(n, a, b, c, d) = (int(_) for _ in input().split())
ct = 0
for w in range(1, n + 1):
	x = w + b - c
	y = w + a - d
	if 1 <= x <= n and 1 <= y <= n:
		z = y + b - c
		if 1 <= z <= n and z == x + a - d:
			ct += 1
print(ct * n)


(n, a, b, c, d) = map(int, input().split())
p = [a + b, a + c, c + d, b + d]
t = min(p) - max(p) + n
print(t * n if t > 0 else 0)


(n, a, b, c, d) = map(int, input().split(' '))
sums = [a + b, a + c, b + d, c + d]
sums.sort()
ans = max(0, n - sums[3] + sums[0])
ans *= n
print(ans)


(n, a, b, c, d) = map(int, input().split())
M = 0
for E in range(1, n + 1):
	F = E + b - c
	H = E + a - d
	K = E + a + b - c - d
	if 0 < F <= n and 0 < H <= n and (0 < K <= n):
		M += n
print(M)


from sys import stdin as Si, maxsize as m
from math import floor as F
from collections import defaultdict as dt
from operator import itemgetter as ig
from math import pi
(n, a, b, c, d) = map(int, Si.readline().split())
count = 0
for c1 in range(1, n + 1):
	c4 = a + c1 - d
	if 1 <= c4 <= n:
		c2 = c1 + b - c
	else:
		continue
	if 1 <= c2 <= n:
		c5 = c2 + a - d
	else:
		continue
	if 1 <= c5 <= n:
		count += 1
	else:
		continue
print(count * n)


(n, a, b, c, d) = map(int, input().split())
mis = min(a + b, b + d, c + d, c + a)
mas = max(a + b, b + d, c + d, c + a)
dif = mas - mis
res = 0
for x3 in range(1, n + 1):
	res += n - dif
print(max(res, 0))


(n, a, b, c, d) = map(int, input().split())
x = 1
y = x + b - c
w = x + a - d
z = y + a - d
mi = 1 - min(x, y, w, z)
x += mi
y += mi
w += mi
z += mi
print((n - max(x, y, w, z) + 1) * n * (n >= max(x, y, w, z)))


(n, a, b, c, d) = map(int, input().split())
(d1, d2, d3, d4) = (a + b, a + c, c + d, b + d)
(m1, m2) = (max(d1, d2, d3, d4), min(d1, d2, d3, d4))
if m1 - m2 >= n:
	s = 0
else:
	s = (n - (m1 - m2)) * n
print(s)


(n, a, b, c, d) = map(int, input().split())
mn = min(a + b, b + d, c + d, c + a)
mx = max(a + b, b + d, c + d, c + a)
res = (n - mx + mn) * n
print(max(res, 0))


import sys
sys.stderr = sys.stdout

def painting(n, a, b, c, d):
	lo = min(a + b, c + d, a + c, b + d)
	hi = max(a + b, c + d, a + c, b + d)
	delta = hi - lo
	return max(n - delta, 0) * n

def main():
	(n, a, b, c, d) = readinti()
	print(painting(n, a, b, c, d))

def readint():
	return int(input())

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

def readintt():
	return tuple(readinti())

def readintl():
	return list(readinti())

def readinttl(k):
	return [readintt() for _ in range(k)]

def readintll(k):
	return [readintl() for _ in range(k)]

def log(*args, **kwargs):
	print(*args, **kwargs, file=sys.__stderr__)
main()


(n, a, b, c, d) = map(int, input().split())
arr = sorted([a + b, b + d, c + d, a + c])
print(n * max(n - (arr[-1] - arr[0]), 0))


nabcd = list(map(int, input().split()))
RANGE = range(1, nabcd[0])
a = nabcd[1]
b = nabcd[2]
c = nabcd[3]
d = nabcd[4]
dif1 = a - d
dif2 = b - c
dif = abs(dif1) + abs(dif2)
counter = nabcd[0] * (nabcd[0] - dif)
print(max(counter, 0))