#include <bits/stdc++.h>
using namespace std;
int num(long long n) {
  int num = 0;
  while (n) {
    n /= 10;
    num++;
  }
  return num;
}
long long find(long long n) {
  int ex = num(n);
  long long cur = 0, ad = 1;
  for (int i = 0; i <= ex - 1; i++) {
    cur += ad;
    ad *= 10;
  }
  if (cur <= n) return cur;
  return ((cur - 1) / 10);
}
long long process(long long n) {
  if (n <= 6) return n;
  if (n <= 11) return (13 - n);
  long long less = find(n);
  long long more = less * 10 + 1;
  if (n % less == 0) {
    if (n <= more / 2 + less) return num(less) * n / less;
    return num(more) + process(more - n);
  } else {
    long long _m = (n / less) * less;
    long long res = process(_m) + process(n - _m);
    if (n > more / 2)
      res = min(process(_m) + process(n - _m), num(more) + process(more - n));
    return res;
  }
}
int main() {
  long long n;
  cin >> n;
  cout << process(n) << endl;
  return 0;
}

### Prompt

Develop a solution in CPP to the problem described below:

Prof. Vasechkin wants to represent positive integer n as a sum of addends, where each addends is an integer number containing only 1s. For example, he can represent 121 as 121=111+11+–1. Help him to find the least number of digits 1 in such sum.

Input

The first line of the input contains integer n (1 ≤ n < 1015).

Output

Print expected minimal number of digits 1.

Examples

Input

121


Output

6

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
int num(long long n) {
  int num = 0;
  while (n) {
    n /= 10;
    num++;
  }
  return num;
}
long long find(long long n) {
  int ex = num(n);
  long long cur = 0, ad = 1;
  for (int i = 0; i <= ex - 1; i++) {
    cur += ad;
    ad *= 10;
  }
  if (cur <= n) return cur;
  return ((cur - 1) / 10);
}
long long process(long long n) {
  if (n <= 6) return n;
  if (n <= 11) return (13 - n);
  long long less = find(n);
  long long more = less * 10 + 1;
  if (n % less == 0) {
    if (n <= more / 2 + less) return num(less) * n / less;
    return num(more) + process(more - n);
  } else {
    long long _m = (n / less) * less;
    long long res = process(_m) + process(n - _m);
    if (n > more / 2)
      res = min(process(_m) + process(n - _m), num(more) + process(more - n));
    return res;
  }
}
int main() {
  long long n;
  cin >> n;
  cout << process(n) << endl;
  return 0;
}

```