-- AtCoder My Practice
-- author: Leonardone @ NEETSDKASU
{-# LANGUAGE OverloadedStrings #-}
import qualified Data.Set              as S
import qualified Data.Text             as T
import qualified Data.Array            as A
import qualified Text.Printf           as P
import qualified Data.Map              as M
import qualified Data.IntMap           as I
import qualified Data.ByteString.Char8 as C
import qualified Data.List             as L
import Data.Maybe (fromJust)
import Data.Ix    (inRange)

mapread = map $ fst . fromJust . C.readInt



main = print . solve . map read . words =<< getContents

solve (k:_:xs) = ans where
    x = head . reverse $ L.sort xs
    y = k - x
    ans = if y >= x - 1 then 0 else x - y - 1

    