d56b54da3cecb329e7e7c9e32bd88099b159d53b64cec7be92b65ad83b1facb8
// https://szkopul.edu.pl/problemset/problem/EoHxXpP-oYOLwSqFmAfGQJvW/site/?key=statement
// OIJ XIV próbne zawody 2 stopnia
#include <iostream>
#include <math.h>
#include <stack>
#include <vector>
int sum_of_digits(long long x) {
if (x == 0) {
return 0;
}
return x % 10 + sum_of_digits(x / 10);
}
int main() {
std::ios_base::sync_with_stdio(0);
std::cin.tie(0);
std::cout.tie(0);
long long a, b;
std::cin >> a >> b;
if (b - a <= 1000 * 1000) {
int ans = 0;
for (auto i = a; i <= b; i++) {
ans = std::max(ans, sum_of_digits(i));
}
std::cout << ans << '\n';
return 0;
}
int ans = 0, ans3;
std::stack<int> dig_a, dig_b;
const auto log_a = floor(log10(a)), log_b = floor(log10(b));
for (int i = 0; i <= log_a; i++) {
dig_a.push(a % 10);
a /= 10;
}
for (int i = 0; i <= log_b; i++) {
dig_b.push(b % 10);
b /= 10;
}
if (log_a == log_b) {
bool flag = false, flag2 = false;
while (!dig_a.empty()) {
auto ra = dig_a.top(), rb = dig_b.top();
dig_a.pop(), dig_b.pop();
if (flag) {
if (!flag2 && rb == 9 && !dig_b.empty() && dig_b.top() != 9) {
ans--;
flag2 = true;
}
ans += 9;
continue;
}
ans += rb;
if (ra != rb) {
flag = true;
if (!dig_b.empty() && dig_b.top() != 9) {
ans--;
flag2 = true;
}
}
}
std::cout << ans << '\n';
return 0;
}
int ans1 = 0, ans2 = 0;
if (!dig_b.empty() && dig_b.top() == 1) {
ans2 = (dig_b.size() - 1) * 9;
}
ans3 = dig_b.top() - 1 + (dig_b.size() - 1) * 9;
while (!dig_b.empty() && dig_b.top() == 9) {
auto temp = dig_b.top();
ans += temp;
ans1 += temp;
dig_b.pop();
}
if (!dig_b.empty()) {
auto temp = dig_b.top();
ans += temp - 1;
ans1 += temp;
dig_b.pop();
}
while (!dig_b.empty()) {
ans += 9;
ans1 += dig_b.top();
dig_b.pop();
}
std::cout << std::max(ans, std::max(ans1, std::max(ans2, ans3))) << '\n';
return 0;
}