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