#!/bin/bash

read N

digit=$(( `echo -n $N | wc -c` -1 ))
nines=$(( 9 * $digit ))

[ $digit -eq 0 ] && { echo $N ; exit; }

printf "%${nines}s" | tr " " "9" | fold -w${digit} | nl -v 0 | tr -d "0 \t" | tac | awk -v N=$N '{if(N>$0)print $0}' | head -1 | grep -o . | awk '{sum+=$0}END{print sum}'