#include <bits/stdc++.h>
#include <cassert>
typedef long long int ll;
using namespace std;

// @@ !! LIM(f:power cmpNaive)
// --> f:power cmpNaive
// ---- inserted function power from util.cc
/* *** WARNING ***  
      ll x = power(10, 12) 
   does not work since it is interpreted as 
      ll x = power<int>((int)10, 12)
   Use power<ll>(10, 12) or power(10LL, 12).
 */
template<typename T>
T power(T a, ll b) {
  T twoPow = a;
  T rv(1);
  while (b > 0) {
    if (b & 1LL) rv *= twoPow;
    twoPow *= twoPow;
    b >>= 1;
  }
  return rv;
}

// ad-hoc power function
template<typename T, typename Op>
T ah_power(T a, ll b, const T& unit_t, Op op) {
  T two_pow = a;
  T ret = unit_t;
  while (b > 0) {
    if (b & 1LL) ret = op(ret, two_pow);
    two_pow = op(two_pow, two_pow);
    b >>= 1;
  }
  return ret;
}
// ---- end power
// ---- inserted library file cmpNaive.cc

const string end_mark("^__=end=__^");
const string end_file_mark("^__=end_file=__^");

int naive(istream& cin, ostream& cout);
int body(istream& cin, ostream& cout);

void cmpNaive() {
  while (true) {
    string s;
    getline(cin, s);
    bool run_body;
    if (s.at(0) == 'Q') {
      return;
    }else if (s.at(0) == 'B') {
      run_body = true;
    }else if (s.at(0) == 'N') {
      run_body = false;
    }else {
      cerr << "Unknown body/naive specifier.\n";
      exit(1);
    }
    string input_s;
    while (true) {
      getline(cin, s);
      if (s == end_mark) break;
      input_s += s;
      input_s += "\n";
    }
    stringstream ss_in(move(input_s));
    stringstream ss_out;
    if (run_body) {
      body(ss_in, ss_out);
    }else {
      naive(ss_in, ss_out);
    }
    cout << ss_out.str() << end_mark << endl;
  }
}

int main(int argc, char *argv[]) {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << setprecision(20);

#if CMPNAIVE
  if (argc == 2) {
    if (strcmp(argv[1], "cmpNaive") == 0) {
      cmpNaive();
    }else if (strcmp(argv[1], "naive") == 0) {
      naive(cin, cout);
    }else {
      cerr << "Unknown argument.\n";
      exit(1);
    }
  }else {
#endif
    body(cin, cout);
#if CMPNAIVE
  }
#endif
  return 0;
}

// ---- end cmpNaive.cc
// @@ !! LIM  -- end mark --

int naive(istream& cin, ostream& cout) {
  ll N; cin >> N;
  ll ans = 0;
  for (ll i = 0; i <= N; i++) {
    ll sum = 0;
    ll x = i;
    while (x > 0) {
      sum += x % 10;
      x = x / 10;
    }
    ans = max(ans, sum);
  }
  cout << ans << endl;
  return 0;
}

int body(istream& cin, ostream& cout) {
  string N; cin >> N;
  ll k = N.size();
  if (k == 1) {
    cout << N << endl;
    return 0;
  }
  ll d = N.at(0) - '0';
  if (d < 9) {
    for (ll j = 1; j < k; j++) {
      if (N.at(j) - '0' < 9) {
	ll ans = (d - 1) + 9 * (k - 1);
	cout << ans << endl;
	return 0;
      }
    }
    cout << d + 9 * (k - 1) << endl;
    return 0;
  }
  ll ans = 0;
  for (ll i = 0; i < k; i++) {
    ll e = N.at(i) - '0';
    if (e == 9) {
      ans += 9;
    }else {
      ans--;
      ans += 9 * (k - i);
      for (ll j = 1; j < k; j++) {
	if (N.at(j) - '0' < 9) {
	  cout << ans << endl;
	  return 0;
	}
      }
      cout << ans + 1 << endl;
      return 0;
    }
  }
  cout << ans << endl;
  return 0;

  return 0;
}



Measured memory_bytes: 3664