#include <bits/stdc++.h>
using namespace std;
long long int a[20];
long long int func(long long int n, long long int index) {
  n = abs(n);
  if (index == 1) return n;
  long long int temp = n / a[index];
  return min(temp * index + func(n - temp * a[index], index - 1),
             (temp + 1) * (index) + func(n - (temp + 1) * a[index], index - 1));
}
int main() {
  long long int n, i, j, k;
  cin >> n;
  a[1] = 1;
  for (i = 2; i <= 17; i++) a[i] = a[i - 1] * 10 + 1;
  long long int ans = func(n, 16);
  cout << ans << endl;
  return 0;
}
