#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
long long f[20];
int foo(long long x) {
  if (!x) return 0;
  int a, b;
  for (int i = 0; i < 20; ++i)
    if (f[i] >= x) {
      if (f[i] - x >= x)
        a = INF;
      else
        a = foo(f[i] - x) + i;
      break;
    }
  for (int i = 0; i < 20; ++i)
    if (f[i + 1] > x) {
      b = foo(x % f[i]) + i * (x / f[i]);
      break;
    }
  return min(a, b);
}
int main() {
  for (int i = 0; i < 18; ++i) f[i + 1] = f[i] * 10 + 1;
  long long x;
  cin >> x;
  cout << foo(x) << endl;
  return 0;
}
