def solve(N):

    if N < 10:

        return N

    num = 9

    while num < N:

        num *= 10

        num += 9

    num -= 9

    num //= 10



    k = int(str(N)[0] + str(num))

    if k <= N:

        return sum(int(c) for c in str(k))

    else:

        k = int(str(N)[0] + str(num)) - num - 1

        return sum(int(c) for c in str(k))





if __name__ == "__main__":

    N = int(eval(input()))

    print((solve(N)))

def solve(N):

    if N < 10:

        return N

    num = int("9" * (len(str(N)) - 1))

    k = int(str(N)[0] + str(num))

    if k <= N:

        return sum(int(c) for c in str(k))

    else:

        k = k - num - 1

        return sum(int(c) for c in str(k))





assert (solve(799) == 25)

assert (solve(999) == 27)

assert (solve(200) == 19)

assert (solve(100) == 18)

assert (solve(99) == 18)

assert (solve(89) == 17)

assert (solve(5) == 5)



if __name__ == "__main__":

    N = int(eval(input()))

    print((solve(N)))

def solve(N):
    if N < 10:
        return N
    num = 9
    while num < N:
        num *= 10
        num += 9
    num -= 9
    num //= 10
    k = int(str(N)[0] + str(num))
    if k <= N:
        return sum(int(c) for c in str(k))
    else:
        k = int(str(N)[0] + str(num)) - num - 1
        return sum(int(c) for c in str(k))


if __name__ == "__main__":
    N = int(eval(input()))
    print((solve(N)))

def solve(N):
    if N < 10:
        return N
    num = int("9" * (len(str(N)) - 1))
    k = int(str(N)[0] + str(num))
    if k <= N:
        return sum(int(c) for c in str(k))
    else:
        k = k - num - 1
        return sum(int(c) for c in str(k))


assert solve(799) == 25
assert solve(999) == 27
assert solve(200) == 19
assert solve(100) == 18
assert solve(99) == 18
assert solve(89) == 17
assert solve(5) == 5
if __name__ == "__main__":
    N = int(eval(input()))
    print((solve(N)))

-    num = 9 -    while num < N: -        num *= 10 -        num += 9 -    num -= 9 -    num //= 10 +    num = int("9" * (len(str(N)) - 1)) -        k = int(str(N)[0] + str(num)) - num - 1 +        k = k - num - 1 +assert solve(799) == 25 +assert solve(999) == 27 +assert solve(200) == 19 +assert solve(100) == 18 +assert solve(99) == 18 +assert solve(89) == 17 +assert solve(5) == 5