#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll read(){
	ll a=0,b=getchar(),c=1;
	while(!isdigit(b))c=b=='-'?-1:1,b=getchar();
	while(isdigit(b))a=a*10+b-'0',b=getchar();
	return a*c;
}
string s;
int main(){
	cin >> s;
	if(s.size()<26){
		for(char c='a';c<='z';c++)
			if(s.find(c)==string::npos){
				cout << s << c;
				return 0;
			}
	}
	for(int i=s.size()-2;i>=0;i--){
		if(s[i]<s[i+1]){
			char t=s[i+1];
				for(int j=i+1;j<s.size();j++)
					if(s[j]>s[i])t=min(t,s[j]);
			s[i]=t;
			s=s.substr(0,i+1);
			cout << s;
			return 0;
		}
	}
	puts("-1");
	return 0;
}
#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <assert.h>
#include <numeric>
#include <time.h>
#include <map>
#include <unordered_map>
#include <stack>
#include <queue>

using namespace std;
typedef long long ll;

#define rep(i, n) for (int i = 0; i < n; i++)
#define all(c) (c).begin(), (c).end()
#define P pair<int, int>
#define chmin(x, y) x = min(x, y)

int main()
{
  string s;
  cin >> s;
  reverse(all(s));
  s += '0';

  int n = s.size();

  const int INF = 100100100;
  vector<vector<int>> dp(n + 1, vector<int>(2, INF));
  dp[0][0] = 0;

  for (int i = 0; i < n; i++)
  {
    for (int j = 0; j < 2; j++)
    {
      int x = s[i] - '0';
      x += j;
      if (x < 10)
        chmin(dp[i + 1][0], dp[i][j] + x);
      if (x > 0)
        chmin(dp[i + 1][1], dp[i][j] + (10 - x));
    }
  }

  cout << dp[n][0] << endl;
}
