#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 1000005
using namespace std;
typedef long long LL;
char s[N];
int dp[N][2];
int main()
{
	scanf("%s",s+1);
	dp[0][1]=1;
	for(int i=1;s[i];i++)
	{
		int d=s[i]-'0';
		dp[i][0]=min(dp[i-1][0]+d,dp[i-1][1]+10-d);
		dp[i][1]=min(dp[i-1][0]+d+1,dp[i-1][1]+9-d);
		if(d==9)dp[i][1]=dp[i-1][1];
	}
	printf("%d\n",dp[strlen(s+1)][0]);
	return 0;
}

#include <bits/stdc++.h>
using namespace std;

//#define int long long
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define rep(i,n) for(int i=0;i<n;++i)
#define rep1(i,n) for(int i=1;i<n;++i)
#define exrep(i, a, b) for(ll i = a; i < b; i++)
#define out(x) cout << x << endl
#define EPS (1e-7)
#define gearup ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long double ld;
typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<pair<int,int> > vpii;
typedef vector<vector<int> > vvi;
typedef vector<vector<char> > vvc;
typedef vector<vector<bool> > vvb;
typedef vector<vector<double> > vvd;
typedef vector<vector<string> > vvs;
typedef vector<ll> vl;
typedef vector<vector<ll> > vvl;
typedef vector<vector<vector<ll> > > vvvl;
ll MOD = 1000000007;
const long long L_INF = 1LL << 60;
const int INF = 2147483647; // 2^31-1
const double PI = acos(-1);
//cout<<fixed<<setprecision(10);

template<class T> inline bool chmin(T& a, T b) {if (a > b) {a = b;return true;}return false;}
template<class T> inline bool chmax(T& a, T b) {if (a < b) {a = b;return true;}return false;}
template<class T> void debug(T v){rep(i,v.size()) cout<<v[i]<<" ";cout<<endl;}
const ll dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};

bool is_Prime(int x){
  if(x < 2)return false;
  else if(x == 2) return true;
  for(ll i = 2; i*i <= x; i++) if(x%i == 0) return false;
  return true;
}
vl res;
vl prime;
ll n;

signed main()
{
  gearup;
  cin >> n;
  //素数格納
  for(int i=2;i<=55555;i++){
    if(is_Prime(i) && (i % 5) == 1)prime.push_back(i);
  }
  rep(i,n){
    if(i)cout << " ";
    cout << prime[i];
  }
  cout << endl;
}