Problem:
 Score : 300 points    Problem Statement Find the maximum possible sum of the digits (in base 10 ) of a positive integer not greater than N .      Constraints  1\leq N \leq 10^{16}  N is an integer.         Input Input is given from Standard Input in the following format:  N       Output Print the maximum possible sum of the digits (in base 10 ) of a positive integer not greater than N .        Sample Input 1 100      Sample Output 1 18  For example, the sum of the digits in 99 is 18 , which turns out to be the maximum value.       Sample Input 2 9995      Sample Output 2 35  For example, the sum of the digits in 9989 is 35 , which turns out to be the maximum value.       Sample Input 3 3141592653589793      Sample Output 3 137

Solution (Python):
s = input()
ch = 0
for i in range(len(s)-1):
    if s[i+1] != "9":
        ch = 1
        break
cnt = 9*(len(s)-1)
if ch == 1:
    print(cnt + int(s[0])-1)
else:
    print(cnt + int(s[0]))

Solution (Python):
n = list(map(int, list(input())))

ans = 0
for i, e in enumerate(n):
    if i == 0:
        ans += n[i]
        continue

    if n[i] != 9:
        ans = ans - 1 + 9 * (len(n) - i)
        break

    ans += 9

print(ans)

Solution (Python):
n = int(input())

lst = list(map(int,str(n)))
if len(lst) == 1:
    print(n)
elif set(lst[1:]) == {9}:
    print(lst[0] + 9*(len(lst)-1))
else:
    print((lst[0]-1) + 9*(len(lst)-1))

Solution (Python):
def ketawa(x):
    l = list(str(x))
    wa = 0
    for a in l:
        wa += int(a)
    return wa

n = int(input())
lst = list(str(n))
keta = len(str(n))




nine = int("".join([lst[0]] + ["9"]*(keta-1)))

if nine <= n:
    print(ketawa(nine))
else:
    lst_head = str(int(lst[0]) - 1)
    nine = int("".join([lst_head] + ["9"]*(keta-1)))
    print(ketawa(nine))

Solution (Python):

import sys
input=sys.stdin.readline

def main():
    n = list(map(int,input().rstrip()))
    digit=len(n)
    ans1=0
    ans2=0

    if digit==1:
        print(n[0])
        exit()
    else:
        ans1=sum(n)
        ans2=n[0]-1+(digit-1)*9
        print(max(ans1,ans2))

if __name__=="__main__":
    main()

Solution (Python):
N = input()

def rest_nine(x):
  for i in range(1, len(x)):
    if int(x[i]) != 9:
      return False
  return True

if rest_nine(N):
  print(int(N[0])+(len(N)-1)*9)
else:
  print(int(N[0])-1+(len(N)-1)*9)

Solution (Python):
n = list(input())
if len(n) == 1:
    print(n[0])
    exit()
n = [int(n[i]) for i in range(len(n))]
m = set(n[1:])
if m == {9}:
    print(sum(n[1:]) + n[0])
    exit()
else:
    print(9*(len(n)-1) + n[0] -1)

Solution (Python):
N = input()
N_i = int(N)
N_s = list(N)


for i in range(0,17):
  if N_i < 10 ** i:
    break
flag = True
for j in range(1,i):
  if N_s[j] != "9":
    flag = False
    break

if flag == True:
  
   print(int(N_s[0])+9*(len(N_s)-1))
else:
   print(int(N_s[0])+9*(len(N_s)-1)-1)

Solution (Python):



def main():
    S = input()
    N = len(S)
    ans = 0
    if N == 1:
        ans = int(S)
    else:
        if S[1:] == "9" * (N - 1):
            ans += 1
        ans += int(S[0]) - 1
        ans += 9 * (N - 1)

    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
def main():
    N = input()
    lenN = len(N)
    listN = list(N)
    N2 = [int(i) for i in listN]
    bf = sum(N2)
    ans = 0
    for i, s in enumerate(N):            
        if s == '9':
           ans += 9
        else:
            if i == 0:
                ans += int(s)-1 + 9*(lenN - i - 1)
                break
            else:
                ans += 9*(lenN - i) - 1
                break
    ans = max(ans, bf)
    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
n = list(str(input()))
maxi = 0

for i in range(len(n)):
  
  maxi = max(maxi, int(n[i])-1 + (len(n)-i-1)*9)
print(max(sum(map(int, n)), maxi))

Solution (Python):
N = int(input())

N = N + 1

N_str = str(N)

first_N = int(N_str[0]) - 1
num = len(N_str)-1

print(num*9+first_N)

Solution (Python):
import sys
N = list(input())

if N.count("9") == len(N):
    print(len(N)*9)
    sys.exit(0)
if N.count("9") == len(N)-1:
    for n in range(len(N)):
        if N[n] != "9":
            if n == 0:
                i = int(N[n])
                print(i+((len(N)-1)*9))
                sys.exit(0)
            else:
                print(8+((len(N)-1)*9))
                sys.exit(0)

for n in range(len(N)-1):
    i = int(N[n])
    if i != 9:
        cnt1 = len(N[:n])
        cnt2 = len(N[n+1:])
        print((cnt1*9)+(i-1)+(cnt2*9))
        sys.exit(0)

Solution (Python):
import itertools as it

N = list(input())
l = len(N)
a = N[0]+"9"*(l-1) 

ans = int(N[0])+9*(l-1)

if int("".join(N))!=int(a):
  ans-=1

print(ans)

Solution (Python):
import itertools as it

N =  int(input())
n =list(map(str,range(10)))
ans = 0
for i in it.combinations_with_replacement(n,16):
  a = int("".join(i))
  if a <= N:
    b = sum(map(int,i))
    ans = max(ans,b)
print(ans)

Solution (Python):
import sys, math
from functools import lru_cache
from collections import deque
sys.setrecursionlimit(10**9)
MOD = 10**9+7

def input():
    return sys.stdin.readline()[:-1]

def mi():
    return map(int, input().split())

def ii():
    return int(input())

def i2(n):
    tmp = [list(mi()) for i in range(n)]
    return [list(i) for i in zip(*tmp)]

N = ii()
m = 0
k = N
while k > 0:
    m += k%10
    k //= 10
print(max(m, int(str(N)[0])-1+9*(len(str(N))-1)))

Solution (Python):
n = int(input())
ans = n

if 8 < n:
    num = 8
    ans = 8
    add = 1
    flag = 0
    
    while 1:
        add *= 10
        
        for i in range(9):
            num += add
            ans += 1
            
            if n <= num:
                flag = 1
                break
                
        if flag == 1:
            break
            
print(ans)

Solution (Python):
n = int(input())
if (n//10**(len(str(n))-1)+1)*10**(len(str(n))-1)-1 > n:
  mx = (n//10**(len(str(n))-1))*10**(len(str(n))-1)-1
else:
  mx = (n//10**(len(str(n))-1)+1)*10**(len(str(n))-1)-1
print(sum(list(map(int,str(mx)))))

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

n = int(input())
str_n = str(n)

if n < 10:
    print(n)
    sys.exit(0)

ans = 0
while True:
    if n // 10 == 0:
        break
    ans += 9
    n //= 10


if set(str_n[1:]) == {'9'}:
    ans += n
else:
    ans += (n - 1)

print(ans)

Solution (Python):
N = input()
l = list(map(int,N))
i = 0


if int(N) < 9:
    ans  = int(N)

elif l.count(9) >= len(l)-1:
    if l[len(l)-1] != 9:
        spot = 8
    else:
        spot = min(l)
    ans = spot + (len(l)-1)*9

else:
    while True:
        if l[i] < 9 and i < len(l)-1:
            spot = l[i] - 1
            break
        else:
            i += 1 
    ans = spot + (len(l)-1)*9

print(ans)

Solution (Python):
n=input()
k=len(n)
if k==1:
    print(n);exit()
nn=int(n)
ans= nn//(10**(k-1))-1+9*(k-1)
print(max(ans, sum([int(i) for i in n]) ))

Solution (Python):
n=input()
if n[1:]=='9'*(len(n)-1): print(9*(len(n)-1)+int(n[0]))
else: print(9*(len(n)-1)+int(n[0])-1)

Solution (Python):
n = input(); a = 0; b = 0; c = int(n[0])
for i in range(len(n)):
    if n[i] != "9":
        if a == 0: a += 1; b = i
        else: a += 1
if a == 0: print(9*len(n))
elif a == 1 and b == 0: print(c+9*(len(n)-1))
else: print(c-1+9*(len(n)-1))

Solution (Python):
N = input()
len_N = len(N)

if N[1:] == '9' * (len_N - 1):
    tmp = int(N[0]) + (len_N - 1) * 9
else:
    tmp = int(N[0]) + (len_N - 1) * 9 - 1
print(tmp)

Solution (Python):
N = list(input())
N_int = [int(n) for n in N]
cnt = 0

for i in range(len(N_int)-1):
    if N_int[i+1] != 9:
        N_int[i] -= 1
        for j in range(i+1, len(N_int)):
            N_int[j] = 9

for i in N_int:
    cnt += int(i)

print(cnt)

Solution (Python):
N=list(str(input()))
ans=0
if len(N)==1:
    print(int(N[0]))
    exit()
for i in range(1,len(N)):
    if N[i]!='9':
        ans+=int(N[0])-1
        ans+=(len(N)-1)*9
        print(ans)
        exit()
for i in range(len(N)):
    ans+=int(N[i])
print(ans)

Solution (Python):
N=int(input())
S=str(N)
if len(S)==1:
  print(N)
  exit()
a=int(S[0])
b=len(S)
N=a+((b-1)*9)-1
if int(S[1:])==(10**(b-1))-1:
  N+=1
print(N)

Solution (Python):
def main():
    n = input()
    len_n = len(n)
    if n[1:] == "9" * (len_n - 1):
        print(int(n[0]) + 9 * (len_n - 1))
    else:
        print(int(n[0]) + 9 * (len_n - 1) - 1)


if __name__ == "__main__":
    main()

Solution (Python):
n = list(input())
n = list(map(int,n))
m = len(n)
if m == 1:
    print(n[0])
elif len(set(n)) == 1 and n[0] == 9:
    print(9 * m)
elif len(set(n)) == 2 and n[0] != 9 and n[1] == 9:
    print(n[0] + (m - 1) * 9)
else:
    print(n[0] - 1 + (m - 1) * 9)

Solution (Python):
def solve():
    N = input()
    if len(N) == 1:
        print(int(N))
    elif all(i == '9' for i in N[1:]):
        print(int(N[0]) + 9 * (len(N) - 1))
    else:
        print((int(N[0]) - 1) + 9 * (len(N) - 1))

if __name__ == "__main__":
    solve()

Solution (Python):
N = input()
a = ''
kazu = int(N[0])
for i in range(len(N) - 1):
  a = a + '9'
if N[1:] == a:
  print(9 * (len(N) - 1) + kazu)
else:
  print(9 * (len(N) - 1) + kazu - 1)

Solution (Python):




def resolve():
    import sys
    input = sys.stdin.readline

    num_list = [int(x) for x in input().rstrip()]

    ans = sum(num_list)

    for i in range(1, len(num_list)+1):
        tmp = sum([x for x in num_list[:i]]) - 1 + 9 * (len(num_list) - i)
        if tmp > ans:
            ans = tmp

    print(ans)


if __name__ == "__main__":
    resolve()

Solution (Python):
def examA():
    N = SI()
    n = len(N)
    if n==1:
        print(N)
        return
    ans = (n-1)*9 + int(N[0]) - 1
    if int(N[1:])==int("9"*(n-1)):
        ans += 1
    print(ans)
    return


def examB():
    def norm2(vec):
        return math.sqrt(vec[0] ** 2 + vec[1] ** 2)
    
    def ConvexHull(point_list):
        pos2idx = {point_list[i]: i for i in range(len(point_list))}
        y_val = defaultdict(list)
        x_list = sorted(list(set([p[0] for p in point_list])))
        for x, y in point_list:
            y_val[x].append(y)

        upper = [(x_list[0], max(y_val[x_list[0]]))]
        lower = [(x_list[0], min(y_val[x_list[0]]))]
        prev = float('inf')
        for xi in x_list[1:]:
            x0, y0 = upper[-1]
            x1, y1 = xi, max(y_val[xi])
            if (y1 - y0) / (x1 - x0) < prev:
                upper.append((x1, y1))
                prev = (y1 - y0) / (x1 - x0)
            else:
                while True:
                    x0, y0 = upper[-1]
                    if len(upper) == 1:
                        upper.append((x1, y1))
                        break
                    x00, y00 = upper[-2]
                    if (y1 - y0) / (x1 - x0) > (y1 - y00) / (x1 - x00):
                        upper.pop()
                    else:
                        prev = (y1 - y0) / (x1 - x0)
                        upper.append((x1, y1))
                        break

        prev = -float('inf')
        for xi in x_list[1:]:
            x0, y0 = lower[-1]
            x1, y1 = xi, min(y_val[xi])
            if (y1 - y0) / (x1 - x0) > prev:
                lower.append((x1, y1))
                prev = (y1 - y0) / (x1 - x0)
            else:
                while True:
                    x0, y0 = lower[-1]
                    if len(lower) == 1:
                        lower.append((x1, y1))
                        break
                    x00, y00 = lower[-2]
                    if (y1 - y0) / (x1 - x0) < (y1 - y00) / (x1 - x00):
                        lower.pop()
                    else:
                        prev = (y1 - y0) / (x1 - x0)
                        lower.append((x1, y1))
                        break

        
        

        upper_idx, lower_idx = [pos2idx[xy] for xy in upper], [pos2idx[xy] for xy in lower]
        if upper_idx[-1] == lower_idx[-1]:
            upper_idx.pop()
        CH_idx = upper_idx
        CH_idx.extend(reversed(lower_idx))
        if CH_idx[0] == CH_idx[-1] and len(CH_idx) > 1:
            CH_idx.pop()
        return CH_idx

    N = I()
    P = [[]for _ in range(N)]
    D = defaultdict(int)
    for i in range(N):
        x,y = LI()
        P[i] = (x,y)
        D[(x,y)] = i
    C = ConvexHull(P)
    ans = [0]*N
    if len(C)==2:
        for c in C:
            ans[c] = 0.5
        for v in ans:
            print(v)
        return
    
    for i,c in enumerate(C):
        s, t, u = C[i - 1], C[i], C[(i + 1) % len(C)]
        x0, y0 = P[s]
        x1, y1 = P[t]
        x2, y2 = P[u]
        vec0 = (y0 - y1, x1 - x0)
        vec1 = (y1 - y2, x2 - x1)
        ans[t] =  math.acos((vec0[0] * vec1[0] + vec0[1] * vec1[1]) / (norm2(vec0) * norm2(vec1))) / (2 * math.pi)
    for v in ans:
        print(v)
    return

def examC():
    ans = 0
    print(ans)
    return

def examD():
    S = SI()
    N = len(S)
    K = I()
    if K>150:
        K = 150
    dp = [[[0]*(K+1) for _ in range(N)] for i in range(N)]
    for i in range(N):
        for j in range(K+1):
            dp[i][i][j] = 1
    for i in range(2,N+1):
        for k in range(K+1):
            for l,r in enumerate(range(i-1,N)):
                if S[l]==S[r]:
                    dp[l][r][k] = dp[l+1][r-1][k] + 2
                else:
                    if k>0:
                        dp[l][r][k] = max(dp[l+1][r][k],dp[l][r-1][k],dp[l+1][r-1][k-1]+2)
                    else:
                        dp[l][r][k] = max(dp[l + 1][r][k], dp[l][r - 1][k])
    ans = max(dp[0][-1])
    
    print(ans)
    return

def examE():
    ans = 0
    print(ans)
    return

def examF():
    ans = 0
    print(ans)
    return

from decimal import Decimal as dec
import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
def I(): return int(input())
def LI(): return list(map(int,sys.stdin.readline().split()))
def DI(): return dec(input())
def LDI(): return list(map(dec,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = 10**(-12)
alphabet = [chr(ord('a') + i) for i in range(26)]

sys.setrecursionlimit(10**7)

if __name__ == '__main__':
    examA()

"""
142
12 9 1445 0 1
asd dfg hj o o
aidn
"""

Solution (Python):
S = input()
N = int(S)
K = len(S)
c = int(S[0])

if len(S) == 1:
  ans = c
elif (N+1)/(10**(K-1))-1 == c:
  ans = c + 9*(K-1)
else:
  ans = (c-1) + 9*(K-1)

print(ans)

Solution (Python):
n=input()
l=len(n)-1
if int(n)==(int(n[0])+1)*10**l-1:
    print(int(n[0])+9*l)
else:
    print(int(n[0])+9*l-1)

Solution (Python):
N = input()
tmp = len(N) - 1
NL = list(N)
if len(N) == 1:
    print(int(N))
elif all(x == "9" for x in NL[1:]):
    print(int(N[0]) + tmp * 9)
else:
    print(int(N[0]) - 1 + tmp * 9)

Solution (Python):
def solve(N):
    if len(N) > 1:
        tmp = N[0] + '9' * (len(N) - 1)
    else:
        print(int(N))
        exit()

    if int(tmp) > int(N):
        tmp = str(int(N[0]) - 1) + '9' * (len(N) - 1)

    ans = 0
    for s in tmp:
        ans += int(s)

    return ans


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

Solution (Python):
n = input()

if n[1:] == "9" * (len(n) - 1) :
    num = (int(n[0])) + 9 * (len(n) - 1)
else:
    num = (int(n[0]) - 1) + 9 * (len(n) - 1)

print(num)

Solution (Python):
import sys
input = sys.stdin.readline

def main():
    s = input()[:-1]
    n = len(s)
    dp = [[0, 0] for _ in range(n+1)]
    
    for i in range(n):
        key = int(s[n-1-i])
        dp[i+1][0] = dp[i][0] + key
        
        if key > 0:
            dp[i+1][0] = max(dp[i+1][0], dp[i][1] + key - 1)
        
        dp[i+1][1] = max(dp[i][0], dp[i][1]) + 9
    
    print(dp[n][0])
    
    
if __name__ == "__main__":
    main()

Solution (Python):
n=input()
h=len(n)
if h==1:
    print(int(n))
else:
    if n[1:]=="9"*(h-1):
        print(int(n[0])+9*(h-1))
    else:
        print(int(n[0])-1+9*(h-1))

Solution (Python):
N = input()
if len(N)==1:
    print(int(N))
else:
    if all(N[i]=='9' for i in range(1,len(N))):
        print(sum(int(i) for i in N))
    else:
        print(int(N[0])-1 + 9*(len(N)-1))

Solution (Python):
s = input()

n = len(s)
ok = False
ans = 9
if n == 1:
    print(s)
    exit()

for i in range(n-1):
    if ok:
        ans += 9
        continue
    if s[i+1] == '9':
        ans += ord(s[i]) - ord('0')
        continue
    x = ord(s[i]) - ord('0')
    ans += x-1
    ok = True

print(ans)

Solution (Python):
import math
n = int(input())
k = int(math.log10(n))

a = n // (10 ** k)
b = n % (10 ** k)

if k == 0:
  ans = n
elif n % 10 == 9:
  ans = k * 9 + a
else:
  ans = k * 9 + a -1

print(ans)

Solution (Python):
N=input()
l=len(N)-1
h=int(N[0])
N=int(N)
if N==(h+1)*10**l-1:
  print(h+9*l)
else:
  print(h+9*l-1)

Solution (Python):
n = int(input())
L = len(str(n))

def sumdigit(i):
    return sum(map(int, list(str(i))))

print(max([sumdigit(n - (n%(10**i) + 1)) for i in range(1, L)] + [sumdigit(n)]))

Solution (Python):
N=list(input())
if len(N) == 1:
    print(int(N[0]))
    exit()
all9 = True
for i in range(1,len(N)):
    if N[i] != '9':
        all9 = False
        break

c = int(N[0])
if not all9:
    c -= 1
print(c + 9 * (len(N)-1))

Solution (Python):
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():
    n = I()
    ans = 0
    if n<10:
        ans = n
    else:
        keta = len(str(n))
        top = int(str(n)[0])
        down = int(str(n)[1:])
        if top == 1:
            if down == 10**(keta-1)-1:
                ans = 1+(keta-1)*9
            else:
                ans = (keta-1)*9
        else:
            if down == 10**(keta-1)-1:
                ans = top+(keta-1)*9
            else:
                ans = top-1+(keta-1)*9
    
    print(ans)

main()

Solution (Python):
N = list(map(int,list(input())))
ans = max(sum(N),N[0]-1+9*(len(N)-1))
print(ans)

Solution (Python):
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np
from functools import reduce

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.readline().split())
rf = lambda: map(float, sys.stdin.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7

s = rr()
lens = len(s)
a = int(s[0])
b = s[1:]
t = '9'*(lens-1)
if b == t:
    print((lens-1)*9+a)
else:
    print((lens-1)*9+a-1)

Solution (Python):
n = list(input())
n = [int(i) for i in n]
digit = len(n)


if n[1:] == [9]*(digit-1):
    ans = sum(n)
else:
    ans = (n[0]-1)+9*(digit-1)
print(ans)

Solution (Python):
N = input()
N_row = len(N)
while True:
  if N_row == 1:
    print(int(N))
    break
  else:
    N = list(N)
    a = int(N[0])
    b = int(N[-1])
    for i in range(1, N_row):
      if not int(N[i]) == 9:
        sum = (a-1) + 9*(N_row-1) 
        print(sum)
        break
    else:
      sum = a + 9*(N_row-1)
      print(sum)
      break
  break

Solution (Python):
n = int(input())
s = str(n)
l = list(s)
for i in range(len(l)):
    l[i] = int(l[i])
print(max(sum(l), int(l[0])-1 + (len(l)-1)*9))

Solution (Python):
N = input()
N_list = list(N)
N = int(N)
K = len(N_list)

ans = int(N_list[0])+(K-1)*9
ans_list = [N_list[0]] + ['9' for _ in range(K-1)]
ans_cand = int(''.join(ans_list))
if N < ans_cand:
    print(ans - 1)
else:
    print(ans)

Solution (Python):
n = input()

ans = 0
s = 0
for i in range(len(n)):
    ans = max(ans, s + int(n[i]) - 1 + 9 * (len(n) - i - 1))
    s += int(n[i])
ans = max(ans, s)

print(ans)

Solution (Python):

n=input()
keta=len(n)
c=n[0]
ans=int(c)+9*(keta-1)
if n[1:]!=("9"*(keta-1)):
    ans-=1
print(ans)

Solution (Python):
n=input()

l=len(n)
if int(n)>9:
  ans=[9]
else:
  print(int(n))
  exit()
tmp=n[l-1]
for i in range(2,l+1):
  
  if int(tmp) != 9:
    tmp=int(n[l-i])-1
    
  else:
    tmp=int(n[l-i])
  if i==l:
    ans.append(int(tmp))
  else:
    ans.append(9)
  
print(sum(ans))

Solution (Python):
n = list(input())
n = [int(i) for i in (n)]
m = len(n)
if m == 1:
    print(n[0])
else:
    flg = False
    for i in range(1,m):
        if n[i] != 9:
            flg = True
            break
    if flg:
        print(n[0] - 1 + 9 * (m - 1))
    else:
        print(sum(n))

Solution (Python):
n = list(input())
n = [int(i) for i in (n)]
m = len(n)
if m == 1:
    print(n[0])
else:
    flg = False
    for i in range(1,m):
        if n[i] != 9:
            flg = True
            break
    if flg:
        n[0] -= 1
        for i in range(1,m):
            n[i] = 9
    print(sum(n))

Solution (Python):
import collections


def main():
    n = input()
    if n[0] == "9":
        if n[1:] == "9" * len(n[1:]):
            print(9 * len(n))
        else:
            print(9 * len(n) - 1)
    else:
        if n[1:] == "9" * len(n[1:]):
            print(int(n[0]) + 9 * len(n[1:]))
        else:
            print(int(n[0]) + 9 * len(n[1:]) - 1)


if __name__ == '__main__':
    main()

Solution (Python):
def digitsum(N):
    return sum(map(int,list(str(N))))
N = int(input())
print(max(digitsum(N),int(str(N)[0])-1+9*(len(str(N))-1)))

Solution (Python):
N = input()
if len(N) == 1 or N[1:len(N)].count('9') == len(N)-1:
    print(sum([int(i) for i in N]))
else :
    print(int(N[0])-1+9*(len(N)-1))

Solution (Python):
N = input()
if N[0] == "9":
    if N[1:] == "9" * len(N[1:]):
        print(9 * len(N))
    else:
        print(9 * len(N) - 1)
else:
    if N[1:] == "9" * len(N[1:]):
        print(int(N[0]) + 9 * len(N[1:]))
    else:
        print(int(N[0]) + 9 * len(N[1:]) - 1)

Solution (Python):
N = input()
if N[0] == "9":
    if N[1:] == "9" * len(N[1:]):
        print(9 * len(N))
    else:
        print(9 * len(N) - 1)
else:
    if N[1:] == "9" * len(N[1:]):
        print(int(N[0]) + 9 * len(N[1:]))
    else:
        print(int(N[0]) + 9 * len(N[1:]) - 1)

Solution (Python):
def main():
    N = int(input())
    
    if N % 10 == 0:
        N -= 1
    else:
        first = 0
        M = N
        while True:
            d = M % 10
            M //= 10
            if M == 0:
                first = d
                break

        if N != (first + 1) * (10 ** (len(str(N)) - 1)) - 1:
            N = (first) * (10 ** (len(str(N)) - 1)) - 1

    ans = 0
    N = str(N)
    for d in N:
        ans += int(d)

    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
N = input()
if N[0] == "9":
    if N[1:] == "9"*len(N[1:]):
        print(9*len(N))
    else:
        print(9*len(N)-1)
else:
    if N[1:] == "9"*len(N[1:]):
        print(int(N[0]) + 9*len(N[1:]))
    else:
        print(int(N[0]) + 9*len(N[1:]) - 1)

Solution (Python):
s = input()
cnt = len(s)

if s[0] == "9":
    if s[1:] == "9"*(cnt-1):
        print(9*cnt)
    else:
        print(8+9*(cnt-1))
else:
    if s[1:] == "9"*(cnt-1):
        print(int(s[0]) + 9*(cnt-1))
    else:
        print((int(s[0])-1) + 9*(cnt-1))

Solution (Python):
n = input()
keta = len(n)
if n[0] == '9':
    
    if n[1:] == '9'*(keta-1):
        print(9*keta)
    
    else:
        print(9*keta-1)
else:
    
    if n[1:] == '9'*(keta-1):
        print(int(n[0]) + 9*(keta-1))
    
    else:
        print(int(n[0])-1 + 9*(keta-1))

Solution (Python):
import sys
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
read = sys.stdin.buffer.read
sys.setrecursionlimit(10 ** 7)
INF = float('inf')

from functools import lru_cache

def F(N):
    if N < 10:
        return N
    q, r = divmod(N, 10)
    x1 = F(q) + r
    x2 = F(q-1) + 9
    return max(x1, x2)
ans = F(int(input()))
print(ans)

Solution (Python):
import sys
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
read = sys.stdin.buffer.read
sys.setrecursionlimit(10 ** 7)
INF = float('inf')

N = input()

sN = set(N)
if len(N) == 1:
    ans = int(N)
elif len(N) > 1 and N[1:] == "9" * (len(N) - 1):
    ans = sum(map(int, list(N)))
else:
    ans = (len(N) - 1) * 9
    fst = int(N[0]) - 1
    ans += fst
print(ans)

Solution (Python):
n = input()

tmp = len(n)-1

if int(n[0] + tmp*'9') == int(n):
    print(9*tmp + int(n[0]))
else:
    print(9*tmp + int(n[0])-1)

Solution (Python):
N=input()

S=[-1]*len(N)
for i in range(len(N)):
    S[i]=int(N[i])

if sum(S[1:])== 9*(len(S)-1):
    print(sum(S))
else:
    print(9*(len(S)-1) + (S[0]-1))

Solution (Python):
n = input()

if len(list(n)) == 1:
    print(int(n))
    exit()

if set(list(n)) == {'9'}:
    print(len(list(n))*9)
    exit()

tmp = ''
for i in range(1,len(n)):
    if n[i] == '9':
        tmp += n[i-1]
    else:
        tmp += str(int(n[i-1])-1) + '9'*(len(n)-i)
        break
else:
    tmp += '9'

ans=0
for i in range(len(tmp)):
    ans += int(tmp[i]) 

print(ans)

Solution (Python):
N=input()

k=len(N)
A='9'*(k-1)

if N[0]==1:
    if N[1:]==A:
        print(k*9-8)
    else:
        print((k-1)*9)

else:
    if N[1:]==A:
        print(int(N[0])+(k-1)*9)
    else:
        print(int(N[0])+(k-1)*9-1)

Solution (Python):
s = input()
n = len(s)

ans = int(s[0]) + 9 * (n - 1)

if n != 1:
  if s[1:] != '9' * (n-1):
    ans -= 1

print(ans)

Solution (Python):
s = input()
n = len(s)

ans = 0
if s[1:] == '9' * (n - 1):
  ans = int(s[0]) + 9 * (n - 1)
elif n != 1:
  ans = int(s[0]) + 9 * (n - 1) - 1
else:
  ans = int(s[0])

print(ans)

Solution (Python):
n=input()
l=len(n)
if all(n[i]=="9" for i in range(1,l)):
  print(int(n[0])+9*(l-1))
else:
  print(int(n[0])-1+9*(l-1))

Solution (Python):
N = [int(i) for i in list(input())][::-1]

if len(N) == 1:
    print(N[0])
    exit()

p = N[-1]
q = sum(N)

ans = 0
for i in range(len(N)-1):
    if N[i] != 9:
        for j in range(i,len(N)):
            if N[j] != 0:
                N[j] -= 1
                N[i] = 9
            else:
                N[j] = 9

N[-1] = p-1
print(max(sum(N),q))

Solution (Python):
N = list(map(int, list(input())))
x = (N[0])-1 + 9*(len(N)-1)
print(max(sum(N), x))

Solution (Python):
n = input()
if n[1:] == "9" *(len(n)-1):
  print(int(n[0]) + 9 * (len(n)-1))
else:
  print((int(n[0])-1) + 9 * (len(n)-1))

Solution (Python):
n = input()

ans = None
for i in n[1:]:
    if int(i) != 9:
        ans = int(n[0]) - 1 + sum([9]*(len(n)-1))
        break

if ans is None:
    ans = sum([ int(i) for i in list(n)  ])
       
print(ans)

Solution (Python):
N = int(input())

n = str(N)

res = 0

if len(n) == 1:
    res = N

else:
    nine = True
    for i in range(1, len(n)):
        if n[i] != "9":
            nine = False
            break

    if nine:
        res = int(n[0]) + 9 * (len(n) - 1)
    else:
        res = (int(n[0]) - 1) + 9 * (len(n) - 1)

print(res)

Solution (Python):
import collections
import sys
import numpy as np
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
MOD = 10**9+7
import itertools
import math

n = int(input())
s = str(n)
k = len(s)
c = int(s[0])
if s == str(c) + "9"*(k-1):
    print(c + 9*(k-1))
else:
    print(c+9*(k-1)-1)

Solution (Python):
def resolve():
    N = int(input())
    N += 1

    ans = 0
    for i, n in enumerate(range(len(str(N)))):
        if i == 0:
            continue
        ans += int(9)
    ans += int(str(N)[0])-1
    print(ans)
    return


resolve()

Solution (Python):
'''
自宅用PCでの解答
'''
import math

import itertools
import queue
import bisect
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit


ipt = stdin.readline
setrecursionlimit(10**7)
mod = 10**9+7
alp = "abcdefghijklmnopqrstuvwxyz"

def main():
    n = input()
    ln = len(n)
    ma = sum(map(int,n))
    ma2 = int(n[0])-1+9*(ln-1)
    if ma < ma2:
        print(ma2)
    else:
        print(ma)


    return None

if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
sn = str(n)
k = len(sn)
c = int(sn[0])
if sn == str(c) + "9" * (len(sn) - 1):
    print(c + 9 * (k - 1))
else:
    print(c + 9 * (k - 1) - 1)

Solution (Python):
def resolve():
    n = input()
    l = len(n)
    if n[1:] == "9" * (l-1): 
        print(int(n[0]) + 9*(l-1))
    else:
        print((int(n[0])-1) + (9*(l-1)))
resolve()

Solution (Python):
N = list(map(int,list(input())))
ans = max(sum(N),N[0]-1+9*(len(N)-1))
print(ans)

Solution (Python):
X = list(map(int,input()))
sn = []
for i in range(len(X)):
  sn.append(sum(X[0:i+1]))
ssn = []
for i in range(len(X)):
  if (sn[i] != 0):
    ssn.append(sn[i]-1 + 9*(len(X)-1-i))
ssn.append(sum(X))
print(max(ssn))

Solution (Python):
n = input()
n_len = len(n)

ans = 0
for i in range(n_len+1):
    curr_sum = 0
    for j in range(n_len):
        num = int(n[j])
        if j < i:
            curr_sum += num
        elif j == i:
            if num == 0: curr_sum = -10000000000
            curr_sum += (num-1)
        else:
            curr_sum += 9
    ans = max(ans, curr_sum)

print(ans)

Solution (Python):
N = input().strip()
k = len(N)
if k==1:
    print(int(N))
else:
    if N[1:]=="9"*(k-1):
        print(int(N[0])+9*(k-1))
    else:
        print(int(N[0])-1+9*(k-1))

Solution (Python):
n = int(input())
s = str(n)
ans1 =0
for i in range(len(s)):
    ans1 += int(s[i])
ans2 = (len(s)-1)*9+int(s[0])-1
print(max(ans1, ans2))

Solution (Python):
s = list(input())
ans = [sum(map(int, s)), 9 * (len(s) - 1) + int(s[0]) - 1]
print(max(ans))

Solution (Python):
S = input()
N = int(S)
ans = sum(map(int,S))

for i in range(1,len(S)):
    n = str(int(S[:i]) - 1)
    t = sum(map(int,n)) + 9*(len(S)-i)
    ans = max(ans,t)
print(ans)

Solution (Python):
N = input()
n = len(N)

if N[1:] == "9"*(n-1):
  ans = int(N[0])+9*(n-1)
else:
  ans = int(N[0])-1 + 9*(n-1)

print(ans)

Solution (Python):
n =input()





c = int(n[0])
k = len(n)
if int(n)<10:
    print(n);exit()
if int(n[1:])==int('9'*(k-1)):
    ans = c+9*(k-1)
else:
    ans = c+9*(k-1)-1

print(ans)

Solution (Python):
n = int(input())

s = str(n)
l = len(s)
sum1 = 0

s = str(n)
for i in range(l):
    sum1 +=int(s[i])

ans = 0
for i in range(l):
    if i == 0:
        sum2 = (int(s[i])-1) + 9*(l-1) 
    else:
        sum2 = int(s[0]) + 9*(l-2) + int(s[i])-1

    ans = max(ans,max(sum1,sum2))

print(ans)

Solution (Python):
N = input()
n = len(N)
if N[1:] == "9"*(n-1):
  ans = int(N[0])+9*(n-1)
else:
  ans = int(N[0])-1 + 9*(n-1)
print(ans)

Solution (Python):
n=int(input())
k=len(str(n))
c=n//10**(k-1)
print(c+9*(k-1) if n==(c+1)*10**(k-1)-1 else c+9*(k-1)-1)

Solution (Python):
N = int(input())
keta = len(str(N))
SN = str(N)
if N <= 9:
    print(N)
elif SN.count('9') == keta:
    print(9*keta)
elif SN.count('9') == keta-1 and SN[0] != '9':
    print((keta - 1) * 9 + int(SN[0]))
else:
    print((keta - 1) * 9 + int(SN[0])-1)

Solution (Python):
N = int(input())
K = len(str(N))
c = N // 10**(K-1)

print(c+9*(K-1) if N ==(c+1)*10**(K-1)-1 else c+9*(K-1)-1)

Solution (Python):
dd = list(map(int, open(0).read().rstrip()))
n = len(dd)

ans = sum(dd)
tmp = 0

for i in range(n):
    if dd[i] > 0:
        tmp += dd[i]
        ans = max(ans, tmp - 1 + (n-i-1)*9)

print(ans)

Solution (Python):
n=int(input())
k=len(str(n))
c=n//10**(k-1)
print(c+9*(k-1) if n==(c+1)*10**(k-1)-1 else c+9*(k-1)-1)

Solution (Python):
n = input()
if n[1:] == "9" * (len(n)-1) :
    print(int(n[0])+9*(len(n)-1))
else :
    print(int(n[0])+9*(len(n)-1)-1)

Solution (Python):
N = input()
print(max(sum([int(i) for i in N]),(len(N)-1)*9+int(N[0])-1))