#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fbo find_by_order
#define ook order_of_key

typedef long long ll;
typedef pair<ll,ll> ii;
typedef vector<int> vi;
typedef long double ld;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

const long long mod = 1000000007;
const long long inf = 1e18;

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
    ll n;
    cin>>n;
    for(ll i=n;i>=0;i--){
        int k=sqrt(i);
        if(k*k==i){
            cout<<i<<endl;
            break;
        }
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define SELECTER(_1, _2, _3, SELECT, ...) SELECT
#define REP1(i, n) for(int (i)=0; (i)<(n); (i)++)
#define REP2(i, a, b) for(int (i)=(a); (i)<(b); (i)++)
#define REP(...) SELECTER(__VA_ARGS__, REP2, REP1,) (__VA_ARGS__)
#define MOD 1000000007

template <class T> ostream& operator<<(ostream& os, const vector<T>& v){ os << "{"; for(size_t i=0; i<v.size(); i++) os << v[i] << (i+1==v.size() ? "" : ", "); os << "}"; return os; }
template <class T, class U> ostream& operator<<(ostream& os, const pair<T, U>& p){ return os << "{" << p.first << ", " << p.second << "}"; }

int dp[1111111][2];

int main(){
    string S;
    cin >> S;
    int N = S.size();
    S += '0';

    for(int i=0; i<1111111; i++)
        dp[i][0] = dp[i][1] = 1<<30;
    dp[0][0] = 0;

    for(int i=0; i<N; i++){
        int d = S[N-1-i] - '0';
        dp[i+1][0] = min(dp[i+1][0], dp[i][0] + d);
        dp[i+1][1] = min(dp[i+1][1], dp[i][0] + (10 - d));
        dp[i+1][0] = min(dp[i+1][0], dp[i][1] + (d + 1));
        dp[i+1][1] = min(dp[i+1][1], dp[i][1] + (10 - (d + 1)));
    }
    int ans = min(dp[N][0], dp[N][1] + 1);
    cout << ans << endl;
    return 0;
}