stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include <iostream>

#include <cstdio>

#include <cmath>

#include <algorithm>

using namespace std;

int main()

{

	int N, M, i;

	cin >> N >> M;

	for (i = M / N; i >= 1; i--)

	{

		if (M%i == 0) {

			cout << i << endl;

			return 0;

		}

	}

	



}

// Faster C++ solution for the same problem (measured speedup: 2.9370775220930123x):
#include <stdio.h>

#include <math.h>



int d[20002], e[20002], c = 0;



int main()

{	 

	int n, m, i, k, j;

	

	scanf("%d%d", &n, &m);

	for(i = 1, k = sqrt(m); i <= k; i++){

		if(m % i == 0){

			d[c] = i;

			e[c++] = m / i;

		}

	}

	for(i = c - 1; i >= 0; i--){

		d[c] = e[i], e[c] = d[i], c++;

	}

	for(i = c - 1; i >= 0 && e[i] < n; i--);

	printf("%d\n", d[i]);

		

	return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main()

{

	ll i,ans=0,n,m;

	scanf("%lld%lld",&n,&m);

	for(i=1;i*i<=m;i++) if(m%i==0)

	{

		if(n*i<=m) ans=max(ans,i);

		if(n<=i) ans=max(ans,m/i);

	}

	cout<<ans<<endl;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 2.8392571741085577x):
#include <stdio.h>

#include <math.h>



int d[20002], e[20002], c = 0;



int main()

{	 

	int n, m, i, k, j;

	

	scanf("%d%d", &n, &m);

	for(i = 1, k = sqrt(m); i <= k; i++){

		if(m % i == 0){

			d[c] = i;

			e[c++] = m / i;

		}

	}

	for(i = c - 1; i >= 0; i--){

		d[c] = e[i], e[c] = d[i], c++;

	}

	for(i = c - 1; i >= 0 && e[i] < n; i--);

	printf("%d\n", d[i]);

		

	return 0;

}
// Slower C++ solution:
#include <iostream>

#include <cstring>

#include <string>

#include <cstdio>

#include <cmath>

using namespace std;

const int N = 2e5 + 100;

int n, ans, m;

int main()

{

    cin >> n >> m;

    for (int i = 1; i <= sqrt(m); i++)

    {

        if (m % i == 0)

        {

            if (i >= n)

                ans = max(m/i, ans);

            if (m / i >= n)

                ans = max(i, ans);

        }

    }

    cout << ans << endl;

}


// Faster C++ solution for the same problem (measured speedup: 2.9188321603053544x):
#include <stdio.h>

#include <math.h>



int d[20002], e[20002], c = 0;



int main()

{	 

	int n, m, i, k, j;

	

	scanf("%d%d", &n, &m);

	for(i = 1, k = sqrt(m); i <= k; i++){

		if(m % i == 0){

			d[c] = i;

			e[c++] = m / i;

		}

	}

	for(i = c - 1; i >= 0; i--){

		d[c] = e[i], e[c] = d[i], c++;

	}

	for(i = c - 1; i >= 0 && e[i] < n; i--);

	printf("%d\n", d[i]);

		

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;





int main()

{

    int n,m,a;

    cin>>n>>m;





    a=1;





    for(int i=1; i*i<=m; i++)

    {

        if(m%i==0)

        {

           if(m/i>=n) a=max(i,a);

           if(i>=n) a=max(m/i,a);

        }



    }

    cout<<a<<endl;



    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 2.8397970280237175x):
#include <stdio.h>

#include <math.h>



int d[20002], e[20002], c = 0;



int main()

{	 

	int n, m, i, k, j;

	

	scanf("%d%d", &n, &m);

	for(i = 1, k = sqrt(m); i <= k; i++){

		if(m % i == 0){

			d[c] = i;

			e[c++] = m / i;

		}

	}

	for(i = c - 1; i >= 0; i--){

		d[c] = e[i], e[c] = d[i], c++;

	}

	for(i = c - 1; i >= 0 && e[i] < n; i--);

	printf("%d\n", d[i]);

		

	return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

int main()

{

    int n, m;

    cin >> n >> m;

    for (int i = m / n; i >= 1; i--)

    {

        if (m % i == 0)

        {

            int t = m / i;

            if (t >= n)

            {

                cout << i << endl;

                break;

            }

        }

    }



    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 2.93758239373297x):
#include <stdio.h>

#include <math.h>



int d[20002], e[20002], c = 0;



int main()

{	 

	int n, m, i, k, j;

	

	scanf("%d%d", &n, &m);

	for(i = 1, k = sqrt(m); i <= k; i++){

		if(m % i == 0){

			d[c] = i;

			e[c++] = m / i;

		}

	}

	for(i = c - 1; i >= 0; i--){

		d[c] = e[i], e[c] = d[i], c++;

	}

	for(i = c - 1; i >= 0 && e[i] < n; i--);

	printf("%d\n", d[i]);

		

	return 0;

}
// Slower C++ solution:
#include <iostream>



using namespace std;



int main()

{

    int n, m;

    cin >> n >> m;

    for (int i = m / n; i >= 1; i--)

    {

        if (m % i == 0)

        {

            int j = m / i;

            if (j >= n)

            {

                cout << i << endl;

                break;

            }

        }

    }

}


// Faster C++ solution for the same problem (measured speedup: 2.93758239373297x):
#include <stdio.h>

#include <math.h>



int d[20002], e[20002], c = 0;



int main()

{	 

	int n, m, i, k, j;

	

	scanf("%d%d", &n, &m);

	for(i = 1, k = sqrt(m); i <= k; i++){

		if(m % i == 0){

			d[c] = i;

			e[c++] = m / i;

		}

	}

	for(i = c - 1; i >= 0; i--){

		d[c] = e[i], e[c] = d[i], c++;

	}

	for(i = c - 1; i >= 0 && e[i] < n; i--);

	printf("%d\n", d[i]);

		

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>



using namespace std;



int main()

{

    long long  n, m, d, c, i;

    cin>>n>>m;

    d=m/n;

    c=m%n;

    for(i=d; i>0 ;i--)

    {

        if(c%i==0)

        {

            cout<<i<<endl;

            return 0;

        }

        c+=n;

    }

}

// Faster C++ solution for the same problem (measured speedup: 2.908701167823305x):
#include <stdio.h>

#include <math.h>



int d[20002], e[20002], c = 0;



int main()

{	 

	int n, m, i, k, j;

	

	scanf("%d%d", &n, &m);

	for(i = 1, k = sqrt(m); i <= k; i++){

		if(m % i == 0){

			d[c] = i;

			e[c++] = m / i;

		}

	}

	for(i = c - 1; i >= 0; i--){

		d[c] = e[i], e[c] = d[i], c++;

	}

	for(i = c - 1; i >= 0 && e[i] < n; i--);

	printf("%d\n", d[i]);

		

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

using ll =long long;





int main()

{

    ll n,k;

    cin>>n>>k;

    vector<ll>v;



    for(ll i=1;i*i<=k;i++)

    {

        if(k%i==0)

        {

            v.push_back(i);

            if(k/i!=i)v.push_back(k/i);

        }

    }



    ll counter;



    sort(v.begin(),v.end());



    for(int i=0;i<v.size();i++)

    {

        if(k/v[i]>=n)counter=v[i];

    }

    cout<<counter<<endl;

}


// Faster C++ solution for the same problem (measured speedup: 2.8734141970189313x):
#include <stdio.h>

#include <math.h>



int d[20002], e[20002], c = 0;



int main()

{	 

	int n, m, i, k, j;

	

	scanf("%d%d", &n, &m);

	for(i = 1, k = sqrt(m); i <= k; i++){

		if(m % i == 0){

			d[c] = i;

			e[c++] = m / i;

		}

	}

	for(i = c - 1; i >= 0; i--){

		d[c] = e[i], e[c] = d[i], c++;

	}

	for(i = c - 1; i >= 0 && e[i] < n; i--);

	printf("%d\n", d[i]);

		

	return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

int n,m,ans;

int main(){

    cin>>n>>m;

    for(int i=m/n;i>=1;i--){

        if(m%i==0){

            int g=m/i;

            if(g>=n){

            	cout<<i<<endl;

            	return 0;

			}

        }

    }

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 2.9371241508064996x):
#include <stdio.h>

#include <math.h>



int d[20002], e[20002], c = 0;



int main()

{	 

	int n, m, i, k, j;

	

	scanf("%d%d", &n, &m);

	for(i = 1, k = sqrt(m); i <= k; i++){

		if(m % i == 0){

			d[c] = i;

			e[c++] = m / i;

		}

	}

	for(i = c - 1; i >= 0; i--){

		d[c] = e[i], e[c] = d[i], c++;

	}

	for(i = c - 1; i >= 0 && e[i] < n; i--);

	printf("%d\n", d[i]);

		

	return 0;

}
// Slower C++ solution:
#include <iostream>

using namespace std;

int main()

{

    long long n, m, ans = 0;

    cin >> n >> m;

    for(long long i = 1; i * i <= m; i++)

    {

        if(m % i == 0)

        {

            if(m >= n * i)

            {

                ans = max(ans, i);

            }

            if(m >= n * (m / i))

            {

                ans = max(ans, m / i);

            }

        }

    }

    cout << ans << endl;

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 2.839721583915229x):
#include <stdio.h>

#include <math.h>



int d[20002], e[20002], c = 0;



int main()

{	 

	int n, m, i, k, j;

	

	scanf("%d%d", &n, &m);

	for(i = 1, k = sqrt(m); i <= k; i++){

		if(m % i == 0){

			d[c] = i;

			e[c++] = m / i;

		}

	}

	for(i = c - 1; i >= 0; i--){

		d[c] = e[i], e[c] = d[i], c++;

	}

	for(i = c - 1; i >= 0 && e[i] < n; i--);

	printf("%d\n", d[i]);

		

	return 0;

}
// Slower C++ solution:
#include<iostream>

#include<vector>

#include<string>

#include<cmath>

#include<algorithm>

#include <numeric>

 

using namespace std;

using ll=long long;

#define rep(i,n) for(int i=0;i<n;i++)

#define ALL(a) (a).begin(),(a).end()



int main(){

    ll n, m;

    cin >> n >> m;



    if(m%n == 0){

        cout << m/n << endl;

        return 0;

    }

    ll mx = (ll)m/n;



    // vector<ll> div;

    ll ans = 1;

    for(ll i = 2; i <= m/n; i++){

        if(m%i == 0)ans = i;

    }

    cout << ans << endl;



}

// Faster C++ solution for the same problem (measured speedup: 1.225581879626275x):
#include <bits/stdc++.h>

 

#define rep(i,n) for(ll i=0;i<n;++i)

#define all_map(itr,mp) for(auto itr=mp.begin();itr!=mp.end();itr++)

#define ALL(a) (a).begin(),(a).end()



using namespace std;

using ll  = long long;

using pll = pair<ll, ll>;



const ll MOD  = 1e9 + 7;

const ll LINF = 1LL << 62;

const int INF = 1e9 + 7;



int main(){

    ll n, m;

    cin >> n >> m;



    for(ll i = m/n; i >= 1; --i){

        ll dif = m - i*n;

        if(dif % i == 0){

            cout << i << endl;

            return 0;

        }

    }

}
// Slower C++ solution:
#include<stdio.h>

#include<string.h>

int dp[1000005][2],num[1000005]={0};

char tmp[1000005];

int min(int a,int b){

	return a<b?a:b;

}

int main()

{

	int len,ans;

    for(int i=0;i<1000005;i++)for(int j=0;j<2;j++)dp[i][j]=99;

    scanf("%s",tmp);

    len=strlen(tmp);

    for(int i=1;i<=len;i++)num[i]=tmp[i-1]-'0';

    dp[len][0]=num[len];dp[len][1]=10-num[len];

    for(int i=len-1;i>=0;i--)

    {

    	/*数位*/

    	dp[i][0]=min(dp[i+1][0]+num[i],dp[i+1][1]+num[i]+1),

        dp[i][1]=min(10+dp[i+1][0]-num[i],10+dp[i+1][1]-(num[i]+1));

	}

	ans=min(dp[0][0],dp[0][1]);

    printf("%d\n",ans);

}

// Faster C++ solution for the same problem (measured speedup: 8.689433257721026x):
#include <cstdio>

#include <iostream>

#include <algorithm>

#include <cstring>

#include <queue> //priority_queue 优先队列

#include <map>

#include <set> //multiset  set<int>::reverse_iterator rit; for(rit=s.rbegin();rit!=s.rend();rit++)

#include <vector>

#include <stack>

#include <cmath>

#include <utility>

#include <sstream>

#include <string>

#define mod 1000000007

#define mod9 998244353

typedef unsigned long long ull;

typedef long long ll;

typedef double db;

const db eps=1e-6;

const int INF = 0x3f3f3f3f;

const ll inf=0x3f3f3f3f3f3f3f3f;

const int N=1e6+8;

inline ll read(){ll s=0,w=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();return s*w;}

using namespace std;



int t,n,m,k,ans=0,cnt=0;

char a[N]={0};

int b[N]={0};

int main(){

    scanf("%s",a+1);

    int len=strlen(a+1);

    for(int i=1;i<=len;i++) b[i]=a[i]-'0';

    for(int i=len;i>=0;i--){

        int sum=b[i];

        int pre=b[i-1];

        if(sum<5){

            ans+=sum;

        }else if(sum>5){

            ans+=10-sum;

            b[i-1]++;

        }else if(pre>=5){

            ans+=10-sum;

            b[i-1]++;

        }else{

            ans+=sum;

        }

    }

    printf("%d\n",ans);

    return 0;

}
// Slower C++ solution:
#include <iostream>

using namespace std;

bool carry = false;

int main() {

  ios::sync_with_stdio(0);

  cin.tie(0);

  string S;

  cin >> S;

  int N = S.size();

  int ans = 0;

  for (int i = N; i >= 0; i--) {

    int n = S[i] - '0';

    if (carry) n++;

    switch(n){

    case 5:

      ans+=5;

      break;

    case 0:

    case 10:

      break;

    case 1:

    case 9:

      ans+=1;

      break;

    case 2:

    case 8:

      ans+=2;

      break;

    case 3:

    case 7:

      ans+=3;

      break;

    case 4:

    case 6:

      ans+=4;

      break;

    }

    carry = (n > 5 || (n == 5 && i > 0 && S[i-1] > '4'));

  }

  if (carry) ans++;

  cout << ans << '\n';

  return 0;

}

 

// Faster C++ solution for the same problem (measured speedup: 5.383056124397258x):
#include<cstdio>

using namespace std;

int main() {

  int flag = 1;

  int flag2 = 1;

  int ans = 0;

  int c;

  int n;

  while ((c = getchar_unlocked()) >= '0') {

    n = c - '0';

    flag2 = flag;

    if (n < 4) {

      if (flag == -1) {

        ans += n + 1;

      } else {

        ans += n;

      }

      flag = 1;

    }

    else if (n > 5) {

      if (flag == 1) {

        ans += 10 - n;

      }

      else {

        ans += 9 - n;

      }

      flag = -1;

    } else if (n == 4) {

      if (flag == -1) {

        flag = 0;

        ans += 5;

      }

      else {

        flag = 1;

        ans += 4;

      }

    } else if (n == 5) {

      if (flag == 1) {

        flag = 0;

        ans += 5;

      } else {

        flag = -1;

        ans += 4;

      }

    }

  }

  if (n > 5 || (n == 5 && flag2 != 1)){

    ++ans;

  }

  printf("%d\n", ans);

  return 0;

}
// Slower C++ solution:
#include <iostream>

using namespace std;

bool carry = false;

int main() {

  ios::sync_with_stdio(0);

  cin.tie(0);

  string S;

  cin >> S;

  int N = S.size();

  int ans = 0;

  int flag = 1;

  for (int i = 0; i < N-1; i++) {

    int n = S[i] - '0';

    if (n < 4) {

      if (flag == -1) {

        ans += n + 1;

      } else {

        ans += n;

      }

      flag = 1;

    }

    else if (n > 5) {

      if (flag == 1) {

        ans += 10 - n;

      }

      else {

        ans += 9 - n;

      }

      flag = -1;

    } else if(n == 4) {

      if (flag == -1) {

        flag = 0;

        ans += 5;

      }

      else {

        flag = 1;

        ans += 4;

      }

    } else if (n == 5) {

      if (flag == 1) {

        flag = 0;

        ans += 5;

      } else {

        flag = -1;

        ans += 4;

      }

    }

  }

  int last = S[N - 1] - '0';

  if(last <= 4) {

    if(flag == -1) {

      ans += last + 1;

    } else {

      ans += last;

    }

  } else if (last == 5) {

    ans += 5;

  } else {

    if(flag == 1) {

      ans += 11 - last;

    } else {

      ans += 10 - last;

    }

  }

  printf("%d\n", ans);

  return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.3695452127795145x):
#include<cstdio>

using namespace std;

int main() {

  int flag = 1;

  int flag2 = 1;

  int ans = 0;

  int c;

  int n;

  while ((c = getchar_unlocked()) >= '0') {

    n = c - '0';

    flag2 = flag;

    if (n < 4) {

      if (flag == -1) {

        ans += n + 1;

      } else {

        ans += n;

      }

      flag = 1;

    }

    else if (n > 5) {

      if (flag == 1) {

        ans += 10 - n;

      }

      else {

        ans += 9 - n;

      }

      flag = -1;

    } else if (n == 4) {

      if (flag == -1) {

        flag = 0;

        ans += 5;

      }

      else {

        flag = 1;

        ans += 4;

      }

    } else if (n == 5) {

      if (flag == 1) {

        flag = 0;

        ans += 5;

      } else {

        flag = -1;

        ans += 4;

      }

    }

  }

  if (n > 5 || (n == 5 && flag2 != 1)){

    ++ans;

  }

  printf("%d\n", ans);

  return 0;

}
// Slower C++ solution:
#include <iostream>

using namespace std;

bool carry = false;

int main() {

  ios::sync_with_stdio(0);

  cin.tie(0);

  string S;

  cin >> S;

  int N = S.size();

  int ans = 0;

  for (int i = N; i >= 0; i--) {

    int n = S[i] - '0';

    if (carry) n++;

    switch(n){

    case 5:

      ans+=5;

      break;

    case 0:

    case 10:

      break;

    case 1:

    case 9:

      ans+=1;

      break;

    case 2:

    case 8:

      ans+=2;

      break;

    case 3:

    case 7:

      ans+=3;

      break;

    case 4:

    case 6:

      ans+=4;

      break;

    }

    carry = (n > 5 || (n == 5 && i > 0 && S[i-1] > '4'));

  }

  if (carry) ans++;

  cout << ans << '\n';

  return 0;

}

 

// Faster C++ solution for the same problem (measured speedup: 5.383056124397258x):
#include<cstdio>

using namespace std;

int main() {

  int flag = 1;

  int flag2 = 1;

  int ans = 0;

  int c;

  int n;

  while ((c = getchar_unlocked()) >= '0') {

    n = c - '0';

    flag2 = flag;

    if (n < 4) {

      if (flag == -1) {

        ans += n + 1;

      } else {

        ans += n;

      }

      flag = 1;

    }

    else if (n > 5) {

      if (flag == 1) {

        ans += 10 - n;

      } else {

        ans += 9 - n;

      }

      flag = -1;

    } else if (n == 4) {

      if (flag == -1) {

        flag = 0;

        ans += 5;

      } else {

        flag = 1;

        ans += 4;

      }

    } else {

      if (flag == 1) {

        flag = 0;

        ans += 5;

      } else {

        flag = -1;

        ans += 4;

      }

    }

  }

  if (n > 5 || (n == 5 && flag2 != 1)){

    ++ans;

  }

  printf("%d\n", ans);

  return 0;

}
// Slower C++ solution:
#include <iostream>

using namespace std;

bool carry = false;

int main() {

  ios::sync_with_stdio(0);

  cin.tie(0);

  string S;

  cin >> S;

  int N = S.size();

  int ans = 0;

  int flag = 1;

  for (int i = 0; i < N-1; i++) {

    int n = S[i] - '0';

    if (n < 4) {

      if (flag == -1) {

        ans += n + 1;

      } else {

        ans += n;

      }

      flag = 1;

    }

    else if (n > 5) {

      if (flag == 1) {

        ans += 10 - n;

      }

      else {

        ans += 9 - n;

      }

      flag = -1;

    } else if(n == 4) {

      if (flag == -1) {

        flag = 0;

        ans += 5;

      }

      else {

        flag = 1;

        ans += 4;

      }

    } else if (n == 5) {

      if (flag == 1) {

        flag = 0;

        ans += 5;

      } else {

        flag = -1;

        ans += 4;

      }

    }

  }

  int last = S[N - 1] - '0';

  if(last <= 4) {

    if(flag == -1) {

      ans += last + 1;

    } else {

      ans += last;

    }

  } else if (last == 5) {

    ans += 5;

  } else {

    if(flag == 1) {

      ans += 11 - last;

    } else {

      ans += 10 - last;

    }

  }

  printf("%d\n", ans);

  return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.3695452127795145x):
#include<cstdio>

using namespace std;

int main() {

  int flag = 1;

  int flag2 = 1;

  int ans = 0;

  int c;

  int n;

  while ((c = getchar_unlocked()) >= '0') {

    n = c - '0';

    flag2 = flag;

    if (n < 4) {

      if (flag == -1) {

        ans += n + 1;

      } else {

        ans += n;

      }

      flag = 1;

    }

    else if (n > 5) {

      if (flag == 1) {

        ans += 10 - n;

      } else {

        ans += 9 - n;

      }

      flag = -1;

    } else if (n == 4) {

      if (flag == -1) {

        flag = 0;

        ans += 5;

      } else {

        flag = 1;

        ans += 4;

      }

    } else {

      if (flag == 1) {

        flag = 0;

        ans += 5;

      } else {

        flag = -1;

        ans += 4;

      }

    }

  }

  if (n > 5 || (n == 5 && flag2 != 1)){

    ++ans;

  }

  printf("%d\n", ans);

  return 0;

}
// Slower C++ solution:
#include <iostream>

using namespace std;

bool carry = false;

int main() {

  ios::sync_with_stdio(0);

  cin.tie(0);

  string S;

  cin >> S;

  int N = S.size();

  int ans = 0;

  for (int i = N; i >= 0; i--) {

    int n = S[i] - '0';

    if (carry) n++;

    switch(n){

    case 5:

      ans+=5;

      break;

    case 0:

    case 10:

      break;

    case 1:

    case 9:

      ans+=1;

      break;

    case 2:

    case 8:

      ans+=2;

      break;

    case 3:

    case 7:

      ans+=3;

      break;

    case 4:

    case 6:

      ans+=4;

      break;

    }

    carry = (n > 5 || (n == 5 && i > 0 && S[i-1] > '4'));

  }

  if (carry) ans++;

  cout << ans << '\n';

  return 0;

}

 

// Faster C++ solution for the same problem (measured speedup: 5.386886649935574x):
#include<cstdio>

using namespace std;

int main() {

  int flag = 1;

  int flag2 = 1;

  int ans = 0;

  int c;

  int n;

  while ((c = getchar_unlocked()) >= '0') {

    n = c - '0';

    flag2 = flag;

    if (n == 5) {

      if (flag == 1) {

        flag = 0;

        ans += 5;

      } else {

        flag = -1;

        ans += 4;

      }

    } else if (n < 4) {

      if (flag == -1) {

        ans += n + 1;

      } else {

        ans += n;

      }

      flag = 1;

    } else if (n > 5) {

      if (flag == 1) {

        ans += 10 - n;

      } else {

        ans += 9 - n;

      }

      flag = -1;

    } else {

      if (flag == -1) {

        flag = 0;

        ans += 5;

      } else {

        flag = 1;

        ans += 4;

      }

    }

  }

  if (n > 5 || (n == 5 && flag2 != 1)){

    ++ans;

  }

  printf("%d\n", ans);

  return 0;

}
// Slower C++ solution:
#include <iostream>

using namespace std;

bool carry = false;

int main() {

  ios::sync_with_stdio(0);

  cin.tie(0);

  string S;

  cin >> S;

  int N = S.size();

  int ans = 0;

  int flag = 1;

  for (int i = 0; i < N-1; i++) {

    int n = S[i] - '0';

    if (n < 4) {

      if (flag == -1) {

        ans += n + 1;

      } else {

        ans += n;

      }

      flag = 1;

    }

    else if (n > 5) {

      if (flag == 1) {

        ans += 10 - n;

      }

      else {

        ans += 9 - n;

      }

      flag = -1;

    } else if(n == 4) {

      if (flag == -1) {

        flag = 0;

        ans += 5;

      }

      else {

        flag = 1;

        ans += 4;

      }

    } else if (n == 5) {

      if (flag == 1) {

        flag = 0;

        ans += 5;

      } else {

        flag = -1;

        ans += 4;

      }

    }

  }

  int last = S[N - 1] - '0';

  if(last <= 4) {

    if(flag == -1) {

      ans += last + 1;

    } else {

      ans += last;

    }

  } else if (last == 5) {

    ans += 5;

  } else {

    if(flag == 1) {

      ans += 11 - last;

    } else {

      ans += 10 - last;

    }

  }

  printf("%d\n", ans);

  return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.37336612409669x):
#include<cstdio>

using namespace std;

int main() {

  int flag = 1;

  int flag2 = 1;

  int ans = 0;

  int c;

  int n;

  while ((c = getchar_unlocked()) >= '0') {

    n = c - '0';

    flag2 = flag;

    if (n == 5) {

      if (flag == 1) {

        flag = 0;

        ans += 5;

      } else {

        flag = -1;

        ans += 4;

      }

    } else if (n < 4) {

      if (flag == -1) {

        ans += n + 1;

      } else {

        ans += n;

      }

      flag = 1;

    } else if (n > 5) {

      if (flag == 1) {

        ans += 10 - n;

      } else {

        ans += 9 - n;

      }

      flag = -1;

    } else {

      if (flag == -1) {

        flag = 0;

        ans += 5;

      } else {

        flag = 1;

        ans += 4;

      }

    }

  }

  if (n > 5 || (n == 5 && flag2 != 1)){

    ++ans;

  }

  printf("%d\n", ans);

  return 0;

}