#include <bits/stdc++.h>
using namespace std;
long long n, ans = 1e15 + 9, num, step, digss, sss;
long long z1[20] = {1,
                    11,
                    111,
                    1111,
                    11111,
                    111111,
                    1111111,
                    11111111,
                    111111111,
                    1111111111LL,
                    11111111111LL,
                    111111111111LL,
                    1111111111111LL,
                    11111111111111LL,
                    111111111111111LL,
                    1111111111111111LL,
                    11111111111111111LL,
                    111111111111111111LL};
map<long long, int> pans;
int caldig(long long x) {
  int digs = 0;
  while (x != 0) {
    x /= 10;
    digs++;
  }
  return digs;
}
long long dfs(long long num, int d) {
  if (d == 0) return num;
  int p1 = num / z1[d];
  int p2 = p1 + 1;
  int r1 = dfs(abs(num - p1 * z1[d]), d - 1);
  int r2 = dfs(abs(num - p2 * z1[d]), d - 1);
  return min(p1 * (d + 1) + r1, p2 * (d + 1) + r2);
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  cout << dfs(n, caldig(n)) << endl;
  return 0;
}
