#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100 + 10;
long long n, a[20];
int DFS(long long n, int i) {
  int t = (n / a[i]) * i;
  n %= a[i];
  if (!n) return t;
  return t + min(DFS(n, i - 1), i + DFS(a[i] - n, i - 1));
}
int main() {
  for (int i = 1; i <= 16; ++i) a[i] = a[i - 1] * 10 + 1;
  cin >> n;
  cout << DFS(n, 16) << endl;
  return 0;
}
