#include<bits/stdc++.h>
using namespace std;
bool check(int num){
	int num1 = num, num2 = num;
	while(num1 != 0){
		int x = num1 % 10;
		num1 /= 10;
		if(x == 7)
		    return 0;
	}
	while(num2 != 0){
		int x = num2 % 8;
		num2 /= 8;
		if(x == 7)
		    return 0;
	}
	return 1;
}
int main(){
	int N = 0, ans = 0;
	cin >> N;
	for(int i = 1 ; i <= N ; i ++)
	    if(check(i))
	        ans ++;
	cout << ans;
	
	return 0;
}
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
string s;
int n, res = 0;

void f(int i, int x, int cnt = 0) {
	if (i == n) {
		if (x % 3 == 0) {
			res = max(res, cnt);
		}
		return;
	}
	f(i + 1, x + s[i] - '0', cnt + 1);
	f(i + 1, x, cnt);
}

void solve() {
	cin >> s;
	n = s.size();
	f(0, 0);
	cout << (n - res == n ? -1 : n - res);
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int t = 1;
	//cin >> t;
	for (int i = 1; i <= t; i++) {
		solve();
	}
}