111111
 100009
 10011
 1000000
 8
 10201
 102030
 908172
 123456
 415
 900000
 21
 909090
 314159
 999999
 909823
 987654
 1453
 1435
 98
 10
 2
 1

1
111111
 9
100001 1 1 1 1 1 1 1 1
 1
10011
 1
1000000
 8
1 1 1 1 1 1 1 1
 2
10101 100
 3
101010 1010 10
 9
101111 101011 101010 101010 101010 101010 101010 101000 100000
 6
111111 11111 1111 111 11 1
 5
111 101 101 101 1
 9
100000 100000 100000 100000 100000 100000 100000 100000 100000
 2
11 10
 9
101010 101010 101010 101010 101010 101010 101010 101010 101010
 9
111111 101011 101011 1011 11 1 1 1 1
 9
111111 111111 111111 111111 111111 111111 111111 111111 111111
 9
101111 101111 101101 101100 101100 101100 101100 101100 101000
 9
111111 111111 111111 111111 111110 111100 111000 110000 100000
 5
1111 111 111 110 10
 5
1111 111 111 101 1
 9
11 11 11 11 11 11 11 11 10
 1
10
 2
1 1
 1
1

#include <bits/stdc++.h>
using namespace std;
vector<long long> visited;
void solve(long long a) {
  if (a > 100000) return;
  visited.push_back(a);
  solve(a * 10);
  solve(a * 10 + 1);
}
signed main() {
  visited.push_back(0);
  solve(1);
  long long a;
  cin >> a;
  long long dp[a + 1];
  dp[0] = 0;
  for (long long i = 1; i <= a; i++) {
    dp[i] = (long long)INT_MAX;
    for (long long j = 0; j < visited.size(); j++)
      if (visited[j] <= i) {
        dp[i] = min(dp[i], 1 + dp[i - visited[j]]);
      }
  }
  cout << dp[a] << endl;
  return 0;
}
