#include<cstdio>

char s[1000006];
int dp[1000006][2];

int min(int x, int y) {
  return x < y ? x : y;
}

int main() {
  scanf("%s", s + 1);
  dp[0][0] = 0;
  dp[0][1] = 1;
  int len = 1;
  for (int i = 1; s[i]; len = i++) {
    dp[i][0] = min(dp[i - 1][0] + min(s[i] - '0', 11 - (s[i] - '0')), dp[i - 1][1] + 10 - (s[i] - '0'));
    dp[i][1] = min(dp[i - 1][0] + min(s[i] - '0' + 1, 10 - (s[i] - '0')), dp[i - 1][1] + 9 - (s[i] - '0'));
  }
  printf("%d\n", dp[len][0]);
}
