#include <bits/stdc++.h>
using namespace std;
const int maxn = 55;
long long one[maxn];
long long n;
long long dfs(long long n, long long i) {
  long long k = n / one[i];
  long long j = n % one[i];
  if (j == 0) return k * i;
  return k * i + min(i + dfs(one[i] - j, i - 1), dfs(j, i - 1));
}
int main() {
  one[0] = 0;
  for (int i = 1; i <= 16; i++) one[i] = 10 * one[i - 1] + 1;
  while (cin >> n) cout << dfs(n, 16) << endl;
  return 0;
}
