#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#ifdef _MSC_VER
#include <intrin.h>
inline unsigned int __builtin_popcount(unsigned int x){return __popcnt(x);}
#endif

using ll = long long;
using vll = std::vector<ll>;
using namespace std;
#define REP(i, x, n) for(int i = x; i < (n); i++)
#define rep(i, n) REP(i, 0, n)
const ll MOD = 1000000007;

int main() {
  string s; cin>>s;
  int k=s.length();
  int ans = 0;
  rep(i,1<<k){
    int x=0;
    rep(j,k){
      if(i>>j&1){
        x+=s[j]-'0';
      }
    }
    if(x%3==0){
      ans = max(ans, (int)__builtin_popcount(i));
    }
  }
  if(ans==0){
    puts("-1");
  }else{
    printf( "%d\n", k-ans );
  }
  return 0;
}
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define ff first
#define ss second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mod 1000000007
#define rep(i,n) for(int i=0; i<n; i++)
#define repa(i,start,end) for(int i=start; i<=end; i++)
#define repd(i,start,end) for(int i=start; i>=end; i--)
#define all(x) x.begin(),x.end()
#define debug(x) cout << "(" << #x << ": " << x << ")" << endl;

bool octals(int n) {
	int oct[100],i=0;
	while(n) {
		oct[i]=n%8;
		if(oct[i]==7)
			return true;
		i++;
		n/=8;
	}
	return false;
}

bool decs(int n) {
	int oct[100],i=0;
	while(n) {
		oct[i]=n%10;
		if(oct[i]==7)
			return true;
		i++;
		n/=10;
	}
	return false;
}

void solve() {
	int n;
	cin >> n;

	int ans=0;

	repa(i,1,n) {
		if(decs(i) || octals(i))
			ans++;
	}

	cout << n-ans << endl;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int t=1;
    while (t--)
    {
        solve();
    }
}