#include #include #include #include #include #include #include using namespace std; int dist(int x, int y) { return (x > y) ? (x - y) : (y - x); } int count(int n) { int cnt = 0; stringstream ss; ss << static_cast >(n); for (auto itr = ss.str().begin(); itr != ss.str().end(); itr++) { if (*itr == '1') cnt++; } return cnt; } bool in(vector v, int n) { for (auto& var : v) { if (var == n) return true; } return false; } int solve(int n) { int cnt = 1; queue q; q.push(1); vector v; v.push_back(1); // 行ったことがある数字を覚えておく while (!q.empty()) { int tmp = q.front(); q.pop(); if (tmp == n) return cnt; int forward = count(tmp) + tmp; int backward = tmp - count(tmp); if (!in(v, forward) && (forward <= n)) { q.push(forward); v.push_back(forward); } else if (!in(v, backward) && (backward > 0)) { q.push(backward); v.push_back(backward); } cnt++; } return -1; } int main() { int n; cin >> n; cout << solve(n) << endl; return 0; }