#include #include #include #include #include using namespace std; int count(long long x) { int ret = 0; while (x) { ret += x % 5; if (x % 10 >= 5) ret++; x /= 10; } return ret; } int solve_all(long long n) { int d = log10(n) + 1; int ans = 1e9; const vector C = {0,1,2,5,6,7}; function rec = [&](int i, long long change) { if (i == d) { int cand = count(n + change) + count(change); ans = min(ans, cand); } else { for (int c : C) rec(i+1, change * 10 + c); } }; rec(0, 0); return ans; } int main() { long long n; cin >> n; cout << solve_all(n) << endl; return 0; }