local k, n = io.read("*n", "*n")
local t = {}
for i = 1, n do
  local tmp = {}
  tmp.cnt = io.read("*n")
  tmp.idx = i
  t[i] = tmp
end
table.sort(t, function(a, b) return a.cnt < b.cnt end)
local vanish = 0
local lastate = 0
while vanish < n - 1 do
  local tgt = n
  if lastate == t[n].idx then
    tgt = n - 1
  end
  t[tgt].cnt = t[tgt].cnt - 1
  if t[tgt].cnt == 0 then vanish = vanish + 1 end
  table.sort(t, function(a, b) return a.cnt < b.cnt end)
  lastate = t[tgt].idx
end
print(t[n].cnt - 1)
