import itertools

n, k = map(int, input().split())
a = list(map(int, input().split()))
best = 0

if n == 1:
    print(a[0] + 1)
else:
    mx1 = -1
    mx2 = -1
    mxi1 = 0
    mxi2 = 0
    for i in range(n):
        if a[i] >= mx1:
            mx2 = mx1
            mxi2 = mxi1
            mx1 = a[i]
            mxi1 = i
        elif a[i] >= mx2:
            mx2 = a[i]
            mxi2 = i
    mxi1, mxi2 = min(mxi1, mxi2), max(mxi1, mxi2)
    ans = mx1 + mx2 + mxi2 + 1
    print(ans)
"""
for el in itertools.product(range(k), repeat=n):
    #print(el)
    ans = 0
    b = [[] for _ in range(k)]
    for i in range(n):
        b[el[i]].append(a[i])
    if any([len(b[bi]) == 0 for bi in range(k)]):
        continue
    for bi in range(k):
        mx = max(b[bi])
        ind = 0
        for i, bai in enumerate(b[bi]):
            if bai == mx:
                ind = i
        ans += mx + ind + 1
    if ans > best:
        best = ans
        print(el)
    #print(el, ans)
print(best)
"""