import sys

from collections import Counter



input = sys.stdin.readline





def main():

    K, T = list(map(int, input().split()))

    a = list(map(int, input().split()))



    ans = 0

    prev_j = -1

    for i in range(K):

        j_max = 0

        n_max = 0

        for j in range(T):

            if j == prev_j:

                continue

            if a[j] > n_max:

                n_max = a[j]

                j_max = j

        if n_max == 0:

            ans += 1

        else:

            prev_j = j_max

            a[j_max] -= 1

    print(ans)





if __name__ == "__main__":

    main()

import sys



input = sys.stdin.readline





def main():

    K, T = list(map(int, input().split()))

    A = list(map(int, input().split()))



    prev_idx = -1

    ans = 0

    for _ in range(K):

        cur_idx = -1

        max_num = 0

        for i, a in enumerate(A):

            if i == prev_idx:

                continue

            if a > max_num:

                max_num = a

                cur_idx = i

        if max_num == 0:

            ans = A[prev_idx]

            break

        A[cur_idx] -= 1

        prev_idx = cur_idx



    print(ans)





if __name__ == "__main__":

    main()

import sys
from collections import Counter

input = sys.stdin.readline


def main():
    K, T = list(map(int, input().split()))
    a = list(map(int, input().split()))
    ans = 0
    prev_j = -1
    for i in range(K):
        j_max = 0
        n_max = 0
        for j in range(T):
            if j == prev_j:
                continue
            if a[j] > n_max:
                n_max = a[j]
                j_max = j
        if n_max == 0:
            ans += 1
        else:
            prev_j = j_max
            a[j_max] -= 1
    print(ans)


if __name__ == "__main__":
    main()

import sys

input = sys.stdin.readline


def main():
    K, T = list(map(int, input().split()))
    A = list(map(int, input().split()))
    prev_idx = -1
    ans = 0
    for _ in range(K):
        cur_idx = -1
        max_num = 0
        for i, a in enumerate(A):
            if i == prev_idx:
                continue
            if a > max_num:
                max_num = a
                cur_idx = i
        if max_num == 0:
            ans = A[prev_idx]
            break
        A[cur_idx] -= 1
        prev_idx = cur_idx
    print(ans)


if __name__ == "__main__":
    main()

-from collections import Counter -    a = list(map(int, input().split())) +    A = list(map(int, input().split())) +    prev_idx = -1 -    prev_j = -1 -    for i in range(K): -        j_max = 0 -        n_max = 0 -        for j in range(T): -            if j == prev_j: +    for _ in range(K): +        cur_idx = -1 +        max_num = 0 +        for i, a in enumerate(A): +            if i == prev_idx: -            if a[j] > n_max: -                n_max = a[j] -                j_max = j -        if n_max == 0: -            ans += 1 -        else: -            prev_j = j_max -            a[j_max] -= 1 +            if a > max_num: +                max_num = a +                cur_idx = i +        if max_num == 0: +            ans = A[prev_idx] +            break +        A[cur_idx] -= 1 +        prev_idx = cur_idx