#include <bits/stdc++.h>
using namespace std;
long long a[20];
long long dfs(long long n, int i) {
  int k = i * (n / a[i]);
  n %= a[i];
  if (!n) return k;
  return k + min(i + dfs(a[i] - n, i - 1), dfs(n, i - 1));
}
int main() {
  a[0] = 0;
  for (int i = 1; i <= 16; ++i) a[i] = a[i - 1] * 10 + 1;
  long long n;
  scanf("%I64d", &n);
  printf("%64d\n", dfs(n, 16));
  return 0;
}
