#include <bits/stdc++.h>
#include <math.h>

using namespace std;

int minCoins(unsigned long V){
    // coins = {1,2,5,10,20,50,100,200,500,1000,2000,5000....}
    int d,k;
    k = 0;
    d = 1; 
    int table[V+1];
    table[0] = 0;
    for(int i = 1; i <= V; i++){
        table[i] = INT_MAX;
    }
    for(int i = 1; i <= V;  i++){
        while(d * pow(10,k) <= i){
            unsigned long value = d * pow(10,k);
            unsigned long sub_res = table[i - value];
            if (sub_res != INT_MAX && sub_res + 1 < table[i]) 
                  table[i] = sub_res + 1; 

            // calc next number
            switch(d){
                case 1: {
                    d = 2;
                    break;
                }
                case 2:{
                    d = 5;
                    break;
                }
                case 5:{
                    d = 1;
                    k += 1;
                    break;                    
                }
            }
        }
    }

}


int main(){
    unsigned long V;
    cin >> V;
    cout << minCoins(V);
    return 0;
}