#include <bits/stdc++.h>

// #undef DEBUG  // Uncomment this line to forcefully disable debug print.
#if DEBUG
template <typename T>
void debug(T value) {
  std::cerr << value;
}
template <typename T, typename... Ts>
void debug(T value, Ts... args) {
  std::cerr << value << ", ";
  debug(args...);
}
#define dbg(...)                              \
  do {                                        \
    cerr << #__VA_ARGS__ << ": ";             \
    debug(__VA_ARGS__);                       \
    cerr << " (L" << __LINE__ << ")" << endl; \
  } while (0)
#else
#define dbg(...)
#endif

void read_from_cin() {}
template <typename T, typename... Ts>
void read_from_cin(T& value, Ts&... args) {
  std::cin >> value;
  read_from_cin(args...);
}
#define in(type, ...) \
  type __VA_ARGS__;   \
  read_from_cin(__VA_ARGS__);

template <typename T>
void write_to_cout(const T& value) {
  std::cout << value << std::endl;
}
template <typename T, typename... Ts>
void write_to_cout(const T& value, const Ts&... args) {
  std::cout << value << ' ';
  write_to_cout(args...);
}
#define out(...) write_to_cout(__VA_ARGS__);

#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

using ll = long long;

using namespace std;

template <typename T>
std::map<T, int> Factorize(T x) {
  std::map<T, int> m;
  for (T i = 2; i * i <= x;) {
    if (x % i == 0) {
      m[i]++;
      x /= i;
    } else {
      ++i;
    }
  }
  if (x > 1) m[x]++;
  return m;
}

int main() {
  in(int, n, m);
  map<int, int> fs = Factorize(m);
  int ans = 1;
  function<void(map<int, int>::iterator, int)> dfs =
      [&](map<int, int>::iterator it, int x) {
        if (it == fs.end()) {
          if (m / x >= n) ans = max(ans, x);
          return;
        }
        rep(i, it->second + 1) {
          dfs(next(it), x);
          x *= it->first;
        }
      };
  dfs(fs.begin(), 1);
  out(ans);
}

#include <bits/stdc++.h>
using namespace std;

template<class T> inline bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }

const int INF = 1e9;

int main() {
    string S; cin >> S;
    int N = S.size();
    vector<vector<int>> dp(N + 1, vector<int>(2, INF));
    dp[0][0] = 0;
    for(int i = 0; i < N; ++i) {
        int d = S[i] - '0';
        chmin(dp[i + 1][0], min(dp[i][0], dp[i][1]) + d);
        chmin(dp[i + 1][1], min(dp[i][0] + 1, dp[i][1] - 1) + 10 - d);
    }
    cout << min(dp[N][0], dp[N][1]) << '\n';
    return 0;
}
