#include <memory.h>

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

#define MOD 998244353
#define INIT -1

int main()
{
   string n;
   cin >> n;
   // result[i][0] : i - 1桁めまで払ってる　繰り下がりはない
   // result[i][1] : i - 1桁目まで払ってる　繰り下がりあり
   int result[1000010][2] = {};
   result[0][0] = 0;
   result[0][1] = 1;
   for (int i = 1; i <= n.length(); i++)
   {
      result[i][0] = min(result[i - 1][0] + (n[i - 1] - '0'), result[i - 1][1] + (10 - (n[i - 1] - '0')));
      result[i][1] = min(result[i - 1][1] + (10 - (n[i - 1] - '0') - 1), result[i - 1][0] + (n[i - 1] - '0' + 1));
   }
   cout << result[n.length()][0] << endl;
}
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#define REP(i, n) for(int (i) = 0; (i) < (n); ++(i))
#define REPR(i, n) for(int (i) = (n); (i) >= 0; --(i))
#define FOR(i, n, m) for(int (i) = (n); (i) < (m); ++(i))

constexpr int INF = 1e9;
//constexpr ll INF = 1LL<<61;
constexpr ll mod = 1e9+7;

int main(){
  string S;
  cin >> S;
  int N = S.size();
  int ans = 0;;
  int c = 0;
  REPR(i, N-1){
    int x = S[i] - '0';
    if((x+c) > 5){
      ans += 10 - (x+c);
      c = 1;
    }
    else if((x+c) == 5){
      if(i > 0 && S[i-1] >= '5'){
        ans += 10 -(x+c);
        c= 1;
      }
      else{
        ans += (x+c);
        c = 0;
      }
    }
    else{
      ans+= x+c;
      c = 0;
    }
  }
  ans += c;
  cout << ans << endl;
  return 0;
}
