import sys

sys.stdin = open('casting.in')
sys.stdout = open('casting.out', 'w')

type = input()
N, a, b, c = map(int, input().split())

if int(type) == 2:
    print(min(a, b, c))
else:
    print((a + b + c - 2 * N) if a + b + c - 2 * N > 0 else 0)
