#include <bits/stdc++.h>
using namespace std;
long long n, one[20];
int bt(long long n, int d) {
  int q = n / one[d] * d;
  if (!(n %= one[d])) return q;
  return q + min(d + bt(one[d] - n, d - 1), bt(n, d - 1));
}
int main() {
  cin >> n;
  for (int i = 1; i <= 16; i++) one[i] = 10 * one[i - 1] + 1;
  cout << bt(n, 16) << '\n';
  return 0;
}
