#include <bits/stdc++.h>
using namespace std;
long long num[20], n;
long long dfs(long long n, int d) {
  if (n == 0) return 0;
  if (d == 1) return n;
  int p1 = n / num[d], p2 = p1 + 1;
  long long r1 = dfs(abs(n - p1 * num[d]), d - 1);
  long long r2 = dfs(abs(n - p2 * num[d]), d - 1);
  return min(p1 * d + r1, p2 * d + r2);
}
int main() {
  for (int i = 1; i <= 16; i++) num[i] = num[i - 1] * 10 + 1;
  cin >> n;
  cout << dfs(n, 16);
  return 0;
}
