from sys import stdin

#import heapq

K,T = [int(x) for x in stdin.readline().rstrip().split()]

data = [(int(b),a) for a,b in enumerate(stdin.readline().rstrip().split())]

#heapq.heapify(data)

ans = 0

cake_num = -1

flag = True

 

if T == 1:

    ans = K-1

else:

    while flag:

        data.sort()

        #print(data)

        c_c,c_n = data.pop()

        #print(c_c,c_n)

        if c_n == cake_num:

            c_c2,c_n2 = data.pop()

            if c_c2 == 0:

                ans = c_c

                flag = False

            else:

                data.append((c_c2-1,c_n2))

            data.append((c_c,c_n))

            cake_num = c_n2

            #flag = False

        else:

            data.append((c_c-1,c_n))

            cake_num = c_n

        if c_c == 0:

            ans = 0

            flag = False

        

print(ans)
from sys import stdin

import heapq

K,T = [int(x) for x in stdin.readline().rstrip().split()]

data = [(-int(b),a) for a,b in enumerate(stdin.readline().rstrip().split())]

heapq.heapify(data)

ans = 0

cake_num = -1

cake_count = 0

flag = True

 

if T == 1:

  ans = K-1

else:

  while flag:

    c_c,c_n = heapq.heappop(data)

    if c_n == cake_num:

        c_c2,c_n2 = heapq.heappop(data)

        if c_c2 == 0:

            ans = abs(c_c)

            flag = False

        else:

            heapq.heappush(data,(c_c2+1,c_n2))

        cake_num = c_n2

        heapq.heappush(data,(c_c,c_n))

        #flag = False

    else:

        heapq.heappush(data,(c_c+1,c_n))

        cake_num = c_n

    if c_c == 0:

      ans = 0

      flag = False

    

print(ans)
from sys import stdin

# import heapq
K, T = [int(x) for x in stdin.readline().rstrip().split()]
data = [(int(b), a) for a, b in enumerate(stdin.readline().rstrip().split())]
# heapq.heapify(data)
ans = 0
cake_num = -1
flag = True
if T == 1:
    ans = K - 1
else:
    while flag:
        data.sort()
        # print(data)
        c_c, c_n = data.pop()
        # print(c_c,c_n)
        if c_n == cake_num:
            c_c2, c_n2 = data.pop()
            if c_c2 == 0:
                ans = c_c
                flag = False
            else:
                data.append((c_c2 - 1, c_n2))
            data.append((c_c, c_n))
            cake_num = c_n2
            # flag = False
        else:
            data.append((c_c - 1, c_n))
            cake_num = c_n
        if c_c == 0:
            ans = 0
            flag = False
print(ans)

from sys import stdin
import heapq

K, T = [int(x) for x in stdin.readline().rstrip().split()]
data = [(-int(b), a) for a, b in enumerate(stdin.readline().rstrip().split())]
heapq.heapify(data)
ans = 0
cake_num = -1
cake_count = 0
flag = True
if T == 1:
    ans = K - 1
else:
    while flag:
        c_c, c_n = heapq.heappop(data)
        if c_n == cake_num:
            c_c2, c_n2 = heapq.heappop(data)
            if c_c2 == 0:
                ans = abs(c_c)
                flag = False
            else:
                heapq.heappush(data, (c_c2 + 1, c_n2))
            cake_num = c_n2
            heapq.heappush(data, (c_c, c_n))
            # flag = False
        else:
            heapq.heappush(data, (c_c + 1, c_n))
            cake_num = c_n
        if c_c == 0:
            ans = 0
            flag = False
print(ans)

+import heapq -# import heapq -data = [(int(b), a) for a, b in enumerate(stdin.readline().rstrip().split())] -# heapq.heapify(data) +data = [(-int(b), a) for a, b in enumerate(stdin.readline().rstrip().split())] +heapq.heapify(data) +cake_count = 0 -        data.sort() -        # print(data) -        c_c, c_n = data.pop() -        # print(c_c,c_n) +        c_c, c_n = heapq.heappop(data) -            c_c2, c_n2 = data.pop() +            c_c2, c_n2 = heapq.heappop(data) -                ans = c_c +                ans = abs(c_c) -                data.append((c_c2 - 1, c_n2)) -            data.append((c_c, c_n)) +                heapq.heappush(data, (c_c2 + 1, c_n2)) +            heapq.heappush(data, (c_c, c_n)) -            data.append((c_c - 1, c_n)) +            heapq.heappush(data, (c_c + 1, c_n))