#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000000;
const long long p[] = {1,
                       11,
                       111,
                       1111,
                       11111,
                       111111,
                       1111111,
                       11111111,
                       111111111,
                       1111111111LL,
                       11111111111LL,
                       111111111111LL,
                       1111111111111LL,
                       11111111111111LL,
                       111111111111111LL,
                       1111111111111111LL};
long long n;
int dfs(long long x, int i) {
  if (x == 0) return 0;
  if (i < 0) return INF;
  if (p[i] * 10 < llabs(x)) return INF;
  int res = dfs(x, i - 1);
  if (llabs(x - p[i]) < x) res = min(res, dfs(llabs(x - p[i]), i) + i + 1);
  if (llabs(x + p[i]) < x) res = min(res, dfs(llabs(x + p[i]), i) + i + 1);
  return res;
}
int main() {
  cin >> n;
  cout << dfs(n, 15) << '\n';
}
